Skip to content

Commit 4474942

Browse files
committed
Split utils into multiple files
1 parent ed42582 commit 4474942

13 files changed

+374
-307
lines changed

src/forEach.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import asyncMap from './map';
2+
3+
export default async function asyncForEach(arr, fn) {
4+
await asyncMap(arr, fn);
5+
}

src/forEach.spec.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import asyncForEach from './forEach';
2+
3+
import {
4+
inputArr,
5+
makePushDuplicate,
6+
makePushDuplicateInRandomTime,
7+
} from '../test-utils';
8+
9+
describe('asyncForEach()', () => {
10+
it.skip('assertions below are valid for synchronous .forEach()', () => {
11+
const [arr, pushDuplicate] = makePushDuplicate();
12+
const mapper = jest.fn().mockImplementation(pushDuplicate);
13+
14+
inputArr.forEach(mapper);
15+
16+
expect.assertions(1 + inputArr.length);
17+
18+
expect(mapper).toHaveBeenCalledTimes(inputArr.length);
19+
inputArr.forEach((el) => {
20+
expect(arr).toContain(el * 2);
21+
});
22+
});
23+
24+
it('iterates through an array properly', async () => {
25+
const [arr, pushDuplicate] = makePushDuplicateInRandomTime();
26+
const mapper = jest.fn().mockImplementation(pushDuplicate);
27+
28+
await asyncForEach(inputArr, mapper);
29+
30+
expect.assertions(1 + inputArr.length);
31+
32+
expect(mapper).toHaveBeenCalledTimes(inputArr.length);
33+
inputArr.forEach((el) => {
34+
expect(arr).toContain(el * 2);
35+
});
36+
});
37+
38+
it.skip('assertions below are valid for synchronous .forEach()', () => {
39+
const [, pushDuplicate] = makePushDuplicate();
40+
const mapper = jest.fn().mockImplementation(pushDuplicate);
41+
42+
const result = inputArr.forEach(mapper);
43+
44+
expect(result).toBe(undefined);
45+
});
46+
47+
it('returns undefined', async () => {
48+
const [, pushDuplicate] = makePushDuplicate();
49+
const mapper = jest.fn().mockImplementation(pushDuplicate);
50+
51+
const result = await asyncForEach(inputArr, mapper);
52+
53+
expect(result).toBe(undefined);
54+
});
55+
});

src/forEach_strict.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import asyncMapStrict from './map_strict';
2+
3+
export default async function asyncForEachStrict(arr, fn) {
4+
await asyncMapStrict(arr, fn);
5+
}

src/forEach_strict.spec.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import asyncForEachStrict from './forEach_strict';
2+
import {
3+
doubleInputArr,
4+
inputArr,
5+
makePushDuplicate,
6+
makePushDuplicateInRandomTime,
7+
} from '../test-utils';
8+
9+
describe('asyncForEachStrict()', () => {
10+
it.skip('assertions below are valid for synchronous .forEach()', () => {
11+
const [arr, pushDuplicate] = makePushDuplicate();
12+
const mapper = jest.fn().mockImplementation(pushDuplicate);
13+
14+
inputArr.forEach(mapper);
15+
16+
expect.assertions(1 + inputArr.length);
17+
18+
expect(mapper).toHaveBeenCalledTimes(inputArr.length);
19+
inputArr.forEach((el) => {
20+
expect(arr).toContain(el * 2);
21+
});
22+
});
23+
24+
it('iterates through an array properly', async () => {
25+
const [arr, pushDuplicate] = makePushDuplicateInRandomTime();
26+
const mapper = jest.fn().mockImplementation(pushDuplicate);
27+
28+
await asyncForEachStrict(inputArr, mapper);
29+
30+
expect.assertions(1 + inputArr.length);
31+
32+
expect(mapper).toHaveBeenCalledTimes(inputArr.length);
33+
inputArr.forEach((el) => {
34+
expect(arr).toContain(el * 2);
35+
});
36+
});
37+
38+
it.skip('assertions below are valid for synchronous .forEach()', () => {
39+
const [arr, pushDuplicate] = makePushDuplicate();
40+
const mapper = jest.fn().mockImplementation(pushDuplicate);
41+
42+
inputArr.forEach(mapper);
43+
44+
expect(mapper).toHaveBeenCalledTimes(inputArr.length);
45+
expect(arr).toEqual(doubleInputArr);
46+
});
47+
48+
it('iterates through an array properly with side effects', async () => {
49+
const [arr, pushDuplicate] = makePushDuplicateInRandomTime();
50+
const mapper = jest.fn().mockImplementation(pushDuplicate);
51+
52+
await asyncForEachStrict(inputArr, mapper);
53+
54+
expect(mapper).toHaveBeenCalledTimes(inputArr.length);
55+
expect(arr).toEqual(doubleInputArr);
56+
});
57+
58+
it.skip('assertions below are valid for synchronous .forEach()', () => {
59+
const [, pushDuplicate] = makePushDuplicate();
60+
const mapper = jest.fn().mockImplementation(pushDuplicate);
61+
62+
const result = inputArr.forEach(mapper);
63+
64+
expect(result).toBe(undefined);
65+
});
66+
67+
it('returns undefined', async () => {
68+
const [, pushDuplicate] = makePushDuplicate();
69+
const mapper = jest.fn().mockImplementation(pushDuplicate);
70+
71+
const result = await asyncForEachStrict(inputArr, mapper);
72+
73+
expect(result).toBe(undefined);
74+
});
75+
});

src/index.js

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,13 @@
1-
export async function asyncMap(arr, fn) {
2-
return Promise.all(arr.map(fn));
3-
}
4-
5-
export async function asyncMapStrict(arr, fn) {
6-
const result = [];
7-
8-
for (let idx = 0; idx < arr.length; idx += 1) {
9-
const cur = arr[idx];
10-
11-
// eslint-disable-next-line no-await-in-loop
12-
result.push(await fn(cur, idx, arr));
13-
}
14-
15-
return result;
16-
}
17-
18-
export async function asyncForEach(arr, fn) {
19-
await asyncMap(arr, fn);
20-
}
21-
22-
export async function asyncForEachStrict(arr, fn) {
23-
await asyncMapStrict(arr, fn);
24-
}
25-
26-
export async function asyncReduce(arr, fn, initialValue) {
27-
let temp = initialValue;
28-
29-
for (let idx = 0; idx < arr.length; idx += 1) {
30-
const cur = arr[idx];
31-
32-
// eslint-disable-next-line no-await-in-loop
33-
temp = await fn(temp, cur, idx);
34-
}
35-
36-
return temp;
37-
}
1+
import asyncForEach from './forEach';
2+
import asyncForEachStrict from './forEach_strict';
3+
import asyncMap from './map';
4+
import asyncMapStrict from './map_strict';
5+
import asyncReduce from './reduce';
6+
7+
export {
8+
asyncForEach,
9+
asyncForEachStrict,
10+
asyncMap,
11+
asyncMapStrict,
12+
asyncReduce,
13+
};

0 commit comments

Comments
 (0)