diff --git a/README.md b/README.md index 0416a45..4d37329 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,12 @@ Install using NPM (`npm install tinyqueue`), then import as a module: import TinyQueue from 'tinyqueue'; ``` +Or use CommonJS `require`: + +```js +const TinyQueue = require('tinyqueue'); +``` + Or use a browser build from a CDN: ```html diff --git a/index.cjs b/index.cjs new file mode 100644 index 0000000..a3036e0 --- /dev/null +++ b/index.cjs @@ -0,0 +1,74 @@ + +class TinyQueue { + constructor(data = [], compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0)) { + this.data = data; + this.length = this.data.length; + this.compare = compare; + + if (this.length > 0) { + for (let i = (this.length >> 1) - 1; i >= 0; i--) this._down(i); + } + } + + push(item) { + this.data.push(item); + this._up(this.length++); + } + + pop() { + if (this.length === 0) return undefined; + + const top = this.data[0]; + const bottom = this.data.pop(); + + if (--this.length > 0) { + this.data[0] = bottom; + this._down(0); + } + + return top; + } + + peek() { + return this.data[0]; + } + + _up(pos) { + const {data, compare} = this; + const item = data[pos]; + + while (pos > 0) { + const parent = (pos - 1) >> 1; + const current = data[parent]; + if (compare(item, current) >= 0) break; + data[pos] = current; + pos = parent; + } + + data[pos] = item; + } + + _down(pos) { + const {data, compare} = this; + const halfLength = this.length >> 1; + const item = data[pos]; + + while (pos < halfLength) { + let bestChild = (pos << 1) + 1; // initially it is the left child + const right = bestChild + 1; + + if (right < this.length && compare(data[right], data[bestChild]) < 0) { + bestChild = right; + } + if (compare(data[bestChild], item) >= 0) break; + + data[pos] = data[bestChild]; + pos = bestChild; + } + + data[pos] = item; + } +} + +module.exports = TinyQueue; +module.exports.default = TinyQueue; diff --git a/index.js b/index.js index 813c9eb..efed8fb 100644 --- a/index.js +++ b/index.js @@ -1,71 +1,2 @@ -export default class TinyQueue { - constructor(data = [], compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0)) { - this.data = data; - this.length = this.data.length; - this.compare = compare; - - if (this.length > 0) { - for (let i = (this.length >> 1) - 1; i >= 0; i--) this._down(i); - } - } - - push(item) { - this.data.push(item); - this._up(this.length++); - } - - pop() { - if (this.length === 0) return undefined; - - const top = this.data[0]; - const bottom = this.data.pop(); - - if (--this.length > 0) { - this.data[0] = bottom; - this._down(0); - } - - return top; - } - - peek() { - return this.data[0]; - } - - _up(pos) { - const {data, compare} = this; - const item = data[pos]; - - while (pos > 0) { - const parent = (pos - 1) >> 1; - const current = data[parent]; - if (compare(item, current) >= 0) break; - data[pos] = current; - pos = parent; - } - - data[pos] = item; - } - - _down(pos) { - const {data, compare} = this; - const halfLength = this.length >> 1; - const item = data[pos]; - - while (pos < halfLength) { - let bestChild = (pos << 1) + 1; // initially it is the left child - const right = bestChild + 1; - - if (right < this.length && compare(data[right], data[bestChild]) < 0) { - bestChild = right; - } - if (compare(data[bestChild], item) >= 0) break; - - data[pos] = data[bestChild]; - pos = bestChild; - } - - data[pos] = item; - } -} +export {default} from './index.cjs'; diff --git a/package.json b/package.json index 3b2a54a..d3e8f06 100644 --- a/package.json +++ b/package.json @@ -3,18 +3,25 @@ "version": "3.0.0", "description": "The smallest and simplest JavaScript priority queue", "type": "module", - "main": "index.js", + "main": "./index.cjs", "module": "index.js", "types": "index.d.ts", + "exports": { + "types": "./index.d.ts", + "import": "./index.js", + "require": "./index.cjs" + }, "scripts": { - "lint": "eslint *.js", + "lint": "eslint *.js *.cjs", "pretest": "npm run lint", "test": "node --test", "bench": "node bench.js", "prepublishOnly": "npm test" }, "files": [ - "index.d.ts" + "index.d.ts", + "index.cjs", + "index.js" ], "repository": { "type": "git", diff --git a/test.js b/test.js index cca97a3..1d1b5c1 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,7 @@ import test from 'node:test'; import assert from 'node:assert/strict'; +import {createRequire} from 'node:module'; import TinyQueue from './index.js'; @@ -52,3 +53,12 @@ test('handles init with empty array', () => { assert.deepEqual(queue.data, []); }); + +test('CJS require works', () => { + const require_ = createRequire(import.meta.url); + const Queue = require_('./'); + const queue = new Queue([3, 1, 2]); + assert.equal(queue.pop(), 1); + assert.equal(queue.pop(), 2); + assert.equal(queue.pop(), 3); +});