forked from eighttrigrams/tsfun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.spec.ts
128 lines (83 loc) · 3.15 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
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
import {Associative, Map} from '../../src/type'
import {lt} from '../../src/comparator'
import {filter, map} from '../../src/associative'
import { flow } from '../../src/composition'
import {expectType} from 'ts-expect'
/**
* tsfun | filter
*/
describe('filter', () => {
it('array', () => {
expect(filter(lt(4))([2, 4, 3])).toEqual([2, 3])
expect(filter(lt(4), [2, 4, 3])).toEqual([2, 3])
expect(filter([2, 4, 3], lt(4))).toEqual([2, 3])
})
it('object', () => {
expect(filter(lt(4))({a: 2, b: 4, c: 3})).toEqual({a: 2, c: 3})
expect(filter(lt(4), {a: 2, b: 4, c: 3})).toEqual({a: 2, c: 3})
})
it('array i', () => {
expect(
filter((_, i: number) => i !== 1)([17, 19, 22]))
.toEqual([17, 22])
})
it('object with k', () => {
expect(
filter((_, k: string) => k !== 'd')({d: 3, e: 4}))
.toEqual({e: 4})
})
it('curry typing', () => {
const $1 = [1, 2]
const $2 = flow($1, filter((_: number) => true))
expectType<Array<number>>($2)
const $4 = {a:1, b: 2}
const $5 = flow($4, filter((_: number) => true))
expectType<Map<number>>($5)
const $9 = {a: 1, b: 2}
const $10 = flow($9, filter(_ => true), map(_ => _ * 2))
expectType<Map<number>>($10)
const $19 = [1, 2]
const $20 = flow($19, filter(_ => true))
expectType<Array<any>>($20)
// ! type determined by x; we intentionally do not check this,
// as a tradeoff with more permissive behaviour in combination with comparators
const $36 = filter((x: string) => true)([1, 2])
const $8: string[] = $36
})
it('typing', () => {
const result1: Associative = filter(_ => true)
// const result: number = filter(_ => true)('a') // WRONG
// const result: Associative = filter(_ => true)('a') as number // WRONG
// const result: Associative = filter(_ => true) as number// WRONG
// const result: number = filter(_ => true) // WRONG
const result4: Array<number> = filter((a, b: number) => true, [1,2])
const result5: Array<string> = filter((a, b: number) => true, ['a','b'])
// const result: Array<number> = filter((a, b: string) => true, [1,2]) // WRONG
const result6: Map<number> = filter((a, b: string) => true, {a: 3, b: 4})
// const result: Map = filter((a, b: number) => true, {a: 3, b: 4}) // WRONG
})
// old array tests
it('multiple argument lists', () =>
expect(
filter(lt(4))([2, 4, 3])
).toEqual([2, 3])
)
it('to be used in composition', () =>
expect(
flow(
[2, 4, 3]
, filter(lt(4))
)
).toEqual([2, 3])
)
it('index provided as second parameter', () => {
expect(
filter((_, i) => i !== 1)([17, 19, 22]))
.toEqual([17, 22])
})
it('typing', () => {
const result1 = filter(_ => true)
// const result: number = filter(_ => true)('a') // WRONG
// const result: number = filter(_ => true) // WRONG
})
})