-
Notifications
You must be signed in to change notification settings - Fork 13
/
Arrays.js
712 lines (662 loc) · 17.7 KB
/
Arrays.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//adds an item into array
Array.prototype.add = function (item) {
var arr = this;
arr.push(item);
return arr;
}
/*adds an item into the array, if the array does not already have
an item whose [prop] property matches the value of item's similar property*/
Array.prototype.addif = function (item, prop) {
var arr = this;
if (prop) {
if (!arr.contains(item[prop], prop)) {
arr.add(item);
}
else {
var matches = arr.where(prop, item[prop]);
if (matches.count() > 0) matches[0] = item;
}
}
else {
if (!arr.contains(item)) {
arr.add(item);
}
}
return arr;
}
Array.prototype.toggle = function (item, prop) {
var arr = this;
if (!prop) {
if (arr.contains(item)) arr.remove(item);
else arr.add(item);
}
else {
if (arr.contains(item[prop], prop)) arr.remove(item[prop], prop);
else arr.add(item);
}
return arr;
}
/*adds a range of items. Usage: arr.addRange([0, 1, 2, 3])*/
Array.prototype.addRange = function (arrs) {
var arr = this;
for (var i = 0; i < arrs.length; i++) {
arr.add(arrs[i]);
}
return arr;
}
Array.prototype.from = function (index) {
var arr = this;
var ret = [];
index = index || 0;
for (var i = index; i < arr.length; i++) {
ret.push(arr[i]);
}
return ret;
}
/*Gets the first item in the array, or first range of items. Usage: arr.first() or arr.first(count)*/
Array.prototype.first = function (count) {
var arr = this.valueOf();
if (count == undefined) {
if (arr.length > 0) {
return arr[0];
}
else return {};
}
else {
var ret = [];
for (var i = 0; i < Math.min(count, arr.count()) ; i++) {
ret.add(arr[i]);
}
return ret;
}
}
//Gets the last item in the array, or last range of items. Usage: arr.last() or arr.last(count)
Array.prototype.last = function (count) {
var arr = this;
if (count == undefined) {
if (arr.length > 0) {
return arr[arr.length - 1];
}
return {};
}
else {
var ret = [];
for (var i = Math.max(arr.length - count, 0) ; i < arr.length; i++) {
ret.add(arr[i]);
}
return ret;
}
}
/*Gets a sub-array with items whose specified property match a specified value. Usage: arr.selectWhere(property as string,
value to match).
*Can be chained. E.g. arr.selectWhere(prop, val).selectWhere(prop1, val1).selectWhere(prop2, val2)
*/
Array.prototype.selectWhere = function (prop, val) {
var arr = this;
var ret = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i][prop] == val) {
ret.add(arr[i]);
}
}
return ret;
}
Array.prototype.where = function (prop, val) {
var arr = this;
if (typeof prop == "string") return arr.selectWhere(prop, val);
if (typeof prop == "function") return arr.selectByFunction(prop);
}
/*Gets a sub-array where properties have values specified
*Usage: arr.selectWheres([ { prop: "name", val: "michael" }, { prop: "sex", val: "male" } ])
*Note: You could use Chained selectWhere instead.
*/
Array.prototype.selectWheres = function (prop_vals) {
var arr = this;
var ret = [];
for (var i = 0; i < arr.length; i++) {
var b = true;
for (var j = 0; j < prop_vals.length; j++) {
var prop = prop_vals[j].prop;
var val = prop_vals[j].val;
if (arr[i][prop] != val) {
b = false;
}
}
if (b) {
ret.add(arr[i]);
}
}
return ret;
}
/*Returns a sub array with items that meet a specified condition provided in a function
*Usage: arr.selectByFunction(function (i) {
* if (arr[i].price <= 500) { return arr[i]; }
* });
*Note that you should always return the sub item if you want it to be in the sub array
*You could also use this as an iterator in place of a for / foreach loop.
*/
Array.prototype.selectByFunction = function (condition) {
var arr = this;
var ret = [];
for (var i = 0; i < arr.length; i++) {
if (condition(i)) {
ret.add(arr[i]);
}
}
return ret;
}
/*Performs bubble sort on an array
*Usage: arr.sort() or arr.sort("asc"): sorts in ascending order.
*Usage: arr.sort("desc"): sorts in descending order.
*Note: For best performance, use on primitive types only
*/
Array.prototype.sort = function (type) {
var arr = this.valueOf();
var swapped;
do {
swapped = false;
var b;
for (var i = 0; i < arr.length - 1; i++) {
if (type == undefined || type == "asc") b = arr[i] > arr[i + 1];
else b = arr[i] < arr[i + 1];
if (b) {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
}
while (swapped);
return arr;
}
/*Performs insertion sort on an array
*Usage: arr.sort()
*Note: Much faster than bubble sort. To be used on very large arrays. A bit annoying to debug tho
*/
Array.prototype.isort = function (type) {
var arr = this.valueOf();
if (arr.length > 1) {
arr.push(arr[0]);
}
var ret = [];
for (var i = 0; i < arr.length; i++) {
var index = 0;
var finished = true;
for (var j = 1; j < ret.length; j++) {
if (ret[j] >= arr[i]) {
index = j;
finished = false;
break;
}
else finished = false;
}
if (ret.length === 0) ret.push(arr[i]);
else if (index === 0 || finished == true) ret.push(arr[i]);
else ret = ret.insert(arr[i], index);
}
if (ret.length > 1) {
var first = ret[0];
ret = ret.splice(1, ret.length - 1);
}
return ret;
}
/*Performs binary search on a sorted array and returns index of found item
*Usage: arr.bsearch(5)
*/
Array.prototype.bsearch = function (item) {
var arr = this.valueOf();
if (arr.length == 0) return -1
else {
var start = 0;
var end = arr.length - 1;
while (start < end) {
var mid = Math.floor(Number(start + end) / 2);
if (item < arr[mid]) {
end = mid;
}
else if (item > arr[mid]) {
start = mid;
}
else {
return mid;
}
}
console.log(arr[start]);
console.log(start);
if (item == arr[start]) return start;
else return -1;
}
}
/*Sort by array_item property. type could be empty (undefined) or 'asc' or 'desc'
* Sorts an array by a specific property.
* Usage: arr.sortBy(property)
* Usage: arr.sortBy(property, "asc")
* Usage: arr.sortBy(property, "desc")
*/
Array.prototype.sortBy = function (prop, type) {
var arr = this;
if (!prop) return arr.sort(type);
var swapped;
do {
swapped = false;
for (var i = 0; i < arr.length - 1; i++) {
var b;
if (type == undefined || type == "asc") b = arr[i][prop] > arr[i + 1][prop];
else b = arr[i][prop] < arr[i + 1][prop];
if (b) {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
}
while (swapped);
}
Array.prototype.sortfn = function (fn) {
fn = fn || function (val1, val2) { return val1 > val2; }
if (typeof fn != 'function') fn =function (val1, val2) { return val1 > val2; }
var arr = this;
var swapped;
do {
swapped = false;
for (var i = 0; i < arr.length - 1; i++) {
if (fn(arr[i], arr[i + 1])) {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
}
while (swapped);
return arr;
}
/*returns boolean whether the array is sorted or not
*Usage: arr.isSorted() or arr.isSorted("asc")
*Usage: arr.isSorted("desc")
*/
Array.prototype.isSorted = function (type) {
var arr = this;
for (var i = 1; i < arr.length; i++) {
var b;
if (type == undefined || type == "asc") b = arr[i] < arr[i - 1];
else b = arr[i] > arr[i - 1];
if (b) {
return false;
}
}
return true;
}
/*inserts into an index
*Usage: arr.insert(5, 2);
*/
Array.prototype.insert = function (item, index) {
var ret = [];
var arr = this;
if (index == undefined) {
arr.push(item);
return arr;
}
if (index > arr.count() - 1) return arr;
for (var i = 0; i < index; i++) {
ret.add(arr[i]);
}
ret.add(item);
for (var i = index; i < arr.length; i++) {
ret.add(arr[i]);
}
arr.clear();
arr.addRange(ret);
arr.prev = [];
return ret;
}
/*clears the array
*Usage: arr.clear();
*/
Array.prototype.clear = function () {
var arr = this;
while (arr.count() > 0) {
arr.pop();
}
return arr;
}
//returns number of items in array. same as array.length. Usage: arr.count()
Array.prototype.count = function () {
var arr = this;
return this.length;
}
/*returns index of an arrayitem where arrayitem's property is equal to item. Similar to arr.contains(item, prop)
*Usage: arr.indexOfProp("michael", "name");
*/
Array.prototype.indexOfProp = function (item, prop) {
var arr = this;
if (!prop) return arr.indexOf(item);
for (var i = 0; i < arr.length; i++) {
if (arr[i][prop] == item) {
return i;
}
}
return -1;
}
//get array item at index
Array.prototype.getAt = function (index) {
var arr = this;
for (var i = 0; i < arr.length; i++) {
if (i == index) return arr[i];
}
return undefined;
}
//returns boolean showing whether array contains an item, or if one of its properties (arr[i].prop) is equal to item
Array.prototype.contains = function (item, prop) {
var arr = this;
if (prop == undefined || prop == null) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == item) {
return true;
}
}
}
else {
for (var i = 0; i < arr.length; i++) {
if (arr[i][prop] == item) return true;
}
}
return false;
}
//makes items distinct
Array.prototype.distinct = function (prop) {
var arr = this.valueOf();
var ret = [];
for (var i = 0; i < arr.length; i++) {
var b;
if (prop == undefined) b = !ret.contains(arr[i]);
else b = !ret.contains(arr[i][prop], prop);
if (b) {
ret.push(arr[i]);
}
}
return ret;
}
//removes an item by either itself or its property
Array.prototype.remove = function (item, prop) {
var arr = this;
var x = 0;
var ret = [];
if (prop == undefined || prop == null) {
for (var i = 0; i < arr.count() ; i++) {
if (arr[i] != item) {
ret.push(arr[i]);
}
}
}
else {
for (var i = 0; i < arr.count() ; i++) {
if (arr[i][prop] != item) {
ret.push(arr[i]);
}
}
}
arr.clear();
arr.addRange(ret);
return arr;
}
//Removes an item at an index of the array
Array.prototype.removeAt = function (index) {
var arr = this;
var ret = [];
for (var i = 0; i < arr.length; i++) {
if (i != index) {
ret.add(arr[i]);
}
}
arr.clear();
arr.addRange(ret);
return arr;
}
//Shuffles the items in an Array
Array.prototype.shuffle = function () {
var arr = this.valueOf();
var ret = [];
while (ret.length < arr.length) {
var x = arr[Math.floor(Number(Math.random() * arr.length))];
if (!(ret.indexOf(x) >= 0)) ret.push(x);
}
return ret;
}
/*Creates a new Array using an existing one. Could be used to clone an array.
*Usage: [].create([0, 1, 2, 3])
*/
Array.prototype.create = function (items) {
var ret = [];
for (var i = 0; i < items.length; i++) {
ret.add(items[i]);
}
return ret;
}
/*Returns an array of items consisting of all numbers starting from Number(start) till and including Number(end)*/
Array.prototype.range = function (start, end) {
var arr = this;
var ret = [];
start = start || 0;
if (!end) {
for (var i = 0; i < start; i++) {
ret.add(i);
}
}
else {
for (var i = start; i <= end; i++) {
ret.add(i);
}
}
return ret;
}
Array.range = function (start, end) {
return [].range(start, end);
}
//returns an array containing the values of designated property in each of its items
Array.prototype.select = function (prop) {
var arr = this;
if (!prop) return this.valueOf();
var ret = [];
arr.selectByFunction(function (i) {
ret.push(arr[i][prop]);
});
return ret;
}
//true, if the array is empty
Array.prototype.isEmpty = function () {
return this.length == 0;
}
/*pass in a function with an item as argument to be performed on each item of the array ... e.g.
[].each(function (item) {
...
});
*/
Array.prototype.each = function (fn) {
var arr = this;
for (var i = 0; i < arr.length; i++) {
arr.index = i;
fn(arr[i]);
}
delete arr.index;
return arr;
}
Array.prototype.flatten = function () {
var arr = this.valueOf();
var ret = [];
arr.each(function (item) {
if (Array.isArray(item)) {
item.each(function (itm) {
ret.add(itm);
});
}
else {
ret.add(item);
}
});
arr.clear();
arr = [].create(ret);
return arr;
}
Array.prototype.string = function (place) {
var arr = this;
var str = "";
place = place || "";
arr.each(function (item) {
str += item + place;
});
return str;
}
//divides an array into pages, with a maximum size
Array.prototype.paginate = function (maxsize) {
if (!maxsize) maxsize = 10;
var arr = this;
var ret = [];
var i = 0;
var ret_i = [];
arr.each(function (item) {
if (i < maxsize) {
ret_i.add(item);
i += 1;
}
else {
ret.add([].create(ret_i));
ret_i.clear();
ret_i.add(item);
i = 1;
}
});
if (!ret_i.isEmpty()) {
ret.add([].create(ret_i));
}
return ret;
}
//returns the minimum array item
Array.prototype.min = function () {
var arr = this;
if (arr.isEmpty()) return 0;
var min = arr.first();
if (arr.count() > 0) {
arr.each(function (item) {
min = item < min ? item : min;
});
return min;
}
return undefined;
}
//returns the maximum array item
Array.prototype.max = function () {
var arr = this;
if (arr.isEmpty()) return 0;
var max = arr.first();
if (arr.count() > 0) {
arr.each(function (item) {
max = item > max ? item : max;
});
return max;
}
return undefined;
}
/*This should used for numbers. It returns the average*/
Array.prototype.average = function () {
var arr = this;
var total = 0;
arr.each(function (item) {
if (Number(item)) total += Number(item);
});
return total / arr.count();
}
Array.prototype.sum = function () {
var arr = this;
var sum = 0;
arr.each(function (x) {
sum += x;
});
return sum;
}
/*Lets you sync arrays by comparing a particular property,
which acts as a primary key, making sure that the values of that property
in each item in that array are unique*/
Array.prototype.sync = function (arr2, prop) {
var arr = this;
if (prop) {
arr2.each(function (item) {
arr.addif(item, prop);
});
}
else {
arr2.each(function (item) {
arr.addif(item);
})
}
return arr;
}
Array.prototype.random = function (count) {
var arr = this.valueOf();
if (count && !isNaN(count) && typeof count == 'number') {
if (arr.length == 0) return undefined;
var ret = [];
for (var i = 0; i < count; i++) {
ret.push(arr[Math.floor(Number(Math.random() * arr.length))]);
}
return ret;
}
else {
return arr[Math.floor(Number(Math.random() * arr.length))];
}
}
Array.prototype.all = function (fn) {
var b = true;
var arr = this;
fn = fn || function (i) {return true};
arr.each(function (item) {
if (fn(item) == false) b = false;
});
return b;
}
Array.prototype.frequency = function (value, prop) {
var arr = this;
if (!prop) {
var count = 0;
arr.each(function (val) {
if (val == value) count++;
});
return count;
}
else {
var count = 0;
arr.each(function (val) {
if (val[prop] == value) count++;
});
return count;
}
}
Array.prototype.trim = function () {
var arr = this;
var ret = [];
arr.each(function (item) {
if (item) ret.add(item);
});
return ret;
}
/* An enhanced version of the togGle() function.
Toggling the arraY toggle(value) On and Off Like a switch*/
Array.prototype.xtoggle = function (item) {
var arr = this;
if (arr.indexOf(item) >= 0) {
var index = arr.indexOf(item);
arr.prev.push({index: index, value: item});
arr.remove(item);
}
else {
var prevValue = arr.prev.where('value', item).first();
if (typeof prevValue.index != 'undefined') {
arr.insert(prevValue.value, prevValue.index);
arr.prev.remove(prevValue.index, 'index');
}
else arr.push(item);
}
return arr;
};