Skip to content

Commit 4f72841

Browse files
committed
add type-checking and first-class TypeScript types
1 parent 0bf254a commit 4f72841

File tree

6 files changed

+79
-65
lines changed

6 files changed

+79
-65
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
*.log
2-
yarn.lock
3-
pnpm-lock.yaml
4-
pacakge-lock.json
52
node_modules
6-
index.umd.js
3+
index.d.ts

index.d.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

index.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
11

2+
/** @template T */
23
export default class FlatQueue {
34

45
constructor() {
6+
/** @type T[] */
57
this.ids = [];
8+
9+
/** @type number[] */
610
this.values = [];
11+
12+
/** Number of items in the queue. */
713
this.length = 0;
814
}
915

16+
/**
17+
* Removes all items from the queue.
18+
*/
1019
clear() {
1120
this.length = 0;
1221
}
1322

14-
push(id, value) {
23+
/**
24+
* Adds `item` to the queue with the specified `priority`.
25+
*
26+
* `priority` must be a number. Items are sorted and returned from low to
27+
* high priority. Multiple items with the same priority value can be added
28+
* to the queue, but there is no guaranteed order between these items.
29+
*
30+
* @param {T} item
31+
* @param {number} priority
32+
*/
33+
push(item, priority) {
1534
let pos = this.length++;
1635

1736
while (pos > 0) {
1837
const parent = (pos - 1) >> 1;
1938
const parentValue = this.values[parent];
20-
if (value >= parentValue) break;
39+
if (priority >= parentValue) break;
2140
this.ids[pos] = this.ids[parent];
2241
this.values[pos] = parentValue;
2342
pos = parent;
2443
}
2544

26-
this.ids[pos] = id;
27-
this.values[pos] = value;
45+
this.ids[pos] = item;
46+
this.values[pos] = priority;
2847
}
2948

49+
/**
50+
* Removes and returns the item from the head of this queue, which is one of
51+
* the items with the lowest priority. If this queue is empty, returns
52+
* `undefined`.
53+
*/
3054
pop() {
3155
if (this.length === 0) return undefined;
3256

@@ -58,16 +82,32 @@ export default class FlatQueue {
5882
return top;
5983
}
6084

85+
/**
86+
* Returns the item from the head of this queue without removing it. If this
87+
* queue is empty, returns `undefined`.
88+
*/
6189
peek() {
6290
if (this.length === 0) return undefined;
6391
return this.ids[0];
6492
}
6593

94+
/**
95+
* Returns the priority value of the item at the head of this queue without
96+
* removing it. If this queue is empty, returns `undefined`.
97+
*/
6698
peekValue() {
6799
if (this.length === 0) return undefined;
68100
return this.values[0];
69101
}
70102

103+
/**
104+
* Shrinks the internal arrays to `this.length`.
105+
*
106+
* `pop()` and `clear()` calls don't free memory automatically to avoid
107+
* unnecessary resize operations. This also means that items that have been
108+
* added to the queue can't be garbage collected until a new item is pushed
109+
* in their place, or this method is called.
110+
*/
71111
shrink() {
72112
this.ids.length = this.values.length = this.length;
73113
}

package-lock.json

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
"types": "./index.d.ts",
1313
"devDependencies": {
1414
"eslint": "^9.32.0",
15-
"eslint-config-mourner": "^4.1.0"
15+
"eslint-config-mourner": "^4.1.0",
16+
"typescript": "^5.9.2"
1617
},
1718
"scripts": {
1819
"pretest": "eslint index.js test.js",
19-
"test": "node test.js"
20+
"test": "tsc && node test.js"
2021
},
2122
"files": [
2223
"index.js",

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"strict": true,
6+
"emitDeclarationOnly": true,
7+
"declaration": true,
8+
"target": "es2017",
9+
"module": "nodenext",
10+
"moduleResolution": "nodenext"
11+
},
12+
"files": [
13+
"index.js"
14+
]
15+
}

0 commit comments

Comments
 (0)