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
91 lines (80 loc) · 1.55 KB
/
Copy pathPrioArray.js
File metadata and controls
91 lines (80 loc) · 1.55 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = PrioArray;
/**
* Creates an Array which adds items by priority
*/
function PrioArray() {
this.reset();
}
PrioArray.prototype = {
/**
* length of Array
*/
get length() {
return this.items.length;
},
/**
* shift item from array
* @return {Any} item
*/
shift: function 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: function push(item, prio) {
var items = this.items;
if (typeof prio !== 'number') {
prio = Infinity;
items.push({
prio: prio,
item: item
});
} else {
var found;
prio = Math.abs(prio);
for (var i = 0; i < items.length; i++) {
if (prio < items[i].prio) {
items.splice(i, 0, {
prio: prio,
item: item
});
found = true;
break;
}
}
if (!found) {
items.push({
prio: prio,
item: item
});
}
}
return this;
},
/**
* unshift `item` to Array using priority
* @param {Any} item
*/
unshift: function unshift(item) {
this.items.unshift({
prio: 0,
item: item
});
return this;
},
/**
* removes all items in the Array
*/
reset: function reset() {
this.items = [];
}
};