Skip to content

Commit bdb6f30

Browse files
committed
more idiomatic ES
1 parent b838838 commit bdb6f30

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

index.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
function defaultCompare(a, b) {
2-
return a < b ? -1 : a > b ? 1 : 0;
3-
}
41

52
export default class TinyQueue {
6-
constructor(data, compare) {
7-
this.data = data || [];
3+
constructor(data = [], compare = defaultCompare) {
4+
this.data = data;
85
this.length = this.data.length;
9-
this.compare = compare || defaultCompare;
6+
this.compare = compare;
107

118
if (this.length > 0) {
129
for (let i = (this.length >> 1) - 1; i >= 0; i--) this._down(i);
1310
}
1411
}
12+
1513
push(item) {
1614
this.data.push(item);
1715
this.length++;
1816
this._up(this.length - 1);
1917
}
18+
2019
pop() {
2120
if (this.length === 0) return undefined;
2221

@@ -31,12 +30,13 @@ export default class TinyQueue {
3130

3231
return top;
3332
}
33+
3434
peek() {
3535
return this.data[0];
3636
}
37+
3738
_up(pos) {
38-
const data = this.data;
39-
const compare = this.compare;
39+
const {data, compare} = this;
4040
const item = data[pos];
4141

4242
while (pos > 0) {
@@ -49,16 +49,16 @@ export default class TinyQueue {
4949

5050
data[pos] = item;
5151
}
52+
5253
_down(pos) {
53-
const data = this.data;
54-
const compare = this.compare;
54+
const {data, compare} = this;
5555
const halfLength = this.length >> 1;
5656
const item = data[pos];
5757

5858
while (pos < halfLength) {
5959
let left = (pos << 1) + 1;
60-
let right = left + 1;
6160
let best = data[left];
61+
const right = left + 1;
6262

6363
if (right < this.length && compare(data[right], best) < 0) {
6464
left = right;
@@ -73,3 +73,7 @@ export default class TinyQueue {
7373
data[pos] = item;
7474
}
7575
}
76+
77+
function defaultCompare(a, b) {
78+
return a < b ? -1 : a > b ? 1 : 0;
79+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"sourceType": "module"
5252
},
5353
"rules": {
54-
"no-var": "error"
54+
"no-var": "error",
55+
"prefer-const": "error"
5556
}
5657
}
5758
}

0 commit comments

Comments
 (0)