forked from eighttrigrams/tsfun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten.spec.ts
271 lines (176 loc) · 7.07 KB
/
flatten.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import {flatten} from '../../src/array'
import {flow} from '../../src/composition'
import {map} from '../../src/associative'
import {Associative} from '../../src/type'
/**
* tsfun | flatten
*/
describe('flatten', () => {
it('1 level', () => {
// with a single argument list and implicit argument
expect(
flatten([[1, 2], [3, 4]])
).toEqual([1, 2, 3, 4])
// with a single argument list and explicit argument
expect(
flatten([[1, 2], [3, 4]], 1)
).toEqual([1, 2, 3, 4])
// with a single argument list and explicit argument in different order
expect(
flatten(1, [[1, 2], [3, 4]])
).toEqual([1, 2, 3, 4])
// or multiple argument lists
expect(
flatten(1)([[1, 2], [3, 4]])
).toEqual([1, 2, 3, 4])
// in the latter case an empty arg list means flatten by 1 level
expect(
flatten()([[1, 2], [3, 4]])
).toEqual([1, 2, 3, 4])
})
it('1 level flattening in compositions', () => {
// plays nicely with other higher order functions like 'map' in compositions
expect(
flow([3, 4]
, map(_ => [_ * 2, _ * 3])
, flatten()
)
).toEqual([6, 9, 8, 12])
expect(
flow([3, 4]
, map(_ => [_ * 2, _ * 3])
, flatten() // or with
)
).toEqual([6, 9, 8, 12])
// of which only the latter will give us the correct type
const result13: Associative<number[]>
= flow([3, 4]
, map(_ => [_ * 2, _ * 3]) // this gives Associative
)
const result14: number[]
= flow([3, 4]
, map(_ => [_ * 2, _ * 3]) // this gives Associative
, flatten() // which flatten happily takes,
// and with the parentheses the type gets correctly inferred
)
})
it('1 - level of multiple', () => {
expect(
flatten(1)([[1, [2, 3]], [4, [5, [6, 7]]]])
).toEqual([1, [2, 3], 4, [5, [6, 7]]])
expect(
flatten()([[1, [2, 3]], [4, [5, [6, 7]]]])
).toEqual([1, [2, 3], 4, [5, [6, 7]]])
expect(
flatten(1, [[1, [2, 3]], [4, [5, [6, 7]]]])
).toEqual([1, [2, 3], 4, [5, [6, 7]]])
expect(
flatten([[1, [2, 3]], [4, [5, [6, 7]]]], 1)
).toEqual([1, [2, 3], 4, [5, [6, 7]]])
})
it('2 - levels', () => {
expect(
flatten(2)([[1, [2, 3]], [4, [5, [6, 7]]]])
).toEqual([1, 2, 3, 4, 5, [6, 7]])
expect(
flatten(2, [[1, [2, 3]], [4, [5, [6, 7]]]])
).toEqual([1, 2, 3, 4, 5, [6, 7]])
expect(
flatten([[1, [2, 3]], [4, [5, [6, 7]]]], 2)
).toEqual([1, 2, 3, 4, 5, [6, 7]])
})
it('3 - levels', () => {
expect(
flatten(3)([[1, [2, [3, 4]]], [5, [6, [7, 8]]]])
).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
expect(
flatten([[1, [2, [3, 4]]], [5, [6, [7, 8]]]], 3)
).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
})
it('1 - level - but not level to compress - implicitly', () => {
expect(
flatten([1, 2] as any)
).toEqual([1, 2])
expect(
flatten(1, [1, 2] as any)
).toEqual([1, 2])
expect(
flatten(1)([1, 2] as any)
).toEqual([1, 2])
})
it('2 - level - but not level to compress', () => {
expect(
flatten([1, 2], 2)
).toEqual([1, 2])
})
it('typing', () => {
// in the 1 level flattening case, we can easily infer the return type
const result1: number[] = flatten()([[1, 2], [3, 4]])
// const result: number = flatten()([[1, 2], [3, 4]]) // NOPE
// const result: string[] = flatten()([[1, 2], [3, 4]]) // NOPE
const result2: number[] = flatten(1)([[1, 2], [3, 4]])
// const result: number = flatten(1)([[1, 2], [3, 4]]) // NOPE
// const result: string[] = flatten(1)([[1, 2], [3, 4]]) // NOPE
const result3: number[] = flatten([[1, 2], [3, 4]], 1)
// const result: number = flatten([[1, 2], [3, 4]], 1) // NOPE
// const result: string[] = flatten([[1, 2], [3, 4]], 1) // NOPE
const result4: number[] = flatten(1, [[1, 2], [3, 4]])
// const result: number = flatten(1, [[1, 2], [3, 4]]) // NOPE
// const result: string[] = flatten(1, [[1, 2], [3, 4]], 1) // NOPE
// const result5: number[] = flatten({a: [1, 2], b: [3, 4]})
// const result6: number[] = flatten()({a: [1, 2], b: [3, 4]})
// const result7: number[] = flatten(1)({a: [1, 2], b: [3, 4]})
// const result8: number[] = flatten({a: [1, 2], b: [3, 4]}, 1)
// const result9: number[] = flatten(1, {a: [1, 2], b: [3, 4]})
// const result: string[] = flatten({a: [1, 2], b: [3, 4]}) // NOPE
// const result: number = flatten({a: [1, 2], b: [3, 4]}) // NOPE
// const result: number[] = flatten(2)({a: [1, 2], b: [3, 4]}) // NOPE
// const result: number[] = flatten({a: [1, 2], b: [3, 4]}, 2) // NOPE
// const result: number[] = flatten(2, {a: [1, 2], b: [3, 4]}) // NOPE
// from two levels onwards, will only infer any[]
const result10: any[] = flatten([[1]], 2)
// const result: string[] = flatten([[1]], 2) // NOPE
const result11: number[] = flatten(2,
[
[[1,2], [2,4]],
[[1,2], [1,2]]
]
)
// const result: string[] = flatten(2, // NOPE
// [
// [[1,2], [2,4]],
// [[1,2], [1,2]]
// ]
// )
const result12: number[] = flatten(3,
[
[[[1,2],[1,2]], [[1,2],[1,2]]],
[[[1,2],[1,2]], [[1,2],[1,2]]]
]
)
// const result: string[] = flatten(3, // NOPE
// [
// [[[1,2],[1,2]], [[1,2],[1,2]]],
// [[[1,2],[1,2]], [[1,2],[1,2]]]
// ]
// )
})
// err cases
it('illegal arguments', () => {
const expected = 'illegal argument in "tsfun|flatten"';
expect(() => flatten(2 as any, 2 as any)).toThrow(expected)
expect(() => flatten([] as any, [] as any)).toThrow(expected)
expect(() => flatten()(1 as any)).toThrow(expected)
expect(() => (flatten as any)(2, 2, 2)).toThrow(expected)
expect(() => flatten(1 as any)(1 as any)).toThrow(expected)
expect(() => flatten()(1 as any)).toThrow(expected)
expect(() => flatten(0)([])).toThrow(expected)
expect(() => flatten(0, [])).toThrow(expected)
expect(() => flatten([], 0)).toThrow(expected)
expect(() => flatten({} as any, 1)).toThrow(expected)
expect(() => flatten(1 as any, {} as any)).toThrow(expected)
expect(() => flatten(1)({} as any)).toThrow(expected)
expect(() => flatten()({} as any)).toThrow(expected)
expect(() => flatten()({} as any)).toThrow(expected)
})
})