Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -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;
71 changes: 1 addition & 70 deletions index.js
Original file line number Diff line number Diff line change
@@ -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';
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
Loading