1
|
|
|
/** global: UB */ |
2
|
|
|
|
3
|
|
|
/*! ARRAY UTILS */ |
4
|
|
|
|
5
|
|
|
var arrayFuncs = { |
6
|
|
|
|
7
|
|
|
indexOf: function(value){ |
8
|
|
|
var list = this; |
9
|
|
|
if (list.length == 0) { |
|
|
|
|
10
|
|
|
return -1; |
11
|
|
|
} |
12
|
|
|
for (var a = 0, al = list.length;a<al;a++){ |
13
|
|
|
if (list[a] == value){ |
14
|
|
|
return a; |
15
|
|
|
} |
16
|
|
|
} |
17
|
|
|
return -1; |
18
|
|
|
}, |
19
|
|
View Code Duplication |
lastIndexOf: function(value){ |
|
|
|
|
20
|
|
|
var list = this; |
21
|
|
|
if (list.length == 0) { |
|
|
|
|
22
|
|
|
return -1; |
23
|
|
|
} |
24
|
|
|
for (var a = list.length-1;a>=0;a--){ |
25
|
|
|
if (list[a] == value){ |
26
|
|
|
return a; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
return -1; |
30
|
|
|
}, |
31
|
|
|
isEqual: function(list2){ |
32
|
|
|
var list = this; |
33
|
|
|
if (list == null || list2 == null) { |
|
|
|
|
34
|
|
|
return list == list2; |
35
|
|
|
} |
36
|
|
|
if(list.length != list2.length){ |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
var len = list.length; |
41
|
|
|
|
42
|
|
|
for (var i = 0;i<len;i++){ |
43
|
|
|
if(list[i] !== list2[i]){ |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
}, |
50
|
|
|
isNotEqual: function(list2){ |
51
|
|
|
var list = this; |
52
|
|
|
return !IsEqual(list, list2); |
53
|
|
|
}, |
54
|
|
|
|
55
|
|
|
exists: function(list){ |
56
|
|
|
var list = this; |
57
|
|
|
return list != null && list.length > 0; |
|
|
|
|
58
|
|
|
}, |
59
|
|
View Code Duplication |
count: function(value, not = false){ |
|
|
|
|
60
|
|
|
var list = this; |
61
|
|
|
var total = 0; |
62
|
|
|
for (var a = 0, al = list.length;a<al;a++){ |
63
|
|
|
if (not) { |
64
|
|
|
if (list[a] != value) { |
65
|
|
|
total++; |
66
|
|
|
} |
67
|
|
|
}else { |
68
|
|
|
if (list[a] == value) { |
69
|
|
|
total++; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
return total; |
74
|
|
|
}, |
75
|
|
|
or: function(list2){ |
76
|
|
|
var list = this; |
77
|
|
|
if (Exists(list)) { |
78
|
|
|
return list; |
79
|
|
|
} |
80
|
|
|
return list2; |
81
|
|
|
}, |
82
|
|
|
|
83
|
|
|
part: function(start, end){ |
84
|
|
|
var list = this; |
85
|
|
|
|
86
|
|
|
// quickly exit if no items or no results possible |
87
|
|
|
if (list == null || list.length == 0 || start > end || start >= list.length) { |
|
|
|
|
88
|
|
|
return []; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// just get the part we need |
92
|
|
|
return list.slice(start, end + 1); |
93
|
|
|
}, |
94
|
|
|
|
95
|
|
|
swap: function(slot1, slot2){ |
96
|
|
|
var list = this; |
97
|
|
|
var temp = list[slot2]; |
98
|
|
|
list[slot2] = list[slot1]; |
99
|
|
|
list[slot1] = temp; |
100
|
|
|
}, |
101
|
|
|
|
102
|
|
|
add: function(item){ |
103
|
|
|
var list = this; |
104
|
|
|
list.push(item); |
105
|
|
|
return list.length - 1; |
106
|
|
|
}, |
107
|
|
|
addToStart: function(item){ |
108
|
|
|
var list = this; |
109
|
|
|
list.unshift(item); |
110
|
|
|
return 0; |
111
|
|
|
}, |
112
|
|
|
/** return false if item exists, true if item does not */ |
113
|
|
|
addOnce: function(value){ |
114
|
|
|
var list = this; |
115
|
|
|
|
116
|
|
|
// return false if item exists |
117
|
|
|
if (list.indexOf(value) > -1) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// add if not found |
122
|
|
|
list.push(value); |
123
|
|
|
return true; |
124
|
|
|
}, |
125
|
|
|
|
126
|
|
|
addArray: function(toAdd, addAtSlot = -1, modifyMain = true){ |
127
|
|
|
var list = this; |
128
|
|
|
|
129
|
|
|
if (toAdd != null && toAdd.length > 0) { |
|
|
|
|
130
|
|
|
if (!modifyMain) { |
131
|
|
|
list = list.concat(); /// shallow clone |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// set length first so allocates memory (maybe?) |
135
|
|
|
var origLen = list.length; |
136
|
|
|
if (addAtSlot == -1){ |
137
|
|
|
list.length += toAdd.length; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// set all slots |
141
|
|
|
var next = addAtSlot == -1 ? origLen : addAtSlot; |
142
|
|
|
for (var a = 0, al = toAdd.length;a<al;a++, next++){ |
143
|
|
|
list[next] = toAdd[a]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
return list; |
147
|
|
|
}, |
148
|
|
|
|
149
|
|
|
/** adds the given value many times */ |
150
|
|
|
addManyTimes: function(val, times){ |
151
|
|
|
var list = this; |
152
|
|
|
|
153
|
|
|
if (times <= 0) { |
154
|
|
|
return list; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
var n = list.length; |
158
|
|
|
for (var t = 0;t<times;t++){ |
159
|
|
|
list[n++] = val; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return list; |
163
|
|
|
}, |
164
|
|
|
|
165
|
|
|
addRange: function(toAdd, startSlot, endSlot){ |
166
|
|
|
var list = this; |
167
|
|
|
|
168
|
|
|
// exit if no work |
169
|
|
|
if (endSlot < startSlot) { |
170
|
|
|
return; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// per wanted slot of the `add` array |
174
|
|
|
startSlot = N.LimitToArray(startSlot, toAdd); |
|
|
|
|
175
|
|
|
endSlot = N.LimitToArray(endSlot, toAdd); |
176
|
|
|
for (var s = startSlot;s <= endSlot;s++){ |
177
|
|
|
|
178
|
|
|
// add into `main` array |
179
|
|
|
list.push(toAdd[s]); |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
}, |
183
|
|
|
|
184
|
|
|
/** returns final index of added item, or index of already existing item */ |
185
|
|
|
findOrAdd: function(value){ |
186
|
|
|
var list = this; |
187
|
|
|
|
188
|
|
|
// return index of item if exists |
189
|
|
|
var i = list.indexOf(value); |
190
|
|
|
if (i > -1) { |
191
|
|
|
return i; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// add if not found |
195
|
|
|
i = list.length; |
196
|
|
|
list[i] = value; |
197
|
|
|
return i; |
198
|
|
|
}, |
199
|
|
|
|
200
|
|
|
insertOne: function(item, slot){ |
201
|
|
|
var list = this; |
202
|
|
|
|
203
|
|
|
// adds one slot at the given point |
204
|
|
|
// modifies the main array |
205
|
|
|
|
206
|
|
|
list.splice(slot, 0, item); |
207
|
|
|
}, |
208
|
|
|
|
209
|
|
|
/** if slot = -1 or outside the array, the item is added to the END of the array. |
210
|
|
|
* Otherwise the item is added at the given slot. */ |
211
|
|
|
insertOneOrAdd: function(item, slot){ |
212
|
|
|
var list = this; |
213
|
|
|
|
214
|
|
|
// adds one slot at the given point |
215
|
|
|
// modifies the main array |
216
|
|
|
|
217
|
|
|
if (slot < 0 || slot >= list.length) { |
218
|
|
|
list.push(item); |
219
|
|
|
return list.length - 1; |
220
|
|
|
}else { |
|
|
|
|
221
|
|
|
list.splice(slot, 0, item); |
222
|
|
|
return slot; |
223
|
|
|
} |
224
|
|
|
}, |
225
|
|
|
insertArray: function(newItems, slot, returnNew = true){ |
226
|
|
|
var list = this; |
227
|
|
|
|
228
|
|
|
// adds many slots at the given point |
229
|
|
|
// returns a new array |
230
|
|
|
if(returnNew){ |
231
|
|
|
return list.slice(0, slot).concat(newItems).concat(list.slice(slot)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// adds many slots at the given point |
235
|
|
|
// modifies the main array |
236
|
|
|
for (var a = 0, al = newItems.length;a<al;a++){ |
237
|
|
|
list.splice(slot++, 0, newItems[a]); |
238
|
|
|
} |
239
|
|
|
return list; |
240
|
|
|
}, |
241
|
|
|
insertArrayAfter: function(newItems, after){ |
242
|
|
|
var list = this; |
243
|
|
|
if (IsLast(list, after)) { |
244
|
|
|
Add(list, newItems); |
245
|
|
|
} else { |
246
|
|
|
var i = list.indexOf(after); |
247
|
|
|
InsertArray(list, i + 1, newItems); |
248
|
|
|
} |
249
|
|
|
}, |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
removeAndInsert: function(from, to){ |
253
|
|
|
var list = this; |
254
|
|
|
|
255
|
|
|
// ensure slots within array |
256
|
|
|
var al = list.length; |
257
|
|
|
from = N.LimitTo(from, 0, al - 1); |
|
|
|
|
258
|
|
|
to = N.LimitTo(to, 0, al - 1); |
259
|
|
|
if (to < from) { |
260
|
|
|
var i1 = to; |
261
|
|
|
var i2 = from; |
262
|
|
|
}else { |
263
|
|
|
var i1 = from; |
|
|
|
|
264
|
|
|
var i2 = to; |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// fill unaffected header |
268
|
|
|
var result = []; |
269
|
|
|
if(i1 > 0){ |
270
|
|
|
for (var a = 0;a<i1;a++){ |
271
|
|
|
result[a] = list[a]; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
// fill "to" |
276
|
|
|
result[to] = list[from]; |
277
|
|
|
|
278
|
|
|
// fill between from and to |
279
|
|
|
if(to < from){ |
280
|
|
|
for (a = to + 1; a <= from; a++) { |
281
|
|
|
result[a] = list[a - 1]; |
282
|
|
|
} |
283
|
|
|
}else { |
284
|
|
|
for (a = from; a < to; a++) { |
285
|
|
|
result[a] = list[a + 1]; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
// fill unaffected footer |
290
|
|
|
for (var a = i2 + 1;a<al;a++){ |
|
|
|
|
291
|
|
|
result[a] = list[a]; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return result; |
295
|
|
|
}, |
296
|
|
|
|
297
|
|
|
/** replace a range of items with a given array */ |
298
|
|
|
replaceRange: function(replaceStartSlot, replaceEndSlot, newItems, returnNew = true){ |
299
|
|
|
var list = this; |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
// simply remove a single item |
303
|
|
|
if (replaceStartSlot == replaceEndSlot && newItems.length == 0) { |
|
|
|
|
304
|
|
|
if (returnNew) { |
305
|
|
|
list = list.concat(); |
306
|
|
|
} |
307
|
|
|
list.splice(replaceStartSlot, 1); |
308
|
|
|
return list; |
309
|
|
|
|
310
|
|
|
// simply replace a single item |
311
|
|
|
}else if (replaceStartSlot == replaceEndSlot && newItems.length == 1) { |
|
|
|
|
312
|
|
|
if (returnNew) { |
313
|
|
|
list = list.concat(); |
314
|
|
|
} |
315
|
|
|
list[replaceStartSlot] = newItems[0]; |
316
|
|
|
return list; |
317
|
|
|
|
318
|
|
|
}else{ |
319
|
|
|
|
320
|
|
|
// alt method if returning a new array |
321
|
|
|
if(returnNew){ |
322
|
|
|
return list.slice(0, replaceStartSlot).concat(newItems).concat(list.slice(replaceEndSlot + 1)); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
// remove many |
326
|
|
|
list.splice(replaceStartSlot, (replaceEndSlot - replaceStartSlot) + 1); |
327
|
|
|
|
328
|
|
|
// insert many |
329
|
|
|
var slot = replaceStartSlot; |
330
|
|
|
for (var a = 0, al = newItems.length;a<al;a++){ |
331
|
|
|
list.splice(slot++, 0, newItems[a]); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
return list; |
335
|
|
|
}, |
336
|
|
|
|
337
|
|
|
replace: function(find, replace, stringReplace = false){ |
338
|
|
|
var list = this; |
339
|
|
|
for (var i = 0, il = list.length;i<il;i++){ |
340
|
|
|
if (stringReplace){ |
341
|
|
|
|
342
|
|
|
// replace substring within string items |
343
|
|
|
var str = list[i]; |
344
|
|
|
if (str && str.constructor === String) { |
345
|
|
|
list[i] = str.replaceAll(find, replace); |
346
|
|
|
} |
347
|
|
|
}else{ |
348
|
|
|
|
349
|
|
|
// replace entire items |
350
|
|
|
if (list[i] == find) { |
351
|
|
|
list[i] = replace; |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
}, |
356
|
|
|
replaceOnce: function(find, replace){ |
357
|
|
|
var list = this; |
358
|
|
|
for (var i = 0, il = list.length;i<il;i++){ |
359
|
|
|
if (list[i] == find) { |
360
|
|
|
list[i] = replace; |
361
|
|
|
return; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
}, |
365
|
|
|
replaceMany: function(findArray, replaceArray){ |
366
|
|
|
var list = this; |
367
|
|
|
if (findArray.length != replaceArray.length){ |
368
|
|
|
return; |
369
|
|
|
} |
370
|
|
|
for (var i = 0, il = list.length;i<il;i++){ |
371
|
|
|
for (var f = 0, fl = findArray.length;f<fl;f++){ |
372
|
|
|
var find = findArray[f]; |
373
|
|
|
if (list[i] == find) { |
374
|
|
|
list[i] = replaceArray[f]; |
375
|
|
|
break; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
}, |
380
|
|
|
|
381
|
|
|
|
382
|
|
|
remove: function(value, stringReplace = false){ |
383
|
|
|
var list = this; |
384
|
|
|
for (var i = 0, il = list.length;i<il;i++){ |
385
|
|
|
if (stringReplace){ |
386
|
|
|
|
387
|
|
|
// remove substring within string items |
388
|
|
|
var str = list[i]; |
389
|
|
|
if (str && str.constructor === String) { |
390
|
|
|
list[i] = str.removeAll(find); |
|
|
|
|
391
|
|
|
} |
392
|
|
|
}else{ |
393
|
|
|
|
394
|
|
|
// remove entire items |
395
|
|
|
if (list[i] == value) { |
396
|
|
|
list.splice(i, 1); |
397
|
|
|
i--; |
|
|
|
|
398
|
|
|
il--; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
}, |
403
|
|
|
removeOnce: function(val){ |
404
|
|
|
var list = this; |
405
|
|
|
var i = list.indexOf(val); |
406
|
|
|
if (i > -1) { |
407
|
|
|
list.splice(i, 1); |
408
|
|
|
} |
409
|
|
|
return i; |
410
|
|
|
}, |
411
|
|
|
removeFirst: function(returnNew = false){ |
412
|
|
|
var list = this; |
413
|
|
|
if (returnNew) { |
414
|
|
|
return list.slice(1); |
415
|
|
|
}else{ |
|
|
|
|
416
|
|
|
if (list.length > 0) { |
417
|
|
|
list.splice(0, 1); |
418
|
|
|
} |
419
|
|
|
return list; |
420
|
|
|
} |
421
|
|
|
}, |
422
|
|
|
removeFirstX: function(count, returnNew = false){ |
423
|
|
|
var list = this; |
424
|
|
|
|
425
|
|
|
// if fewer items than wanted, clear entire array |
426
|
|
|
if (list.length < count) { |
427
|
|
|
if (returnNew) { |
428
|
|
|
return []; |
429
|
|
|
} |
430
|
|
|
list.length = 0; |
431
|
|
|
return list; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
// delete X items from start |
435
|
|
|
if (returnNew) { |
436
|
|
|
return list.slice(count); |
437
|
|
|
}else{ |
|
|
|
|
438
|
|
|
list.splice(0, count); |
439
|
|
|
return list; |
440
|
|
|
} |
441
|
|
|
}, |
442
|
|
|
removeLast: function(returnNew = false){ |
443
|
|
|
var list = this; |
444
|
|
|
if (returnNew) { |
445
|
|
|
return list.slice(0, list.length - 1); |
446
|
|
|
} |
447
|
|
|
if (list.length > 0) { |
448
|
|
|
list.splice( - 1, 1); |
449
|
|
|
} |
450
|
|
|
return list; |
451
|
|
|
}, |
452
|
|
|
removeLastX: function(count, returnNew = false){ |
453
|
|
|
var list = this; |
454
|
|
|
|
455
|
|
|
// if fewer items than wanted, clear entire array |
456
|
|
|
if (list.length < count) { |
457
|
|
|
if (returnNew) { |
458
|
|
|
return []; |
459
|
|
|
} |
460
|
|
|
list.length = 0; |
461
|
|
|
return list; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
// delete X items from end |
465
|
|
|
if (returnNew) { |
466
|
|
|
return list.slice(0, list.length - count); |
467
|
|
|
}else{ |
|
|
|
|
468
|
|
|
list.splice( -count, count); |
469
|
|
|
return list; |
470
|
|
|
} |
471
|
|
|
}, |
472
|
|
|
|
473
|
|
|
removeEdges: function(fromLeftEdge, fromRightEdge){ |
474
|
|
|
var list = this; |
475
|
|
|
if ((list.length - fromLeftEdge - fromRightEdge) <= 0) { |
476
|
|
|
return []; |
477
|
|
|
} |
478
|
|
|
return list.slice(fromLeftEdge, list.length - fromRightEdge); |
479
|
|
|
}, |
480
|
|
|
|
481
|
|
|
contains: function(value){ |
482
|
|
|
var list = this; |
483
|
|
|
if (list.length == 1) { |
|
|
|
|
484
|
|
|
return list[0] == value; |
485
|
|
|
} |
486
|
|
|
return list.indexOf(value) > -1; |
487
|
|
|
}, |
488
|
|
|
containsAny: function(values){ |
489
|
|
|
var list = this; |
490
|
|
|
return IndexOfAny(list, values) > -1; |
491
|
|
|
}, |
492
|
|
|
containsAll: function(values){ |
493
|
|
|
var list = this; |
494
|
|
|
|
495
|
|
|
// exit if either null |
496
|
|
|
if (list == null || values == null || list.length == 0 || values.length == 0) { |
|
|
|
|
497
|
|
|
return false; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
// check if all found |
501
|
|
|
for (var f = 0, fl = values.length;f<fl;f++){ |
502
|
|
|
if (list.indexOf(values[f]) == -1) { |
503
|
|
|
return false; |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
return true; |
507
|
|
|
}, |
508
|
|
|
|
509
|
|
|
|
510
|
|
|
splitAt: function(slot, includeSlot = false, includeInFirst = false){ |
511
|
|
|
var list = this; |
512
|
|
|
if (includeSlot) { |
513
|
|
|
if (includeInFirst) { |
514
|
|
|
return [list.slice(0, slot + 1), list.slice(slot + 1)]; |
515
|
|
|
}else { |
|
|
|
|
516
|
|
|
return [list.slice(0, slot), list.slice(slot)]; |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
return [list.slice(0, slot), list.slice(slot+1)]; |
520
|
|
|
}, |
521
|
|
|
splitAtEvery: function(val){ |
522
|
|
|
var list = this; |
523
|
|
|
var splits = []; |
524
|
|
|
var lastSlot = 0; |
525
|
|
|
for (var a = 0, al = list.length;a<al;a++){ |
526
|
|
|
if (list[a] == val && a > lastSlot) { |
527
|
|
|
splits.push(list.slice(lastSlot, a)); |
528
|
|
|
lastSlot = a + 1; |
529
|
|
|
} |
530
|
|
|
} |
531
|
|
|
return splits; |
532
|
|
|
}, |
533
|
|
|
|
534
|
|
|
moveToTop: function(slot, returnNew = false){ |
535
|
|
|
var list = this; |
536
|
|
|
|
537
|
|
|
if (returnNew){ |
538
|
|
|
|
539
|
|
|
// ALWAYS RETURNS NEW ARRAY |
540
|
|
|
|
541
|
|
|
// exit quickly if slot not in array |
542
|
|
|
var al = list.length; |
543
|
|
|
if (slot < 0 || slot >= al) { |
544
|
|
|
return list.concat(); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
// create new array with slot on top |
548
|
|
|
var newArr = [list[slot]]; |
549
|
|
|
var n = 1; |
550
|
|
|
|
551
|
|
|
// add all other slots |
552
|
|
|
for (var a = 0;a<al;a++){ |
553
|
|
|
if (a != slot) { |
554
|
|
|
newArr[n++] = list[a]; |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
return newArr; |
559
|
|
|
|
560
|
|
|
}else{ |
|
|
|
|
561
|
|
|
|
562
|
|
|
// MODIFIES ARRAY IN PLACE |
563
|
|
|
|
564
|
|
|
// exit quickly if slot not in array |
565
|
|
|
var al = list.length; |
|
|
|
|
566
|
|
|
if (slot < 0 || slot >= al) { |
567
|
|
|
return; |
|
|
|
|
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
// delete and re-add slot |
571
|
|
|
var val = list[slot]; |
572
|
|
|
list.splice(slot, 1); |
573
|
|
|
list.unshift(val); |
|
|
|
|
574
|
|
|
} |
575
|
|
|
}, |
576
|
|
|
moveToBottom: function(slot, returnNew = false){ |
577
|
|
|
var list = this; |
578
|
|
|
|
579
|
|
|
if (returnNew){ |
580
|
|
|
|
581
|
|
|
// ALWAYS RETURNS NEW ARRAY |
582
|
|
|
|
583
|
|
|
// exit quickly if slot not in array |
584
|
|
|
var al = list.length; |
585
|
|
|
if (slot < 0 || slot >= al) { |
586
|
|
|
return list.concat(); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
// create new array with slot on top |
590
|
|
|
var newArr = []; |
591
|
|
|
var n = 0; |
592
|
|
|
|
593
|
|
|
// add all other slots |
594
|
|
|
for (var a = 0;a<al;a++){ |
595
|
|
|
if (a != slot) { |
596
|
|
|
newArr[n++] = list[a]; |
597
|
|
|
} |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
// add slot to bottom |
601
|
|
|
newArr[n++] = list[slot]; |
|
|
|
|
602
|
|
|
return newArr; |
603
|
|
|
|
604
|
|
|
}else{ |
|
|
|
|
605
|
|
|
|
606
|
|
|
// MODIFIES ARRAY IN PLACE |
607
|
|
|
|
608
|
|
|
// exit quickly if slot not in array |
609
|
|
|
var al = list.length; |
|
|
|
|
610
|
|
|
if (slot < 0 || slot >= al) { |
611
|
|
|
return; |
|
|
|
|
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
// delete and re-add slot |
615
|
|
|
var val = list[slot]; |
616
|
|
|
list.splice(slot, 1); |
617
|
|
|
list.push(val); |
|
|
|
|
618
|
|
|
} |
619
|
|
|
}, |
620
|
|
|
moveUp: function(item){ |
621
|
|
|
var list = this; |
622
|
|
|
|
623
|
|
|
// MODIFIES ARRAY IN PLACE |
624
|
|
|
|
625
|
|
|
// exit quickly if slot not in array, or already on top |
626
|
|
|
var al = list.length; |
|
|
|
|
627
|
|
|
var slot = list.indexOf(item); |
628
|
|
|
if (slot <= 0) { |
629
|
|
|
return false; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
// move one up |
633
|
|
|
Swap(list, slot, slot - 1); |
634
|
|
|
return true; |
635
|
|
|
}, |
636
|
|
|
moveDown: function(item){ |
637
|
|
|
var list = this; |
638
|
|
|
|
639
|
|
|
// MODIFIES ARRAY IN PLACE |
640
|
|
|
|
641
|
|
|
// exit quickly if slot not in array, or already at bottom |
642
|
|
|
var al = list.length; |
643
|
|
|
var slot = list.indexOf(item); |
644
|
|
|
if (slot < 0 || slot >= (al - 1)) { |
645
|
|
|
return false; |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
// move one down |
649
|
|
|
Swap(list, slot, slot + 1); |
650
|
|
|
return true; |
651
|
|
|
}, |
652
|
|
|
moveSlotUp: function(slot){ |
653
|
|
|
var list = this; |
654
|
|
|
|
655
|
|
|
// MODIFIES ARRAY IN PLACE |
656
|
|
|
|
657
|
|
|
// exit quickly if slot not in array, or already on top |
658
|
|
|
var al = list.length; |
659
|
|
|
if (slot <= 0 || slot >= al) { |
660
|
|
|
return false; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
// move one up |
664
|
|
|
Swap(list, slot, slot - 1); |
665
|
|
|
return true; |
666
|
|
|
}, |
667
|
|
|
moveSlotDown: function(slot){ |
668
|
|
|
var list = this; |
669
|
|
|
|
670
|
|
|
// MODIFIES ARRAY IN PLACE |
671
|
|
|
|
672
|
|
|
// exit quickly if slot not in array, or already at bottom |
673
|
|
|
var al = list.length; |
674
|
|
|
if (slot < 0 || slot >= (al - 1)) { |
675
|
|
|
return false; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
// move one down |
679
|
|
|
Swap(list, slot, slot + 1); |
680
|
|
|
return true; |
681
|
|
|
}, |
682
|
|
|
/** Move an item from the given list, to the start/end of the target list */ |
683
|
|
|
moveToArray: function(item, toList, evenIfExists = false, addToEnd = true){ |
684
|
|
|
var list = this; |
685
|
|
|
|
686
|
|
|
// remove from source list |
687
|
|
|
RemoveOne(list, item); |
688
|
|
|
|
689
|
|
|
// add to target list |
690
|
|
|
if (evenIfExists || toList.indexOf(item) == -1) { |
691
|
|
|
if (addToEnd) { |
692
|
|
|
toList.push(item); |
693
|
|
|
}else{ |
694
|
|
|
toList.unshift(item); |
695
|
|
|
} |
696
|
|
|
return true; |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
return false; |
700
|
|
|
}, |
701
|
|
|
beginsWithArray: function(value){ |
702
|
|
|
return this.startsWithArray(value); |
703
|
|
|
}, |
704
|
|
|
startsWithArray: function(check){ |
705
|
|
|
var list = this; |
706
|
|
|
|
707
|
|
|
// quickly test if length sufficient |
708
|
|
|
var clen = check.length; |
709
|
|
|
var mlen = list.length; |
710
|
|
|
if (mlen < clen) { |
711
|
|
|
return false; |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
// check if first slots match |
715
|
|
|
for (var b = 0;b<clen;b++){ |
716
|
|
|
if (list[b] != check[b]) { |
717
|
|
|
return false; |
718
|
|
|
} |
719
|
|
|
} |
720
|
|
|
return true; |
721
|
|
|
}, |
722
|
|
|
endsWithArray: function(check){ |
723
|
|
|
var list = this; |
724
|
|
|
|
725
|
|
|
// quickly test if length sufficient |
726
|
|
|
var clen = check.length; |
727
|
|
|
var mlen = list.length; |
728
|
|
|
if (mlen < clen) { |
729
|
|
|
return false; |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
// check if last slots match |
733
|
|
|
var off = (mlen - clen); |
734
|
|
|
for (var b = 0;b<clen;b++){ |
735
|
|
|
if (list[off + b] != check[b]) { |
736
|
|
|
return false; |
737
|
|
|
} |
738
|
|
|
} |
739
|
|
|
return true; |
740
|
|
|
}, |
741
|
|
|
indexOfArray: function(containsArr){ |
742
|
|
|
var list = this; |
743
|
|
|
|
744
|
|
|
// returns index of containing list in main array |
745
|
|
|
|
746
|
|
|
if (list.length < containsArr.length) { |
747
|
|
|
return -1; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
var cl = containsArr.length; |
751
|
|
|
for (var a = 0, al = list.length - (cl - 1);a<al;a++){ |
752
|
|
|
|
753
|
|
|
var allMatch = true; |
754
|
|
|
for (var c = 0;c<cl;c++){ |
755
|
|
|
if (list[a+c] != containsArr[c]) { |
756
|
|
|
allMatch = false; |
757
|
|
|
break; |
758
|
|
|
} |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
if (allMatch) { |
762
|
|
|
return a; |
763
|
|
|
} |
764
|
|
|
} |
765
|
|
|
return -1; |
766
|
|
|
}, |
767
|
|
|
|
768
|
|
|
beginsWith: function(value){ |
769
|
|
|
return this.startsWith(value); |
770
|
|
|
}, |
771
|
|
|
startsWith: function(value){ |
772
|
|
|
var list = this; |
773
|
|
|
return list[0] == value; |
774
|
|
|
}, |
775
|
|
|
endsWith: function(value){ |
776
|
|
|
var list = this; |
777
|
|
|
return list[list.length - 1] == value; |
778
|
|
|
}, |
779
|
|
|
|
780
|
|
View Code Duplication |
next: function(obj, wrap = false){ |
|
|
|
|
781
|
|
|
var list = this; |
782
|
|
|
if (list == null) { |
|
|
|
|
783
|
|
|
return null; |
784
|
|
|
} |
785
|
|
|
var i = list.indexOf(obj); |
786
|
|
|
if (i > -1) { |
787
|
|
|
return (wrap && i >= (list.length - 1)) ? list[0] : list[i + 1]; |
788
|
|
|
} |
789
|
|
|
return null; |
790
|
|
|
}, |
791
|
|
View Code Duplication |
prev: function(obj, wrap = false){ |
|
|
|
|
792
|
|
|
var list = this; |
793
|
|
|
if (list == null) { |
|
|
|
|
794
|
|
|
return null; |
795
|
|
|
} |
796
|
|
|
var i = list.indexOf(obj); |
797
|
|
|
if (i > 0) { |
798
|
|
|
return (wrap && i <= 0) ? list[list.length - 1] : list[i - 1]; |
799
|
|
|
} |
800
|
|
|
return null; |
801
|
|
|
}, |
802
|
|
|
|
803
|
|
|
|
804
|
|
|
/** Returns the nearest existing slot value in the array. Returns `ifNoSlots` if the array is empty. */ |
805
|
|
|
within: function(slot, ifNoSlots = null){ |
806
|
|
|
var list = this; |
807
|
|
|
|
808
|
|
|
// return null if array empty |
809
|
|
|
var len = list.length; |
810
|
|
|
if (len == 0) { |
|
|
|
|
811
|
|
|
return ifNoSlots; |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
// return first slot if index negative |
815
|
|
|
if (slot < 0) { |
816
|
|
|
return list[0]; |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
// return last slot if index more than last slot |
820
|
|
|
if (slot >= len) { |
821
|
|
|
return list[len - 1]; |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
// return given slot if within array |
825
|
|
|
return list[slot]; |
826
|
|
|
}, |
827
|
|
|
|
828
|
|
|
first: function(list){ |
829
|
|
|
var list = this; |
830
|
|
|
var len = list.length; |
831
|
|
|
if (len == 0) { |
|
|
|
|
832
|
|
|
return null; |
833
|
|
|
} |
834
|
|
|
return list[0]; |
835
|
|
|
}, |
836
|
|
|
firstExisting: function(slots, blankVal = null){ |
837
|
|
|
var list = this; |
838
|
|
|
for (var s = 0, sl = slots.length;s<sl;s++){ |
839
|
|
|
var val; |
840
|
|
|
if ((val = list[slots[s]]) != blankVal) { |
841
|
|
|
return val; |
842
|
|
|
} |
843
|
|
|
} |
844
|
|
|
return null; |
845
|
|
|
}, |
846
|
|
|
last: function(list){ |
847
|
|
|
var list = this; |
848
|
|
|
var len = list.length; |
849
|
|
|
if (len == 0) { |
|
|
|
|
850
|
|
|
return null; |
851
|
|
|
} |
852
|
|
|
return list[len-1]; |
853
|
|
|
}, |
854
|
|
|
lastX: function(count){ |
855
|
|
|
var list = this; |
856
|
|
|
var s = N.I_Max(0, list.length - count); |
|
|
|
|
857
|
|
|
return list.slice(s, list.length); |
858
|
|
|
}, |
859
|
|
|
setFirst: function(value){ |
860
|
|
|
var list = this; |
861
|
|
|
var len = list.length; |
862
|
|
|
if (len == 0) { |
|
|
|
|
863
|
|
|
return; |
864
|
|
|
} |
865
|
|
|
list[0] = value; |
866
|
|
|
}, |
867
|
|
|
setLast: function(value){ |
868
|
|
|
var list = this; |
869
|
|
|
var len = list.length; |
870
|
|
|
if (len == 0) { |
|
|
|
|
871
|
|
|
return; |
872
|
|
|
} |
873
|
|
|
list[len-1] = value; |
874
|
|
|
}, |
875
|
|
|
|
876
|
|
|
random: function(list){ |
877
|
|
|
var list = this; |
878
|
|
|
|
879
|
|
|
// return null if array empty |
880
|
|
|
if (list.length == 0) { |
|
|
|
|
881
|
|
|
return null; |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
// return random slot within array |
885
|
|
|
return list[parseInt(Math.random() * 1000000) % list.length]; |
886
|
|
|
}, |
887
|
|
|
|
888
|
|
|
pick: function(IDs, sameSlots = false, fastAndUnsafe = false, out = null, evenIfNull = false){ |
889
|
|
|
var list = this; |
890
|
|
|
if (out == null){ |
|
|
|
|
891
|
|
|
out = []; |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
if (IDs){ |
895
|
|
|
if (fastAndUnsafe) { |
896
|
|
|
|
897
|
|
|
for (var i = 0, il = IDs.length;i<il;i++){ |
898
|
|
|
var id = IDs[i]; |
899
|
|
|
var val = list[id]; |
900
|
|
|
if (sameSlots) { |
901
|
|
|
out[id] = val; |
902
|
|
|
}else{ |
903
|
|
|
out.push(val); |
904
|
|
|
} |
905
|
|
|
} |
906
|
|
|
|
907
|
|
|
}else{ |
908
|
|
|
|
909
|
|
|
for (var i = 0, il = IDs.length;i<il;i++){ |
|
|
|
|
910
|
|
|
var id = IDs[i]; |
|
|
|
|
911
|
|
|
if (id > -1){ |
912
|
|
|
var val = list[id]; |
|
|
|
|
913
|
|
|
if (val != null || evenIfNull) { |
|
|
|
|
914
|
|
|
if (sameSlots) { |
915
|
|
|
out[id] = val; |
916
|
|
|
}else{ |
917
|
|
|
out.push(val); |
918
|
|
|
} |
919
|
|
|
} |
920
|
|
|
} |
921
|
|
|
} |
922
|
|
|
|
923
|
|
|
} |
924
|
|
|
} |
925
|
|
|
|
926
|
|
|
return out; |
927
|
|
|
}, |
928
|
|
|
|
929
|
|
|
/** Get the data of `list`, by searching `indexID` in `indexArr`, or return `defaultVal` if not found */ |
930
|
|
|
getByMatchingArray: function(indexArr, indexID, defaultVal = null){ |
931
|
|
|
var list = this; |
932
|
|
|
var slot = indexArr.indexOf(indexID); |
933
|
|
|
return slot == -1 ? defaultVal : list[slot]; |
934
|
|
|
}, |
935
|
|
|
/** Set the data of `list`, by searching `indexID` in `indexArr` */ |
936
|
|
|
setByMatchingArray: function(indexArr, indexID, data){ |
937
|
|
|
var list = this; |
938
|
|
|
var slot = indexArr.indexOf(indexID); |
939
|
|
|
if (slot == -1) { |
940
|
|
|
return false; |
941
|
|
|
} |
942
|
|
|
list[slot] = data; |
943
|
|
|
return true; |
944
|
|
|
}, |
945
|
|
|
|
946
|
|
|
page: function(page, pageLength, invisibleRows = null){ |
947
|
|
|
var list = this; |
948
|
|
|
|
949
|
|
|
// ensure page no. in limits |
950
|
|
|
var lastPage = Math.ceil(list.length / pageLength); |
951
|
|
|
page = N.LimitTo(page, 0, lastPage - 1); |
|
|
|
|
952
|
|
|
|
953
|
|
|
// get first/last row in page |
954
|
|
|
var pageStart = page*pageLength;/// 0-based - first row in page |
955
|
|
|
var pageEnd = (pageStart + pageLength) - 1;/// 0-based - last row in page |
956
|
|
|
|
957
|
|
|
// get on-page rows |
958
|
|
|
var visibleRows = GetMany(list, pageStart, pageLength); |
959
|
|
|
|
960
|
|
|
// get off-page rows |
961
|
|
|
if (invisibleRows) { |
962
|
|
|
invisibleRows.addRange(list, 0, pageStart - 1); |
963
|
|
|
invisibleRows.addRange(list, pageEnd + 1, list.length - 1); |
964
|
|
|
} |
965
|
|
|
|
966
|
|
|
return visibleRows; |
967
|
|
|
}, |
968
|
|
|
|
969
|
|
|
trim: function(returnNew = false, trimVal = null){ |
970
|
|
|
var list = this; |
971
|
|
|
var first = IndexOf(list, trimVal, true); |
972
|
|
|
if (first == -1) { |
973
|
|
|
if (returnNew) { |
974
|
|
|
return []; |
975
|
|
|
}else { |
|
|
|
|
976
|
|
|
list.length = 0; |
977
|
|
|
} |
978
|
|
|
}else{ |
979
|
|
|
var last = LastIndexOf(list, trimVal, true); |
980
|
|
|
if (returnNew) { |
981
|
|
|
return GetManySE(list, first, last); |
982
|
|
|
}else { |
|
|
|
|
983
|
|
|
|
984
|
|
|
if (first != -1 && first != 0){ |
|
|
|
|
985
|
|
|
DeleteManySE(list, 0, first-1); |
986
|
|
|
} |
987
|
|
|
if (last != -1){ |
988
|
|
|
list.length = last - first + 1; |
989
|
|
|
} |
990
|
|
|
} |
991
|
|
|
} |
992
|
|
|
return list; |
993
|
|
|
}, |
994
|
|
|
|
995
|
|
|
trimLeft: function(returnNew = false, trimVal = null){ |
996
|
|
|
var list = this; |
997
|
|
|
var first = IndexOf(list, trimVal, true); |
998
|
|
|
if (first == -1) { |
999
|
|
|
if (returnNew) { |
1000
|
|
|
return []; |
1001
|
|
|
}else { |
|
|
|
|
1002
|
|
|
list.length = 0; |
1003
|
|
|
} |
1004
|
|
|
}else{ |
1005
|
|
|
if (returnNew) { |
1006
|
|
|
return GetAfter(list, first); |
1007
|
|
|
}else { |
|
|
|
|
1008
|
|
|
if (first != -1 && first != 0){ |
|
|
|
|
1009
|
|
|
DeleteManySE(list, 0, first-1); |
1010
|
|
|
} |
1011
|
|
|
} |
1012
|
|
|
} |
1013
|
|
|
return list; |
1014
|
|
|
}, |
1015
|
|
|
trimRight: function(returnNew = false, trimVal = null){ |
1016
|
|
|
var list = this; |
1017
|
|
|
var last = IndexOf(list, trimVal, true); |
1018
|
|
|
if (last == -1) { |
1019
|
|
|
if (returnNew) { |
1020
|
|
|
return []; |
1021
|
|
|
}else { |
|
|
|
|
1022
|
|
|
list.length = 0; |
1023
|
|
|
} |
1024
|
|
|
}else{ |
1025
|
|
|
if (returnNew) { |
1026
|
|
|
return GetBefore(list, last); |
1027
|
|
|
}else { |
|
|
|
|
1028
|
|
|
list.length = last + 1; |
1029
|
|
|
} |
1030
|
|
|
} |
1031
|
|
|
return list; |
1032
|
|
|
}, |
1033
|
|
|
|
1034
|
|
|
transpose: function() { |
1035
|
|
|
var arr = this; |
1036
|
|
|
var transposed = []; |
1037
|
|
|
|
1038
|
|
|
for (var r = 0; r < arr.length; r++) { |
1039
|
|
|
for (var c = 0; c < arr[r].length; c++) { |
1040
|
|
|
if (transposed[c] == null) { |
|
|
|
|
1041
|
|
|
transposed[c] = []; |
1042
|
|
|
} |
1043
|
|
|
transposed[c][r] = arr[r][c]; |
1044
|
|
|
} |
1045
|
|
|
} |
1046
|
|
|
|
1047
|
|
|
return transposed; |
1048
|
|
|
}, |
1049
|
|
|
|
1050
|
|
|
|
1051
|
|
|
none:null |
1052
|
|
|
}; |
1053
|
|
|
|
1054
|
|
|
// register funcs |
1055
|
|
|
UB.registerFuncs(Array.prototype, arrayFuncs); |
1056
|
|
|
|