/
array.test.js
110 lines (84 loc) · 2.94 KB
/
array.test.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
'use strict';
const {mapAsync, reduceAsync, filterAsync, forEachAsync} = require('../src/array');
const waitFor = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); // eslint-disable-line no-promise-executor-return
const waitRandom = () => waitFor(Math.floor(Math.random() * Math.floor(50)));
test('async map', async () => {
const afunc = async (value) => {
await waitRandom();
return value * value;
};
const func = (value) => value * value;
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const expected = array.map((value) => func(value));
const result1 = await mapAsync(array, (v) => func(v));
const result2 = await mapAsync(array, (v) => afunc(v));
expect(result1).toEqual(expected);
expect(result2).toEqual(expected);
});
test('async map (default)', async () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const result = await mapAsync(array);
expect(result).toEqual(array);
});
test('async map (empty)', async () => {
const result = await mapAsync();
expect(result).toEqual([]);
});
test('async reduce', async () => {
const afunc = async (res, value, index) => {
await waitRandom();
return [...res, value * index];
};
const func = (res, value, index) => [...res, value * index];
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const expected = array.reduce((res, value, index) => func(res, value, index), []);
const result1 = await reduceAsync([], array, func);
const result2 = await reduceAsync([], array, afunc);
expect(result1).toEqual(expected);
expect(result2).toEqual(expected);
});
test('async reduce (default)', async () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const result = await reduceAsync(array);
expect(result).toEqual(array);
});
test('async reduce (empty)', async () => {
const result = await reduceAsync();
expect(result).toEqual(undefined);
});
test('async filter', async () => {
const afunc = async (value) => {
await waitRandom();
return value % 2;
};
const func = (value) => value % 2;
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const expected = array.filter((value) => func(value));
const result1 = await filterAsync(array, func);
const result2 = await filterAsync(array, afunc);
expect(result1).toEqual(expected);
expect(result2).toEqual(expected);
});
test('async filter (default)', async () => {
const array = [1, 0, 3, false, 5, undefined, 7, null];
const result = await filterAsync(array);
expect(result).toEqual([1, 3, 5, 7]);
});
test('async filter (empty)', async () => {
const result = await filterAsync();
expect(result).toEqual([]);
});
test('async forEach', async () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const expected = [];
const result1 = [];
const result2 = [];
array.forEach((v) => expected.push(v));
await forEachAsync(array, (v) => result1.push(v));
await forEachAsync(array, async (v) => {
await waitRandom();
result2.push(v);
});
expect(result1).toEqual(expected);
expect(result2).toEqual(expected);
});