forked from andrewplummer/Sugar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumerable.js
2035 lines (1676 loc) · 90.7 KB
/
enumerable.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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace('Array', function() {
'use strict';
method('map', function() {
var fn;
test([1,4,9], [Math.sqrt], [1,2,3], 'passing Math.sqrt directly');
test([{ foo: 'bar' }], [function(el) { return el['foo']; }], ['bar'], 'with key "foo"');
fn = function(el, i, a) {
equal(el, 'a', 'first parameter is the element');
equal(i, 0, 'second parameter is the index');
equal(a, ['a'], 'third parameter is the array');
equal(this.toString(), 'this', 'scope is passed properly');
};
run(['a'], 'map', [fn, 'this']);
test(['foot','goose','moose'], [function(el) { return el.replace(/o/g, 'e'); }], ['feet', 'geese', 'meese'], 'with regexp');
test(['foot','goose','moose'], ['length'], [4,5,5], 'length');
test([{name:'john',age:25},{name:'fred',age:85}], ['age'], [25,85], 'age');
test([{name:'john',age:25},{name:'fred',age:85}], ['name'], ['john','fred'], 'name');
test([{name:'john',age:25},{name:'fred',age:85}], ['cupsize'], twoUndefined, '(nonexistent) cupsize');
test([], ['name'], [], 'empty array');
// Nested properties with dot syntax
test([{name:{first:'John',last:'Waters'}},{name:{first:'Fred',last:'Flintstone'}}], ['name.first'], ['John', 'Fred'], 'deep matching with dot');
test([{a:{b:{c:'x'}}},{a:{b:{c:'y'}}},{a:{b:{c:'z'}}}], ['a.b.c'], ['x','y','z'], 'deeper matching with dot');
test([{a:[1]},{a:[2]}], ['a.0'], [1,2], 'matching nested array indexes');
test([{a:[1]},{b:[2]}], ['a.0'], safeArray(1, undefined), 'matching nested array index non-existent');
test([{a:'a'},{b:'a'}], ['.'], twoUndefined, 'single dot');
test([{a:'a'},{b:'a'}], ['..'], twoUndefined, 'double dot');
test([[[1,2]],[[1,2]]], ['0.1'], [2,2], 'deep arrays');
test([{name:{first:'Joe',last:'P'}},{name:{first:'John',last:'Q'}}], [['name.first', 'name.last']], [['Joe','P'], ['John', 'Q']], 'array with dots');
test([1,2,3], ['toString'], ['1','2','3'], 'calls a function on a shortcut string');
raisesError(function(){ run([1,2,3], 'map') }, 'no argument raises a type error');
raisesError(function(){ run([1,2,3], 'map', oneUndefined) }, 'undefined raises a type error');
raisesError(function(){ run([1,2,3], 'map', [null]) }, 'null raises a type error');
test([1,2,3], [4], threeUndefined, 'number');
// Nested properties with dot and bracket syntax
var accounts = [
{
profile: {
addresses: [{
street: '1600 Pennsylvania Ave',
city: 'Washington DC'
}, {
street: '221B Baker St',
city: 'London'
}, {
street: '350 5th Ave',
city: 'New York'
}]
}
},
{
profile: {
addresses: [{
street: '31 Spooner St.',
city: 'Quahog'
}, {
street: '742 Evergreen Terrace',
city: 'Springfield'
}, {
street: '342 Gravelpit Terrace',
city: 'Bedrock'
}]
}
}
];
test(accounts, ['profile.addresses[0]'], [accounts[0].profile.addresses[0], accounts[1].profile.addresses[0]], 'deep with bracket');
test(accounts, ['profile.addresses[0].city'], ['Washington DC', 'Quahog'], 'deep with bracket and trailing dot');
test(accounts, ['profile.addresses[1]'], [accounts[0].profile.addresses[1], accounts[1].profile.addresses[1]], 'deep with bracket | 1');
test(accounts, ['profile.addresses[1].city'], ['London', 'Springfield'], 'deep with bracket and trailing dot | 1');
test(accounts, ['profile.addresses[-1]'], [accounts[0].profile.addresses[2], accounts[1].profile.addresses[2]], 'deep with bracket | -1');
test(accounts, ['profile.addresses[-1].city'], ['New York', 'Bedrock'], 'deep with bracket and trailing dot | -1');
// Bracket range syntax
var a1 = accounts[0].profile.addresses.slice(0, 2);
var a2 = accounts[1].profile.addresses.slice(0, 2);
test(accounts, ['profile.addresses[0..1]'], [a1, a2], 'allows range syntax');
test(accounts, ['profile.addresses[0..1].city'], [['Washington DC', 'London'], ['Quahog', 'Springfield']], 'allows range syntax with trailing dot');
raisesError(function() { run([{foo:'bar'}], 'map', 'foo[0..1]'); }, 'Range syntax on an object should raise an error', TypeError);
// Issue #386
var arr = [
{
name: 'john',
age: 25
},
{
name: 'fred',
age: 85
}
];
test(arr, [['name', 'age']], [['john', 25], ['fred', 85]], 'mapping on both name and age');
test(arr, [['name', 'hair']], [safeArray('john', undefined), safeArray('fred', undefined)], 'mapping on name and non-existent property');
test(arr, [['hair', 'age']], [safeArray(undefined, 25), safeArray(undefined, 85)], 'mapping on non-existent property and name');
test(arr, [['hair', 'eyes']], [twoUndefined, twoUndefined], 'mapping on two non-existent properties');
var arr = [
{
age: 25,
size: 3
},
{
age: 85,
size: 7
}
];
var count1 = 0;
var count2 = 0;
var fn1 = function(obj, i, a) {
equal(this.valueOf(), 0, 'context should still be passable');
equal(obj, arr[i], 'first argument should be the element');
equal(i, count1, 'second argument should be the index');
equal(a, arr, 'third argument should be the array');
count1++;
return obj.age + 5;
}
var fn2 = function(obj) {
count2++;
return obj.size - 3;
}
var expected = [
[30, 0],
[90, 4]
]
var result = run(arr, 'map', [[fn1, fn2], 0]);
equal(result, expected, 'should be able to use two mapping functions');
equal(count1, 2, 'first mapping function should have run twice');
equal(count2, 2, 'second mapping function should have run twice');
// Testing the context
var resultContext = null;
var fn = function() {
resultContext = this;
}
run(['x'], 'map', [fn]);
equal(resultContext, testNullScope, 'Context should be passable with explicit mapping fn');
var resultContext = null;
var fakeContext = {};
var fn = function() {
resultContext = this;
}
run(['x'], 'map', [fn, fakeContext]);
equal(resultContext, fakeContext, 'Context should be passable with explicit mapping fn');
// Issue #525
var result = [{foo:'foo'},{bar:'bar'}].map(Object.keys);
equal(result, [['foo'],['bar']], 'non-function argument should not be called');
});
method('every', function() {
var fn, arr;
raisesError(function() { run([1,2,3], 'every', []); }, 'should error with no args');
test([1,1,1], [1], true, 'numeric | 1 matches');
test([1,1,2], [1], false, 'numeric | 1 does not match');
test([1,2,3], [3], false, 'numeric | 3 does not match');
test(['a','a','a'], ['a'], true, 'accepts a string shortcut match');
test(['a','b','a'], ['a'], false, 'accepts a string shortcut no match');
test(['a','b','c'], [/[a-f]/], true, 'accepts a regex shortcut match');
test(['a','b','c'], [/[m-z]/], false, 'accepts a regex shortcut no match');
test([{a:1},{a:1}], [{a:1}], true, 'checks objects match');
test([{a:1},{a:2}], [{a:1}], false, 'checks object no match');
test(['a','b','c'], [function(e) { return e.length > 1; }], false, 'alphabet | length is greater than 1');
test(['a','b','c'], [function(e) { return e.length < 2; }], true, 'alphabet | length is less than 2');
test(['a','bar','cat'], [function(e) { return e.length < 2; }], false, 'a,bar,cat | length is less than 2');
test([{a:1},{a:2},{a:1}], [function(e) { return e['a'] == 1; }], false, 'objects | key "a" is 1');
test([{a:1},{a:2},{a:1}], [function(e) { return e['b'] == 1; }], false, 'objects | key "b" is 1');
test([{a:1},{a:1},{a:1}], [function(e) { return e['a'] == 1; }], true, 'objects | key "a" is 1 for all');
fn = function(el) {
return el >= 10;
}
test([12,5,8,130,44], [fn], false, 'not every element is greater than 10');
test([12,54,18,130,44], [fn], true, 'every element is greater than 10');
test(threeUndefined, [undefined], true, 'all undefined');
test(['a', 'b'], [undefined], false, 'none undefined');
arr = testClone(threeUndefined);
arr.push('a');
test(arr, [oneUndefined], false, 'every undefined');
fn = function(el, i, a) {
equal(el, 'a', 'First parameter is the element');
equal(i, 0, 'Second parameter is the index');
equal(a, ['a'], 'Third parameter is the array');
equal(this.toString(), 'this', 'Scope is passed properly');
}
run(['a'], 'every', [fn, 'this']);
test([{name:'john',age:25}], [{name:'john',age:25}], true, 'handles complex objects');
test([{name:'john',age:25},{name:'fred',age:85}], ['age'], false, 'simple string mistakenly passed for complex objects');
test([{name:'john',age:25},{name:'fred',age:85}], [{name:'john',age:25}], false, "john isn't all");
});
method('some', function() {
var arr, fn;
test([1,2,3], [1], true, 'accepts a number shortcut match');
test([2,3,4], [1], false, 'accepts a number shortcut no match');
test(['a','b','c'], ['a'], true, 'accepts a string shortcut match');
test(['b','c','d'], ['a'], false, 'accepts a string shortcut no match');
test(['a','b','c'], [/[a-f]/], true, 'accepts a regex shortcut match');
test(['a','b','c'], [/[m-z]/], false, 'accepts a regex shortcut no match');
test([{a:1},{a:2}], [{a:1}], true, 'checks objects match');
test([{a:2},{a:3}], [{a:1}], false, 'checks object no match');
test([0], [0], true, '[0] | 0');
test([12,5,8,130,44], [function(el, i, a) { return el > 10 }], true, 'some elements are greater than 10');
test([12,5,8,130,44], [function(el, i, a) { return el < 10 }], true, 'some elements are less than 10');
test([12,54,18,130,44], [function(el, i, a) { return el >= 10 }], true, 'all elements are greater than 10');
test([12,5,8,130,44], [function(el, i, a) { return el < 4 }], false, 'no elements are less than 4');
test([], [function(el, i, a) { return el > 10 }], false, 'no elements are greater than 10 in an empty array');
test(threeUndefined, oneUndefined, true, 'all undefined');
test(['a', 'b'], oneUndefined, false, 'none undefined');
arr = testClone(threeUndefined);
arr.push('a');
test(arr, oneUndefined, true, 'some undefined');
fn = function(el, i, a) {
equal(el, 'a', 'first parameter is the element');
equal(i, 0, 'second parameter is the index');
equal(a, ['a'], 'third parameter is the array');
equal(this.toString(), 'this', 'scope is passed properly');
}
run(['a'], 'some', [fn, 'this']);
test([{name:'john',age:25}], [{name:'john',age:25}], true, 'handles complex objects');
test([{name:'john',age:25},{name:'fred',age:85}], ['age'], false, 'simple string mistakenly passed for complex objects');
test([{name:'john',age:25},{name:'fred',age:85}], [{name:'john',age:25}], true, 'john can be found ');
var people = [
{ name: 'jim', age: 27, hair: 'brown' },
{ name: 'mary', age: 52, hair: 'blonde' },
{ name: 'ronnie', age: 13, hair: 'brown' },
{ name: 'edmund', age: 27, hair: 'blonde' },
{ name: 'buddy', age: 82, hair: { color: 'red', type: 'long', cost: 15, last_cut: new Date(2010, 4, 18) } }
];
test(people, [{ age: 27 }], true, 'complex | one property');
test(people, [{ age: 27, hair: 'brown' }], true, 'complex | two properties');
test(people, [{ hair: { color: 'red' }}], true, 'complex | nested property');
test(people, [{ hair: { color: 'green' }}], false, 'complex | non-matching nested property');
test(people, [{ hair: { color: 'red', type: 'long' }}], true, 'complex | two nested properties');
test(people, [{ hair: { color: 'green', type: 'mean' }}], false, 'complex | two non-matching nested properties');
test(people, [{ hair: { color: 'red', type: 'mean' }}], false, 'complex | two nested properties, one non-matching');
test(people, [{ hair: { color: 'red', life: 'long' }}], false, 'complex | two nested properties, one non-existing');
test(people, [{ hair: { color: /r/ }}], true, 'complex | nested regex');
test(people, [{ hair: { cost: 15 }}], true, 'complex | nested number');
test(people, [{ hair: { cost: 23 }}], false, 'complex | nested non-matching number');
test(people, [{ hair: { cost: undefined }}], false, 'complex | nested undefined property');
test(people, [{ hair: { cost: NaN }}], false, 'complex | nested property is NaN');
test(people, [{ hair: { color: function(c){ return c == 'red'; } }}], true, 'complex | nested function');
test(people, [{ some: { random: { shit: {}}}}], false, 'complex | totally unrelated properties');
test(people, [{ hair: { last_cut: new Date(2010, 4, 18) }}], true, 'complex | simple date');
});
method('filter', function() {
var fn;
test([1,2,3], [1], [1], 'accepts a number shortcut match');
test([2,3,4], [1], [], 'accepts a number shortcut no match');
test(['a','b','c'], ['a'], ['a'], 'accepts a string shortcut match');
test(['b','c','d'], ['a'], [], 'accepts a string shortcut no match');
test(['a','b','c'], [/[a-f]/], ['a','b','c'], 'accepts a regex shortcut match');
test(['a','b','c'], [/[m-z]/], [], 'accepts a regex shortcut no match');
test([{a:1},{a:2}], [{a:1}], [{a:1}], 'checks objects match');
test([{a:2},{a:3}], [{a:1}], [], 'checks object no match');
test(['a','a','c'], ['a'], ['a','a'], 'a,a');
test(['a','b','c'], ['q'], [], 'q');
test([2,2,3], [2], [2,2], '2,2');
test([1,2,3], [4], [], '4');
test([12,4,8,130,44], [function(el, i, a) { return el > 10 }], [12,130,44], 'numbers above 10');
test([12,4,8,130,44], [function(el, i, a) { return el < 10 }], [4,8], 'numbers below 10');
fn = function(el, i, a) {
equal(el, 'a', 'first parameter is the element');
equal(i, 0, 'second parameter is the index');
equal(a, ['a'], 'third parameter is the array');
equal(this.toString(), 'this', 'scope is passed properly');
}
run(['a'], 'filter', [fn, 'this']);
test([{name:'john',age:25},{name:'fred',age:85}], ['age'], [], 'simple string mistakenly passed for complex objects');
test([{name:'john',age:25},{name:'fred',age:85}], [{name:'john',age:25}], [{name:'john',age:25}], 'filtering john');
test([{name:'john',age:25},{name:'fred',age:85}], [{name:'fred',age:85}], [{name:'fred',age:85}], 'filtering fred');
raisesError(function() { run([1,2,3], 'filter'); }, 'no argument raises a type error');
test(threeUndefined, oneUndefined, threeUndefined, 'undefined should match all undefined');
test(threeUndefined, [null], [], 'null should not match all undefined');
test(undefinedWithNull, oneUndefined, oneUndefined, 'undefined should match one undefined');
test(undefinedWithNull, [null], [null], 'null should match one null');
test([null, null], [null], [null, null], 'null should match all null');
test([null, null], oneUndefined, [], 'undefined should not match all null');
test([{a:1},{b:2},{c:3}], [{a:1}], [{a:1}], 'a:1');
test([{a:1},{a:1},{c:3}], [{a:1}], [{a:1},{a:1}], 'a:1,a:1');
test([{a:1},{b:2},{c:3}], [{d:4}], [], 'd:4');
test([{a:1},{b:2},{c:3}], [{c:4}], [], 'c:4');
test([[1,2],[2,3],[4,5]], [[2,3]], [[2,3]], '2,3');
test([[1,2],[2,3],[4,5]], [[2,4]], [], '2,4');
test([[1,2],[2,3],[2,3]], [[2,3]], [[2,3],[2,3]], '[2,3],[2,3]');
test(['foo','bar'], [/f+/], ['foo'], '/f+/');
test(['foo','bar'], [/[a-f]/], ['foo','bar'], '/[a-f]/');
test(['foo','bar'], [ /q+/], [], '/q+/');
test([{a:10},{a:8},{a:3}], [function(e) { return e['a'] > 5; }, 0], [{a:10},{a:8}], 'key "a" is greater than 5');
test([{a:10},{a:8},{a:3}], [function(e) { return e['a'] > 5; }, 0, true], [{a:10},{a:8}], 'looping | key "a" is greater than 5');
test([function() {}], [function(e) {}, 0], [], 'null function');
test([null, null], [null, 0], [null, null], 'null');
test([function() {}], [function(e) {}, 0, true], [], 'looping | null function');
test([null, null], [null, 0, true], [null, null], 'looping | null');
var fn = function() {
return false;
}
test([fn], [fn], [], 'should not find functions by reference');
var undefinedContextObj = (function(){ return this; }).call(undefined);
var fn = function() {
equal(this, undefinedContextObj, 'this context should be the array');
}
run([1], 'filter', [fn]);
var people = [
{ name: 'jim', age: 27, hair: 'brown' },
{ name: 'mary', age: 52, hair: 'blonde' },
{ name: 'ronnie', age: 13, hair: 'brown' },
{ name: 'edmund', age: 27, hair: 'blonde' },
{ name: 'buddy', age: 82, hair: { color: 'red', type: 'long', cost: 15, last_cut: new Date(2010, 4, 18) } }
];
raisesError(function(){ run([], 'filter') }, 'no argument raises an error', TypeError);
test(people, [{}], people, 'complex | empty object');
test(people, ['age'], [], 'complex | string argument');
test(people, [4], [], 'complex | number argument');
test(people, [{ age: 27 }], [people[0], people[3]], 'complex | one property');
test(people, [{ age: 27, hair: 'brown' }], [people[0]], 'complex | two properties');
test(people, [{ hair: { color: 'red' }}], [people[4]], 'complex | nested property');
test(people, [{ hair: { color: 'green' }}], [], 'complex | non-matching nested property');
test(people, [{ hair: { color: 'red', type: 'long' }}], [people[4]], 'complex | two nested properties');
test(people, [{ hair: { color: 'green', type: 'mean' }}], [], 'complex | two non-matching nested properties');
test(people, [{ hair: { color: 'red', type: 'mean' }}], [], 'complex | two nested properties, one non-matching');
test(people, [{ hair: { color: 'red', life: 'long' }}], [], 'complex | two nested properties, one non-existing');
test(people, [{ hair: { color: /r/ }}], [people[4]], 'complex | nested regex');
test(people, [{ hair: { cost: 15 }}], [people[4]], 'complex | nested number');
test(people, [{ hair: { cost: 23 }}], [], 'complex | nested non-matching number');
test(people, [{ hair: { cost: undefined }}], [], 'complex | nested undefined property');
test(people, [{ hair: { post: undefined }}], [people[4]], 'complex | nested undefined property non-existent');
test(people, [{ hair: { cost: NaN }}], [], 'complex | nested property is NaN');
test(people, [{ hair: { color: function(c){ return c == 'red'; } }}], [people[4]], 'complex | nested function');
test(people, [{ some: { random: { shit: {}}}}], [], 'complex | totally unrelated properties');
test(people, [{ hair: { last_cut: new Date(2010, 4, 18) }}], [people[4]], 'complex | simple date');
// Issue #157 Ensure that instances can be subject to fuzzy matches despite not being "objects"
function Foo(a) {
this.a = a;
}
var one = new Foo('one');
var two = new Foo('two');
var three = new Foo('three');
var four = new Foo(new Date(2001, 3, 15));
test([one, two, three, four], [{ a: 'one' }], [one], 'matches class instances | object with string');
test([one, two, three, four], [{ a: /^t/ }], [two, three], 'matches class instances | object with regex');
test([one, two, three, four], ['one'], [], 'matches class instances | string');
test([one, two, three, four], [/t/], [one, two, three, four], 'directly passing a regex is matching the objects stringified');
test([one, two, three, four], [/x/], [], 'directly passing a regex with no matching letter');
test([one, two, three, four], [true], [], 'matches class instances | boolean');
test([one, two, three, four], [new Date()], [], 'matches class instances | now');
test([one, two, three, four], [new Date(2001, 3, 15)], [], 'matches class instances | correct date');
test([one, two, three, four], [null], [], 'matches class instances | null');
test([one, two, three, four], oneUndefined, [], 'matches class instances | undefined');
test([one, two, three, four], [{ a: 'twof' }], [], 'matches class instances | nonexistent string');
test([one, two, three, four], [{ b: 'one' }], [], 'matches class instances | nonexistent property');
test([one, two, three, four], [{}], [one, two, three, four], 'matches class instances | empty object');
test([one, two, three, four], [{ a: new Date(2001, 3, 15) }], [four], 'matches class instances | object with correct date');
test([one, two, three, four], [{ b: new Date(2001, 3, 15) }], [], 'matches class instances | object with correct date but wrong property');
test([one, two, three, four], [{ a: new Date(2001, 3, 16) }], [], 'matches class instances | object with incorrect date');
test([one, two, three, four], [{ a: new Date(2001, 3, 15, 0, 0, 0, 1) }], [], 'matches class instances | object with date off by 1ms');
var date = new Date(2001, 3, 15);
var timestamp = date.getTime();
var obj = { a: { getTime: function() { return timestamp; } }};
test([obj], [{ a: date }], [obj], 'duck typing for date matching');
var five = new Foo(one);
test([five], [{ a: 'one' }], [], 'nested instances | object with string');
test([five], [{ a: { a: 'one' } }], [five], 'nested instances | object with double nested string');
test([five], [{ a: { a: 'two' } }], [], 'nested instances | object with double nested string but incorrect');
// Fuzzy matching behavior on functions.
var count = 0;
var fn = function(){ count ++; };
run([1,2,3], 'filter', [fn]);
equal(count, 3, 'functions treated as callbacks when matching against non-functions');
count = 0;
run([function() {}, function() {}, function() {}], 'filter', [fn]);
equal(count, 3, 'functions are not directly matched');
var fn1 = function() {};
var fn2 = function() {};
var matchFn1 = function(el) {
return el === fn1;
}
var matchFn2 = function(el) {
return el === fn2;
}
equal(run([fn1, fn2, fn1], 'filter', [matchFn1]), [fn1, fn1], 'functions can be matched inside the callback');
equal(run([fn1, fn2, fn1], 'filter', [matchFn2]), [fn2], 'fn2 | functions can be matched inside the callback');
});
method('find', function() {
test(['a','b','c'], ['a'], 'a', 'a');
test(['a','a','c'], ['a'], 'a', 'first a');
test(['a','b','c'], ['q'], undefined, 'q');
test([1,2,3], [1], 1, '1');
test([2,2,3], [2], 2, '2');
test([1,2,3], [4], undefined, '4');
test([{a:1},{b:2},{c:3}], [{a:1}], {a:1}, 'a:1');
test([{a:1},{a:1},{c:3}], [{a:1}], {a:1}, 'first a:1');
test([{a:1},{b:2},{c:3}], [{d:4}], undefined, 'd:4');
test([{a:1},{b:2},{c:3}], [{c:4}], undefined, 'c:4');
test([[1,2],[2,3],[4,5]], [[2,3]], [2,3], '2,3');
test([[1,2],[2,3],[4,5]], [[2,4]], undefined, '2,4');
test([[1,2],[2,3],[2,3]], [[2,3]], [2,3], 'first 2,3');
test(['foo','bar'], [/f+/], 'foo', '/f+/');
test(['foo','bar'], [/[a-f]/], 'foo', '/a-f/');
test(['foo','bar'], [/q+/], undefined, '/q+/');
test([function() {}], [function(e) {}, 0], undefined, 'undefined function');
test([null, null], [null, 0], null, 'null');
test(threeUndefined, safeArray(undefined, 0), undefined, 'undefined');
test(safeArray(undefined, 'a'), safeArray(undefined, 1), undefined, 'undefined can be found');
var count = 0;
[1,2,3].find(function(n) {
count++;
return n == 1;
});
equal(count, 1, 'should immediately finish when it finds a match');
var count = 0;
run([1,2,3], 'find', [function(n) {
count++;
return n == 1;
}]);
equal(count, 1, 'should also be mapped to global');
raisesError(function() { run([1,2,3], 'find'); }, 'no argument raises a type error');
});
method('findIndex', function() {
raisesError(function() { run([1,2,3], 'findIndex'); }, 'no argument raises a type error');
test(['a','b','c'], ['b'], 1, 'b in a,b,c');
test(['a','b','c'], ['b', 0], 1, 'b in a,b,c from 0');
test(['a','b','c'], ['a'], 0, 'a in a,b,c');
test(['a','b','c'], ['f'], -1, 'f in a,b,c');
test(['a','b','c','b'], ['b'], 1, 'finds first instance');
test([5,2,4], [5], 0, '5 in 5,2,4');
test([5,2,4], [2], 1, '2 in 5,2,4');
test([5,2,4], [4], 2, '4 in 5,2,4');
test([{ foo: 'bar' }], [{ foo: 'bar' }], 0, 'will find deep objects');
test([{ foo: 'bar' }], [function(a) { return a.foo === 'bar'; }], 0, 'will run against a function');
test(['a','b','c'], [/[bz]/], 1, 'matches regexp');
var people = [
{ name: 'jim', age: 27, hair: 'brown' },
{ name: 'mary', age: 52, hair: 'blonde' },
{ name: 'ronnie', age: 13, hair: 'brown' },
{ name: 'edmund', age: 27, hair: 'blonde' }
];
test(people, [function(person) { return person.age == 13; }], 2, 'JSON objects');
});
method('none', function() {
test([1,2,3], [1], false, 'numeric | 1');
test([1,2,3], [4], true, 'numeric | 4');
test([1,2,3], ['a'], true, 'numeric | a');
test(['a','b','c'], ['a'], false, 'alphabet | a');
test(['a','b','c'], ['f'], true, 'alphabet | f');
test(['a','b','c'], [/[a-f]/], false, 'alphabet | /[a-f]/');
test(['a','b','c'], [/[m-z]/], true, 'alphabet | /[m-z]/');
test([{a:1},{a:2},{a:1}], [1], true, 'objects | 1');
test([{a:1},{a:2},{a:1}], [{a:1}], false, 'objects | a:1');
test(['a','b','c'], [function(e) { return e.length > 1; }], true, 'alphabet | length is greater than 1');
test(['a','b','c'], [function(e) { return e.length < 2; }], false, 'alphabet | length is less than 2');
test(['a','bar','cat'], [function(e) { return e.length < 2; }], false, 'a,bar,cat | length is less than 2');
test([{a:1},{a:2},{a:1}], [function(e) { return e['a'] == 1; }], false, 'objects | key "a" is 1');
test([{a:1},{a:2},{a:1}], [function(e) { return e['b'] == 1; }], true, 'objects | key "b" is 1');
raisesError(function() { run([1,2,3], 'none'); }, 'no argument raises a TypeError');
test(threeUndefined, oneUndefined, false, 'undefined should match all undefined');
test(threeUndefined, [null], true, 'null should not match all undefined');
test(undefinedWithNull, oneUndefined, false, 'undefined should match one undefined');
test(undefinedWithNull, [null], false, 'null should match one null');
test([null, null], [null], false, 'null should match all null');
test([null, null], oneUndefined, true, 'undefined should not match all null');
var people = [
{ name: 'jim', age: 27, hair: 'brown' },
{ name: 'mary', age: 52, hair: 'blonde' },
{ name: 'ronnie', age: 13, hair: 'brown' },
{ name: 'edmund', age: 27, hair: 'blonde' },
{ name: 'buddy', age: 82, hair: { color: 'red', type: 'long', cost: 15, last_cut: new Date(2010, 4, 18) } }
];
test(people, [{ age: 27 }], false, 'complex | one property');
test(people, [{ age: 27, hair: 'brown' }], false, 'complex | two properties');
test(people, [{ hair: { color: 'red' }}], false, 'complex | nested property');
test(people, [{ hair: { color: 'green' }}], true, 'complex | non-matching nested property');
test(people, [{ hair: { color: 'red', type: 'long' }}], false, 'complex | two nested properties');
test(people, [{ hair: { color: 'green', type: 'mean' }}], true, 'complex | two non-matching nested properties');
test(people, [{ hair: { color: 'red', type: 'mean' }}], true, 'complex | two nested properties, one non-matching');
test(people, [{ hair: { color: 'red', life: 'long' }}], true, 'complex | two nested properties, one non-existing');
test(people, [{ hair: { color: /r/ }}], false, 'complex | nested regex');
test(people, [{ hair: { cost: 15 }}], false, 'complex | nested number');
test(people, [{ hair: { cost: 23 }}], true, 'complex | nested non-matching number');
test(people, [{ hair: { cost: undefined }}], true, 'complex | nested undefined property');
test(people, [{ hair: { cost: NaN }}], true, 'complex | nested property is NaN');
test(people, [{ hair: { color: function(c){ return c == 'red'; } }}], false, 'complex | nested function');
test(people, [{ none: { random: { shit: {}}}}], true, 'complex | totally unrelated properties');
test(people, [{ hair: { last_cut: new Date(2010, 4, 18) }}], false, 'complex | simple date');
});
group('Fuzzy Matching', function() {
var arr = [{name: 'joe', age: 25}];
var match = { name: /j/ };
equal(run(arr, 'every', [match]), true, 'every');
equal(run(arr, 'some', [match]), true, 'some');
equal(run(arr, 'none', [match]), false, 'none');
equal(run(arr, 'count', [match]), 1, 'count');
equal(run(arr, 'filter', [match]), [arr[0]], 'filter');
equal(run(arr, 'find', [match]), arr[0], 'find');
equal(run(arr, 'findIndex', [match]), 0, 'findIndex');
});
group('Array Inheritance', function() {
var count;
// Inherits from array...
var Soup = function() {}, x;
Soup.prototype = [1,2,3];
x = new Soup();
count = 0;
run(x, 'forEachFromIndex', [0, function() {
count++;
}]);
run(x, 'findFromIndex', [0, function() {
count++;
}]);
run(x, 'filterFromIndex', [0, function() {
count++;
}]);
equal(count, 9, 'array elements in the prototype chain are also properly iterated');
// Inherits from sparse array...
var arr = ['a'];
arr[20] = 'b';
Soup.prototype = arr;
x = new Soup();
count = 0;
run(x, 'forEachFromIndex', [0, function() {
count++;
}]);
equal(count, 2, 'sparse array elements in the prototype chain are also properly iterated');
// This test cannot be framed in a meaninful way... IE will not set the length property
// when pushing new elements and other browsers will not work on sparse arrays...
// equal(count, 6, 'Array | objects that inherit from arrays can still iterate');
});
method('forEachFromIndex', function() {
var xyz = ['x','y','z'];
function assertForEachFromIndex(arr, args, expectedEls, expectedIdx) {
var els = [], idx = [];
var fn = function(el, i) {
els.push(el);
idx.push(i);
}
args.push(fn);
run(arr, 'forEachFromIndex', args);
equal(els, expectedEls, 'forEachFromIndex | els');
equal(idx, expectedIdx, 'forEachFromIndex | idx');
}
// Return value
equal(run(xyz, 'forEachFromIndex', [0, function(){}]), undefined, 'Should return the same as forEach');
// Errors
raisesError(function() { run(xyz, 'forEachFromIndex', []); }, 'error with no args', TypeError);
raisesError(function() { run(xyz, 'forEachFromIndex', [4]); }, 'error with only start index', TypeError);
// No looping, pos index
assertForEachFromIndex(xyz, [0], ['x','y','z'], [0, 1, 2]);
assertForEachFromIndex(xyz, [1], ['y','z'], [1, 2]);
assertForEachFromIndex(xyz, [2], ['z'], [2]);
assertForEachFromIndex(xyz, [3], [], []);
// No looping, neg index
assertForEachFromIndex(xyz, [-1], ['z'], [2]);
assertForEachFromIndex(xyz, [-2], ['y','z'], [1, 2]);
assertForEachFromIndex(xyz, [-3], ['x','y','z'], [0, 1, 2]);
assertForEachFromIndex(xyz, [-4], ['x','y','z'], [0, 1, 2]);
// No looping explicit, pos index
assertForEachFromIndex(xyz, [0, false], ['x','y','z'], [0, 1, 2]);
assertForEachFromIndex(xyz, [1, false], ['y','z'], [1, 2]);
assertForEachFromIndex(xyz, [2, false], ['z'], [2]);
assertForEachFromIndex(xyz, [3, false], [], []);
// No looping explicit, neg index
assertForEachFromIndex(xyz, [-1, false], ['z'], [2]);
assertForEachFromIndex(xyz, [-2, false], ['y','z'], [1, 2]);
assertForEachFromIndex(xyz, [-3, false], ['x','y','z'], [0, 1, 2]);
assertForEachFromIndex(xyz, [-4, false], ['x','y','z'], [0, 1, 2]);
// Looping, pos index
assertForEachFromIndex(xyz, [0, true], ['x','y','z'], [0, 1, 2]);
assertForEachFromIndex(xyz, [1, true], ['y','z','x'], [1, 2, 0]);
assertForEachFromIndex(xyz, [2, true], ['z','x','y'], [2, 0, 1]);
assertForEachFromIndex(xyz, [3, true], ['x','y','z'], [0, 1, 2]);
assertForEachFromIndex(xyz, [4, true], ['x','y','z'], [0, 1, 2]);
// Looping, neg index
assertForEachFromIndex(xyz, [-1, true], ['z','x','y'], [2, 0, 1]);
assertForEachFromIndex(xyz, [-2, true], ['y','z','x'], [1, 2, 0]);
assertForEachFromIndex(xyz, [-3, true], ['x','y','z'], [0, 1, 2]);
assertForEachFromIndex(xyz, [-4, true], ['x','y','z'], [0, 1, 2]);
// Args
assertFromIndexArgs(['x'], 'forEach', [0], ['x', 0]);
assertFromIndexArgs(['x','y'], 'forEach', [1], ['y', 1]);
// Passing context value
var fn = function() { actualContext = this;}, actualContext, fakeContext = {};
run([1], 'forEachFromIndex', [0, fn, fakeContext]);
equal(actualContext, fakeContext, 'Context should be passable');
// Moved from Array#each
var arr = [2, 5, 9];
var fn = function(el, i, a) {
equal(el, arr[i], 'looping successfully');
};
run(arr, 'forEachFromIndex', [0, fn]);
var arr = ['a', [1], { foo: 'bar' }, 352];
var count = 0;
var fn = function() {
count++;
};
run(arr, 'forEachFromIndex', [0, fn]);
equal(count, 4, 'complex array | should have looped 4 times');
var fn = function(el, i, a) {
equal(el, 'a', 'first parameter is the element');
equal(i, 0, 'second parameter is the index');
equal(a, ['a'], 'third parameter is the array');
equal(this, 'this', 'scope is also the array');
};
run(['a'], 'forEachFromIndex', [0, fn, 'this']);
var count = 0;
run({'0':'a','length':'1'}, 'forEachFromIndex', [0, function() { count++; }]);
equal(count, 1, 'looping over array-like objects with string lengths');
var result = [];
var count = 0;
var fn = function(s, i) {
result.push(s);
equal(i, count + 1, 'index should be correct');
count++;
}
run(['a','b','c'], 'forEachFromIndex', [1, fn]);
equal(count, 2, 'should have run 2 times');
equal(result, ['b','c'], 'result');
var result = [];
var indexes = [1,2,0];
var count = 0;
var fn = function(s, i) {
result.push(s);
equal(i, indexes[count], 'looping from index 1 | index should be correct');
count++;
}
run(['a','b','c'], 'forEachFromIndex', [1, true, fn]);
equal(count, 3, 'looping from index 1 | should have run 3 times')
equal(result, ['b','c','a'], 'looping from index 1 | result');
var result = [];
var indexes = [0,1,2];
var count = 0;
var fn = function(s, i) {
result.push(s);
equal(i, indexes[count], 'looping from index 0 | index should be correct')
count++;
}
run(['a','b','c'], 'forEachFromIndex', [0, true, fn]);
equal(count, 3, 'looping from index 0 | should have run 3 times')
equal(result, ['a','b','c'], 'looping from index 0 | result');
var result = [];
var indexes = [2,0,1];
var count = 0;
var fn = function(s, i) {
result.push(s);
equal(i, indexes[count], 'looping from index 2 | index should be correct');
count++;
}
run(['a','b','c'], 'forEachFromIndex', [2, true, fn]);
equal(count, 3, 'looping from index 2 | should have run 3 times')
equal(result, ['c','a','b'], 'looping from index 2 | result');
var result = [];
var count = 0;
var fn = function(s, i) {
result.push(s);
count++;
}
run(['a','b','c'], 'forEachFromIndex', [3, true, fn]);
equal(count, 3, 'looping from index 3 | should have run 3 times')
equal(result, ['a','b','c'], 'looping from index 3 | result');
var result = [];
var count = 0;
var fn = function(s, i) {
result.push(s);
count++;
}
run(['a','b','c'], 'forEachFromIndex', [4, true, fn]);
equal(count, 3, 'looping from index 4 | should have run 3 times')
equal(result, ['a','b','c'], 'looping from index 4 | result');
var result = [];
var count = 0;
var fn = function(s, i) {
result.push(s);
count++;
}
run(['a','b','c'], 'forEachFromIndex', [49, true, fn]);
equal(count, 3, 'looping from index 49 | should have run 3 times')
equal(result, ['a','b','c'], 'looping from index 49 | result');
var result = [];
var count = 0;
var fn = function(s, i) {
result.push(s);
count++;
}
run(['a','b','c'], 'forEachFromIndex', [0, fn, 'hoofa']);
equal(count, 3, 'string index should default to 0 | should have run 3 times')
equal(result, ['a','b','c'], 'string index should default to 0 | result');
// Sparse array handling
var arr = ['a'];
arr[Math.pow(2,32) - 2] = 'b';
var expectedValues = ['a','b'];
var expectedIndexes = [0, Math.pow(2,32) - 2];
var count = 0;
var fn = function(el, i, a) {
equal(this, testNullScope, 'sparse | this object should be default');
equal(el, expectedValues[count], 'sparse | first argument should be the current element');
equal(i, expectedIndexes[count], 'sparse | second argument should be the current index');
equal(a, arr, 'sparse | third argument should be the array');
count++;
}
run(arr, 'forEachFromIndex', [0, fn]);
equal(count, 2, 'sparse | count should match');
var arr = [];
arr[-2] = 'd';
arr[2] = 'f';
arr[Math.pow(2, 32)] = 'c';
var count = 0;
var fn = function(el, i) {
equal(el, 'f', 'sparse | values outside range are not iterated over | el');
equal(i, 2, 'sparse | values outside range are not iterated over | index');
count++;
}
run(arr, 'forEachFromIndex', [0, fn]);
equal(count, 1, 'sparse | values outside range are not iterated over | count');
var arr = [];
arr[9] = 'd';
arr[2] = 'f';
arr[5] = 'c';
var count = 0;
var values = [];
var indexes = [];
var expectedValues = ['f','c','d'];
var expectedIndexes = [2,5,9];
fn = function(val, i) {
values.push(val);
indexes.push(i);
}
run(arr, 'forEachFromIndex', [0, fn]);
equal(values, expectedValues, 'sparse | unordered should produce correct values');
equal(indexes, expectedIndexes, 'sparse | unordered should produce correct indexes');
var arr = [];
arr[9] = 'd';
arr[2] = 'f';
arr[5] = 'c';
var values = [];
var indexes = [];
var expectedValues = ['d','f','c'];
var expectedIndexes = [9,2,5];
var fn = function(val, i) {
values.push(val);
indexes.push(i);
}
run(arr, 'forEachFromIndex', [7, true, fn]);
equal(values, expectedValues, 'sparse | looping should return correct values');
equal(indexes, expectedIndexes, 'sparse | looping should return correct indexes');
var count = 0;
var fn = function() {
count++;
}
run(threeUndefined, 'forEachFromIndex', [0, fn]);
equal(count, 3, 'simply having an undefined in an array does not qualify it as sparse');
});
method('mapFromIndex', function() {
var arr = [
{ name: 'John' },
{ name: 'Karen' },
{ name: 'Marty' }
];
// No looping, pos index
assertFromIndex(arr, 'map', [0, 'name'], ['John', 'Karen', 'Marty']);
assertFromIndex(arr, 'map', [1, 'name'], ['Karen', 'Marty']);
assertFromIndex(arr, 'map', [2, 'name'], ['Marty']);
assertFromIndex(arr, 'map', [3, 'name'], []);
assertFromIndex(arr, 'map', [4, 'name'], []);
// No looping, neg index
assertFromIndex(arr, 'map', [-1, 'name'], ['Marty']);
assertFromIndex(arr, 'map', [-2, 'name'], ['Karen', 'Marty']);
assertFromIndex(arr, 'map', [-3, 'name'], ['John', 'Karen', 'Marty']);
assertFromIndex(arr, 'map', [-4, 'name'], ['John', 'Karen', 'Marty']);
// Looping, pos index
assertFromIndex(arr, 'map', [0, true, 'name'], ['John', 'Karen', 'Marty']);
assertFromIndex(arr, 'map', [1, true, 'name'], ['Karen', 'Marty', 'John']);
assertFromIndex(arr, 'map', [2, true, 'name'], ['Marty', 'John', 'Karen']);
assertFromIndex(arr, 'map', [3, true, 'name'], ['John', 'Karen', 'Marty']);
assertFromIndex(arr, 'map', [4, true, 'name'], ['John', 'Karen', 'Marty']);
// Looping, neg index
assertFromIndex(arr, 'map', [-1, true, 'name'], ['Marty', 'John', 'Karen']);
assertFromIndex(arr, 'map', [-2, true, 'name'], ['Karen', 'Marty', 'John']);
assertFromIndex(arr, 'map', [-3, true, 'name'], ['John', 'Karen', 'Marty']);
assertFromIndex(arr, 'map', [-4, true, 'name'], ['John', 'Karen', 'Marty']);
// Function
assertFromIndex(arr, 'map', [0, function(el) { return el.name; }], ['John', 'Karen', 'Marty']);
assertFromIndex(arr, 'map', [1, function(el) { return el.name; }], ['Karen', 'Marty']);
assertFromIndex(arr, 'map', [2, function(el) { return el.name; }], ['Marty']);
assertFromIndex(arr, 'map', [3, function(el) { return el.name; }], []);
});
method('filterFromIndex', function() {
var xyz = ['x','y','z'];
// No looping, pos index
assertFromIndex(xyz, 'filter', [0, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [1, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [2, 'y'], []);
assertFromIndex(xyz, 'filter', [3, 'y'], []);
assertFromIndex(xyz, 'filter', [4, 'y'], []);
// No looping, neg index
assertFromIndex(xyz, 'filter', [-1, 'y'], []);
assertFromIndex(xyz, 'filter', [-2, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [-3, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [-4, 'y'], ['y']);
// Looping, pos index
assertFromIndex(xyz, 'filter', [0, true, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [1, true, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [2, true, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [3, true, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [4, true, 'y'], ['y']);
// Looping, neg index
assertFromIndex(xyz, 'filter', [-1, true, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [-2, true, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [-3, true, 'y'], ['y']);
assertFromIndex(xyz, 'filter', [-4, true, 'y'], ['y']);
// Looping, pos index, does not exist
assertFromIndex(xyz, 'filter', [0, true, 'q'], []);
assertFromIndex(xyz, 'filter', [1, true, 'q'], []);
assertFromIndex(xyz, 'filter', [2, true, 'q'], []);
assertFromIndex(xyz, 'filter', [3, true, 'q'], []);
assertFromIndex(xyz, 'filter', [4, true, 'q'], []);
// Looping, neg index, does not exist
assertFromIndex(xyz, 'filter', [-1, true, 'q'], []);
assertFromIndex(xyz, 'filter', [-2, true, 'q'], []);
assertFromIndex(xyz, 'filter', [-3, true, 'q'], []);
assertFromIndex(xyz, 'filter', [-4, true, 'q'], []);
// Regex
assertFromIndex(xyz, 'filter', [0, /[xy]/], ['x', 'y']);
assertFromIndex(xyz, 'filter', [1, /[xy]/], ['y']);
assertFromIndex(xyz, 'filter', [2, /[xy]/], []);
assertFromIndex(xyz, 'filter', [3, /[xy]/], []);
// Function
assertFromIndex(xyz, 'filter', [0, function(el) { return el === 'y'; }], ['y']);
assertFromIndex(xyz, 'filter', [1, function(el) { return el === 'y'; }], ['y']);
assertFromIndex(xyz, 'filter', [2, function(el) { return el === 'y'; }], []);
assertFromIndex(xyz, 'filter', [3, function(el) { return el === 'y'; }], []);