-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
720 lines (641 loc) · 19.5 KB
/
index.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
'use strict';
const test = require('ava');
const { forEach, map, find, findIndex, some, every, filter, reduce } = require('../');
const { forEachSeries, mapSeries, findSeries, findIndexSeries, someSeries, everySeries, filterSeries } = require('../');
const { asyncForEach } = require('../').instanceMethods;
const delay = (ms) => new Promise(resolve => setTimeout(() => resolve(ms), ms || 0));
test('forEach, check callbacks are run in parallel', async (t) => {
let total = 0;
const parallelCheck = [];
await forEach([2, 1, 3], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
parallelCheck.push(num);
total += num;
});
t.deepEqual(parallelCheck, [1, 2, 3]);
t.is(total, 6);
});
test('forEach passing a non-async function', async (t) => {
let total = 0;
await forEach([2, 1, 3], (num, index, array) => {
t.is(array[index], num);
total += num;
});
t.is(total, 6);
});
test('forEach unwraps Promises in the array', async (t) => {
let total = 0;
await forEach([2, Promise.resolve(1), 3], async (num, index, array) => {
t.is(await Promise.resolve(array[index]), num);
total += num;
});
t.is(total, 6);
});
test('forEach should execute callbacks as soon as Promises are unwrapped', async (t) => {
const parallelCheck = [];
await forEach([delay(500), delay(300), delay(400)], (num) => {
parallelCheck.push(num);
});
t.deepEqual(parallelCheck, [300, 400, 500]);
});
test('forEach, throw inside callback', async function (t) {
const err = await t.throwsAsync(forEach([2, 1, 3], () => {
throw new Error('test');
}));
t.is(err.message, 'test');
});
test('forEach using thisArg', async (t) => {
let total = 0;
const testObj = { test: 1 };
await forEach([1, 2, 3], async function (num, index, array) {
await delay();
t.is(array[index], num);
t.deepEqual(this, testObj);
total += num;
}, testObj);
t.is(total, 6);
});
test.cb('forEach used with promises in a non-async function', (t) => {
let total = 0;
forEach([1, 2, 3], async function (num, index, array) {
await delay();
t.is(array[index], num);
total += num;
}).then(() => {
t.is(total, 6);
t.end();
});
});
test('forEach should not execute any callback if array is empty', async (t) => {
let count = 0;
await forEach([], async () => {
await delay();
count++;
});
t.is(count, 0);
});
test('forEach should skip holes in arrays', async (t) => {
let count = 0;
// eslint-disable-next-line no-sparse-arrays
await forEach([0, 1, 2, , 5, ,], async () => {
count++;
});
t.is(count, 4);
});
test('map, check callbacks are run in parallel', async (t) => {
const parallelCheck = [];
const arr = await map([3, 1, 2], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
parallelCheck.push(num);
return num * 2;
});
t.deepEqual(arr, [6, 2, 4]);
t.deepEqual(parallelCheck, [1, 2, 3]);
});
test('map unwraps Promises in the array', async (t) => {
const arr = await map([2, Promise.resolve(1), 3], async (num, index, array) => {
t.is(await Promise.resolve(array[index]), num);
return num * 2;
});
t.deepEqual(arr, [4, 2, 6]);
});
test('map should execute callbacks as soon as Promises are unwrapped', async (t) => {
const parallelCheck = [];
await map([delay(500), delay(300), delay(400)], (num) => {
parallelCheck.push(num);
});
t.deepEqual(parallelCheck, [300, 400, 500]);
});
test('map passing a non-async function that return a promise', async (t) => {
const arr = await map([1, 2, 3], (num) => Promise.resolve(num * 2));
t.deepEqual(arr, [2, 4, 6]);
});
test('map, throw inside callback', async function (t) {
const err = await t.throwsAsync(map([2, 1, 3], () => {
throw new Error('test');
}));
t.is(err.message, 'test');
});
test.cb('map used with promises in a non-async function', (t) => {
map([1, 2, 3], async function (num) {
await delay();
return num * 2;
}).then((result) => {
t.deepEqual(result, [2, 4, 6]);
t.end();
});
});
test('map should return an empty array if passed array is empty', async (t) => {
const count = 0;
const arr = await map([], async () => {
await delay();
return 3;
});
t.deepEqual(arr, []);
t.deepEqual(count, 0);
});
test('map should skip holes in arrays', async (t) => {
let count = 0;
// eslint-disable-next-line no-sparse-arrays
await map([0, 1, 2, , 5, ,], async () => {
count++;
});
t.is(count, 4);
});
test('find', async (t) => {
const foundNum = await find([1, 2, 3], async (num, index, array) => {
await delay();
t.is(array[index], num);
return num === 2;
});
t.is(foundNum, 2);
});
test('find, throw inside callback', async function (t) {
const err = await t.throwsAsync(find([2, 1, 3], () => {
throw new Error('test');
}));
t.is(err.message, 'test');
});
test('find returns undefined if did not find anything', async (t) => {
const foundNum = await find([1, 2], async () => {
await delay();
return false;
});
t.is(foundNum, undefined);
});
test('find returns undefined if array is empty', async (t) => {
const foundNum = await find([], async () => {
await delay();
return false;
});
t.is(foundNum, undefined);
});
test('find unwraps Promises in the array', async (t) => {
const foundNum = await find([1, Promise.resolve(2), 3], async (num, index, array) => {
await delay();
t.is(await Promise.resolve(array[index]), num);
return num === 2;
});
t.is(foundNum, 2);
});
test('find should execute callbacks as soon as Promises are unwrapped', async (t) => {
const parallelCheck = [];
await find([delay(500), delay(300), delay(400)], (num) => {
parallelCheck.push(num);
});
t.deepEqual(parallelCheck, [300, 400, 500]);
});
test('find passing a non-async callback', async (t) => {
const foundNum = await find([1, 2, 3], (num, index, array) => {
t.is(array[index], num);
return num === 2;
});
t.is(foundNum, 2);
});
test('find should not skip holes in arrays', async (t) => {
let count = 0;
// eslint-disable-next-line no-sparse-arrays
await find([0, 1, 2, , 5, ,], async () => {
count++;
});
t.is(count, 6);
});
test('findIndex', async (t) => {
const foundIndex = await findIndex([1, 2, 3], async (num, index, array) => {
await delay();
t.is(array[index], num);
return num === 2;
});
t.is(foundIndex, 1);
});
test('findIndex, throw inside callback', async function (t) {
const err = await t.throwsAsync(findIndex([2, 1, 3], () => {
throw new Error('test');
}));
t.is(err.message, 'test');
});
test('findIndex returns -1 if did not find anything', async (t) => {
const notFound = await findIndex([1, 2], async () => {
await delay();
return false;
});
t.is(notFound, -1);
});
test('findIndex returns -1 if array is empty', async (t) => {
const notFound = await findIndex([], async () => {
await delay();
return false;
});
t.is(notFound, -1);
});
test('findIndex unwraps Promises in the array', async (t) => {
const foundIndex = await findIndex([Promise.resolve(1), 2, 3], async (num, index, array) => {
await delay();
t.is(await Promise.resolve(array[index]), num);
return num === 2;
});
t.is(foundIndex, 1);
});
test('findIndex should execute callbacks as soon as Promises are unwrapped', async (t) => {
const parallelCheck = [];
await findIndex([delay(500), delay(300), delay(400)], (num) => {
parallelCheck.push(num);
});
t.deepEqual(parallelCheck, [300, 400, 500]);
});
test('findIndex should not skip holes in arrays', async (t) => {
let count = 0;
// eslint-disable-next-line no-sparse-arrays
await findIndex([0, 1, 2, , 5, ,], async () => {
count++;
});
t.is(count, 6);
});
test('some', async (t) => {
const isIncluded = await some([1, 2, 3], async (num, index, array) => {
await delay();
t.is(array[index], num);
return num === 3;
});
t.true(isIncluded);
});
test('some, throw inside callback', async function (t) {
const err = await t.throwsAsync(some([2, 1, 3], () => {
throw new Error('test');
}));
t.is(err.message, 'test');
});
test('some should iterate until the end', async (t) => {
const isIncluded = await some([500, 200, 400], async (num, index, array) => {
await delay(num);
t.is(array[index], num);
return num === 500;
});
t.true(isIncluded);
});
test('some unwraps Promises in the array', async (t) => {
const isIncluded = await some([1, Promise.resolve(2), 3], async (num, index, array) => {
await delay();
t.is(await Promise.resolve(array[index]), num);
return num === 3;
});
t.true(isIncluded);
});
test('some should execute callbacks as soon as Promises are unwrapped', async (t) => {
const parallelCheck = [];
await some([delay(500), delay(300), delay(400)], (num) => {
parallelCheck.push(num);
});
t.deepEqual(parallelCheck, [300, 400, 500]);
});
test('some passing a non-async callback', async (t) => {
const isIncluded = await some([1, 2, 3], (num, index, array) => {
t.is(array[index], num);
return num === 3;
});
t.true(isIncluded);
});
test('some (return false)', async (t) => {
const isIncluded = await some([1, 2, 3], async (num, index, array) => {
await delay();
t.is(array[index], num);
return num === 4;
});
t.false(isIncluded);
});
test('some with empty array should return false', async (t) => {
const isIncluded = await some([], async () => {
await delay();
return false;
});
t.false(isIncluded);
});
test('some should skip holes in arrays', async (t) => {
let count = 0;
// eslint-disable-next-line no-sparse-arrays
await some([0, 1, 2, , 5, ,], async () => {
count++;
});
t.is(count, 4);
});
test('every', async (t) => {
const allIncluded = await every([1, 2, 3], async (num, index, array) => {
await delay();
t.is(array[index], num);
return typeof num === 'number';
});
t.true(allIncluded);
});
test('every, throw inside callback', async function (t) {
const err = await t.throwsAsync(every([2, 1, 3], () => {
throw new Error('test');
}));
t.is(err.message, 'test');
});
test('every should iterate until the end', async (t) => {
const isIncluded = await every([500, 200, 400], async (num, index, array) => {
await delay(num);
t.is(array[index], num);
return true;
});
t.true(isIncluded);
});
test('every unwraps Promises in the array', async (t) => {
const allIncluded = await every([Promise.resolve(1), 2, 3], async (num, index, array) => {
await delay();
t.is(await Promise.resolve(array[index]), num);
return typeof num === 'number';
});
t.true(allIncluded);
});
test('every should execute callbacks as soon as Promises are unwrapped', async (t) => {
const parallelCheck = [];
await every([delay(500), delay(300), delay(400)], (num) => {
parallelCheck.push(num);
return true;
});
t.deepEqual(parallelCheck, [300, 400, 500]);
});
test('every passing a non-async callback', async (t) => {
const allIncluded = await every([1, 2, 3], (num, index, array) => {
t.is(array[index], num);
return typeof num === 'number';
});
t.true(allIncluded);
});
test('every (return false)', async (t) => {
const allIncluded = await every([1, 2, '3'], async (num, index, array) => {
await delay();
t.is(array[index], num);
return typeof num === 'number';
});
t.false(allIncluded);
});
test('every with empty array should return true', async (t) => {
const allIncluded = await every([], async () => {
await delay();
return false;
});
t.true(allIncluded);
});
test('every should skip holes in arrays', async (t) => {
let count = 0;
// eslint-disable-next-line no-sparse-arrays
const allIncluded = await every([0, 1, 2, , 5, ,], async () => {
count++;
return true;
});
t.is(allIncluded, true);
t.is(count, 4);
});
test('filter', async (t) => {
const numbers = await filter([2, 1, '3', 4, '5'], async (num) => {
await delay(num * 100);
return typeof num === 'number';
});
t.deepEqual(numbers, [2, 1, 4]);
});
test('filter should skip holes in arrays', async (t) => {
let count = 0;
// eslint-disable-next-line no-sparse-arrays
const numbers = await filter([0, 1, 2, '3', , 5, '6', ,], async (num) => {
await delay(num * 100);
count++;
return typeof num === 'number';
});
t.is(count, 6);
t.deepEqual(numbers, [0, 1, 2, 5]);
});
test('filter, check callbacks are run in parallel', async (t) => {
const parallelCheck = [];
const numbers = await filter([2, 1, '3'], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
if (typeof num === 'number') {
parallelCheck.push(num);
return true;
}
});
t.deepEqual(parallelCheck, [1, 2]);
t.deepEqual(numbers, [2, 1]);
});
test('filter, throw inside callback', async function (t) {
const err = await t.throwsAsync(filter([2, 1, 3], () => {
throw new Error('test');
}));
t.is(err.message, 'test');
});
test('filter unwraps Promises in the array', async (t) => {
const parallelCheck = [];
const numbers = await filter([Promise.resolve(2), 1, '3'], async (num, index, array) => {
await delay(num * 100);
t.is(await Promise.resolve(array[index]), num);
if (typeof num === 'number') {
parallelCheck.push(num);
return true;
}
});
t.deepEqual(parallelCheck, [1, 2]);
t.deepEqual(numbers, [2, 1]);
});
test('filter should execute callbacks as soon as Promises are unwrapped', async (t) => {
const parallelCheck = [];
await filter([delay(500), delay(300), delay(400)], (num) => {
parallelCheck.push(num);
});
t.deepEqual(parallelCheck, [300, 400, 500]);
});
test('filter should return an empty array if passed array is empty', async (t) => {
let count = 0;
const empty = await filter([], async () => {
await delay();
count++;
return true;
});
t.deepEqual(count, 0);
t.deepEqual(empty, []);
});
test('reduce with initialValue', async (t) => {
const sum = await reduce([1, 2, 3], async (accumulator, currentValue, index, array) => {
await delay();
t.is(array[index], currentValue);
return accumulator + currentValue;
}, 1);
t.is(sum, 7);
});
test('reduce with falsy initialValue', async (t) => {
const sum = await reduce(['1', '2', '3'], async (accumulator, currentValue, index, array) => {
await delay();
t.is(array[index], currentValue);
return accumulator + Number(currentValue);
}, 0);
t.is(sum, 6);
const string = await reduce([1, 2, 3], async (accumulator, currentValue, index, array) => {
await delay();
t.is(array[index], currentValue);
return accumulator + String(currentValue);
}, '');
t.is(string, '123');
const somePositive = await reduce([-1, 2, 3], async (accumulator, currentValue, index, array) => {
await delay();
t.is(array[index], currentValue);
return accumulator ? accumulator : currentValue > 0;
}, false);
t.is(somePositive, true);
});
test('reduce, throw inside callback', async function (t) {
const err = await t.throwsAsync(reduce([2, 1, 3], () => {
throw new Error('test');
}));
t.is(err.message, 'test');
});
test('reduce unwrap Promises in the array', async (t) => {
const sum = await reduce([Promise.resolve(1), 2, 3], async (accumulator, currentValue, index, array) => {
await delay();
t.is(await Promise.resolve(array[index]), currentValue);
return accumulator + currentValue;
}, 1);
t.is(sum, 7);
});
test('reduce unwrap Promises in the initialValue', async (t) => {
const sum = await reduce([1, 2, 3], async (accumulator, currentValue, index, array) => {
await delay();
t.is(await Promise.resolve(array[index]), currentValue);
return accumulator + currentValue;
}, Promise.resolve(1));
t.is(sum, 7);
});
test('reduce without initialValue', async (t) => {
const sum = await reduce([1, 2, 3], async (accumulator, currentValue, index, array) => {
await delay();
t.is(array[index], currentValue);
return accumulator + currentValue;
});
t.is(sum, 6);
});
test('reduce of array with two elements without initialValue', async (t) => {
const sum = await reduce([1, 2], async (accumulator, currentValue, index, array) => {
await delay();
t.is(array[index], currentValue);
return accumulator + currentValue;
});
t.is(sum, 3);
});
test('reduce of empty array without initialValue should throw TypeError', async (t) => {
const err = await t.throwsAsync(
reduce([], async (accumulator, currentValue) => {
await delay();
return accumulator + currentValue;
})
);
t.is(err.name, 'TypeError');
t.is(err.message, 'Reduce of empty array with no initial value');
});
test('reduce of empty array with initialValue should return initialValue', async (t) => {
let count = 0;
const sum = await reduce([], async (accumulator, currentValue) => {
await delay();
count++;
return accumulator + currentValue;
}, 6);
t.is(count, 0);
t.is(sum, 6);
});
test('reduce of array with one element and no initialValue should return that element', async (t) => {
let count = 0;
const sum = await reduce([6], async (accumulator, currentValue) => {
await delay();
count++;
return accumulator + currentValue;
});
t.is(count, 0);
t.is(sum, 6);
});
test('forEachSeries', async (t) => {
let total = 0;
const seriesCheck = [];
await forEachSeries([2, 1, 3], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
seriesCheck.push(num);
total += num;
});
t.deepEqual(seriesCheck, [2, 1, 3]);
t.is(total, 6);
});
test('asyncForEach', async (t) => {
let total = 0;
await asyncForEach.call([2, 1, 3], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
total += num;
});
t.is(total, 6);
});
test('mapSeries', async (t) => {
const seriesCheck = [];
const arr = await mapSeries([3, 1, 2], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
seriesCheck.push(num);
return num * 2;
});
t.deepEqual(arr, [6, 2, 4]);
t.deepEqual(seriesCheck, [3, 1, 2]);
});
test('findSeries', async (t) => {
const seriesCheck = [];
const foundNum = await findSeries([3, 1, 2], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
seriesCheck.push(num);
return num === 2;
});
t.is(foundNum, 2);
t.deepEqual(seriesCheck, [3, 1, 2]);
});
test('findIndexSeries', async (t) => {
const seriesCheck = [];
const foundNum = await findIndexSeries([3, 1, 2], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
seriesCheck.push(num);
return num === 2;
});
t.is(foundNum, 2);
t.deepEqual(seriesCheck, [3, 1, 2]);
});
test('someSeries', async (t) => {
const seriesCheck = [];
const isIncluded = await someSeries([3, 1, 2], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
seriesCheck.push(num);
return num === 2;
});
t.true(isIncluded);
t.deepEqual(seriesCheck, [3, 1, 2]);
});
test('everySeries', async (t) => {
const seriesCheck = [];
const allIncluded = await everySeries([3, 1, 2], async (num, index, array) => {
await delay(num * 100);
t.is(array[index], num);
seriesCheck.push(num);
return typeof num === 'number';
});
t.true(allIncluded);
t.deepEqual(seriesCheck, [3, 1, 2]);
});
test('filterSeries', async (t) => {
const seriesCheck = [];
const numbers = await filterSeries([2, 1, '3'], async (num) => {
await delay(num * 100);
seriesCheck.push(num);
return typeof num === 'number';
});
t.deepEqual(numbers, [2, 1]);
t.deepEqual(seriesCheck, [2, 1, '3']);
});