-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.spec.ts
52 lines (46 loc) · 1.36 KB
/
filter.spec.ts
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
import { expectType } from '#/utils'
import { concurrent, delay, each, filter, map, max, pipe, toAsync } from 'fx-flow'
expectType<Iterable<number>>(filter((v): v is number => v !== undefined, [1, 2, undefined]))
expectType<Iterable<number | undefined>>(filter((v) => v !== undefined, [1, 2, undefined]))
expectType<AsyncIterable<number>>(filter((v): v is number => v !== undefined, toAsync([1, 2, undefined])))
describe('filter', () => {
describe('sync', () => {
it('should be able to filter iterable', () => {
expect(
pipe(
[1, 2, 3, 4],
filter((v) => v < 2),
max
)
).toEqual(1)
})
it('should return a function if "iterable" is not passed', () => {
expect(typeof filter(() => true)).toEqual('function')
})
})
describe('async', () => {
it('should be able to work with asyncIterable', () => {
pipe(
[1, 2, 3, 4],
toAsync,
filter((v) => v < 2),
max
).then((res) => {
expect(res).toEqual(1)
})
})
it('should be able to work with concurrent', () => {
let sum = 0
pipe(
[1, 2, 3, 4, 5, 6, 7, 8],
toAsync,
filter((v) => v < 5),
map((v) => delay(10, v)),
concurrent(2),
each((v) => (sum = sum + v))
).then(() => {
expect(sum).toEqual(10)
})
})
})
})