1
|
|
|
/** global: UB */ |
2
|
|
|
/** global: Buffer */ |
3
|
|
|
|
4
|
|
|
/*! ARRAY UTILS */ |
5
|
|
|
|
6
|
|
|
var arrayFuncs = { |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
isEqual: function(list2){ |
10
|
|
|
var list = this; |
11
|
|
|
if (list == null || list2 == null) { |
|
|
|
|
12
|
|
|
return list == list2; |
13
|
|
|
} |
14
|
|
|
if(list.length != list2.length){ |
15
|
|
|
return false; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
var len = list.length; |
19
|
|
|
|
20
|
|
|
for (var i = 0;i<len;i++){ |
21
|
|
|
if(list[i] !== list2[i]){ |
22
|
|
|
return false; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return true; |
27
|
|
|
}, |
28
|
|
|
isNotEqual: function(list2){ |
29
|
|
|
var list = this; |
30
|
|
|
return !IsEqual(list, list2); |
31
|
|
|
}, |
32
|
|
|
|
33
|
|
|
exists: function(){ |
34
|
|
|
var list = this; |
35
|
|
|
return list != null && list.length > 0; |
|
|
|
|
36
|
|
|
}, |
37
|
|
|
isFirst: function(value){ |
38
|
|
|
var list = this; |
39
|
|
|
return list != null && list[0] == value; |
|
|
|
|
40
|
|
|
}, |
41
|
|
|
isLast: function(value){ |
42
|
|
|
var list = this; |
43
|
|
|
return list != null && list[list.length - 1] == value; |
|
|
|
|
44
|
|
|
}, |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
beginsWithArray: function(value){ |
48
|
|
|
return this.startsWithArray(value); |
49
|
|
|
}, |
50
|
|
|
startsWithArray: function(check){ |
51
|
|
|
var list = this; |
52
|
|
|
|
53
|
|
|
// quickly test if length sufficient |
54
|
|
|
var clen = check.length; |
55
|
|
|
var mlen = list.length; |
56
|
|
|
if (mlen < clen) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// check if first slots match |
61
|
|
|
for (var b = 0;b<clen;b++){ |
62
|
|
|
if (list[b] != check[b]) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return true; |
67
|
|
|
}, |
68
|
|
|
endsWithArray: function(check){ |
69
|
|
|
var list = this; |
70
|
|
|
|
71
|
|
|
// quickly test if length sufficient |
72
|
|
|
var clen = check.length; |
73
|
|
|
var mlen = list.length; |
74
|
|
|
if (mlen < clen) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// check if last slots match |
79
|
|
|
var off = (mlen - clen); |
80
|
|
|
for (var b = 0;b<clen;b++){ |
81
|
|
|
if (list[off + b] != check[b]) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return true; |
86
|
|
|
}, |
87
|
|
|
beginsWith: function(value){ |
88
|
|
|
return this.startsWith(value); |
89
|
|
|
}, |
90
|
|
|
startsWith: function(value){ |
91
|
|
|
var list = this; |
92
|
|
|
return list[0] == value; |
93
|
|
|
}, |
94
|
|
|
endsWith: function(value){ |
95
|
|
|
var list = this; |
96
|
|
|
return list[list.length - 1] == value; |
97
|
|
|
}, |
98
|
|
|
|
99
|
|
View Code Duplication |
next: function(obj, wrap = false){ |
|
|
|
|
100
|
|
|
var list = this; |
101
|
|
|
if (list == null) { |
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
var i = list.indexOf(obj); |
105
|
|
|
if (i > -1) { |
106
|
|
|
return (wrap && i >= (list.length - 1)) ? list[0] : list[i + 1]; |
107
|
|
|
} |
108
|
|
|
return null; |
109
|
|
|
}, |
110
|
|
|
prev: function(obj, wrap = false){ |
111
|
|
|
var list = this; |
112
|
|
|
if (list == null) { |
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
var i = list.indexOf(obj); |
116
|
|
|
if (i > 0) { |
117
|
|
|
return wrap ? list[list.length - 1] : list[i - 1]; |
118
|
|
|
} |
119
|
|
|
return null; |
120
|
|
|
}, |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
allSame: function(){ |
124
|
|
|
var arr = this; |
125
|
|
|
|
126
|
|
|
// checks if all values in the array are same |
127
|
|
|
if (arr.length == 0) { |
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
var last = arr[0]; |
131
|
|
|
for (var a = 1, al = arr.length;a<al;a++){ |
132
|
|
|
if (arr[a] != last) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return true; |
137
|
|
|
}, |
138
|
|
|
|
139
|
|
|
allEqualTo: function(value){ |
140
|
|
|
var arr = this; |
141
|
|
|
|
142
|
|
|
// Checks if all the values in the array are equal to the given value |
143
|
|
|
if (arr.length == 0) { |
|
|
|
|
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
for (var a = 0, al = arr.length;a<al;a++){ |
147
|
|
|
if (arr[a] != value) { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
return true; |
152
|
|
|
}, |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
none:null |
156
|
|
|
}; |
157
|
|
|
|
158
|
|
|
// register funcs |
159
|
|
|
UB.registerFuncs(Array.prototype, arrayFuncs); |
160
|
|
|
|