-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
63 lines (56 loc) · 1.22 KB
/
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
import test from 'ava';
import pFilter, {pFilterIterable} from './index.js';
// See `p-map` for more comprehensive tests
test('main', async t => {
t.deepEqual(
await pFilter([Promise.resolve(1), 2, 3, 4], x => x % 2),
[1, 3],
);
t.deepEqual(
await pFilter([1, 2, 3, 4], x => Promise.resolve(x % 2)),
[1, 3],
);
});
test('handles empty iterable', async t => {
t.deepEqual(await pFilter([]), []);
});
test('pFilterIterable', async t => {
const iterableToArray = async iterable => {
const array = [];
for await (const item of iterable) {
array.push(item);
}
return array;
};
const rangeIterable = {
async * [Symbol.asyncIterator]() {
yield 1;
yield 2;
yield 3;
yield 4;
},
};
t.deepEqual(
await iterableToArray(pFilterIterable(rangeIterable, x => x % 2)),
[1, 3],
);
t.deepEqual(
await iterableToArray(
pFilterIterable(rangeIterable, x => Promise.resolve(x % 2)),
),
[1, 3],
);
t.deepEqual(
await iterableToArray(
pFilterIterable([Promise.resolve(1), 2, 3, 4], x => x % 2),
),
[1, 3],
);
t.deepEqual(
await iterableToArray(
pFilterIterable([1, 2, 3, 4], x => Promise.resolve(x % 2)),
),
[1, 3],
);
t.deepEqual(await iterableToArray(pFilterIterable([])), []);
});