forked from Azure/login
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrioArray.js
More file actions
66 lines (61 loc) · 1.23 KB
/
Copy pathPrioArray.js
File metadata and controls
66 lines (61 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Creates an Array which adds items by priority
*/
export default function PrioArray () {
this.reset()
}
PrioArray.prototype = {
/**
* length of Array
*/
get length () {
return this.items.length
},
/**
* shift item from array
* @return {Any} item
*/
shift () {
return (this.items.shift() || /* istanbul ignore next */ {}).item
},
/**
* push `item` to Array using priority
* @param {Any} item
* @param {Number} [prio=Infinity] - priority `0 ... Infinity` - lower values have higher priority
*/
push (item, prio) {
const items = this.items
if (typeof prio !== 'number') {
prio = Infinity
items.push({ prio, item })
} else {
let found
prio = Math.abs(prio)
for (let i = 0; i < items.length; i++) {
if (prio < items[i].prio) {
items.splice(i, 0, { prio, item })
found = true
break
}
}
if (!found) {
items.push({ prio, item })
}
}
return this
},
/**
* unshift `item` to Array using priority
* @param {Any} item
*/
unshift (item) {
this.items.unshift({ prio: 0, item })
return this
},
/**
* removes all items in the Array
*/
reset () {
this.items = []
}
}