-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
array.test.ts
74 lines (66 loc) · 1.42 KB
/
array.test.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
import { describe, expect, it } from 'vitest'
import { flattenArrayable, partition, range, toArray } from './array'
describe('toArray', () => {
it.each([
[undefined, []],
[null, []],
[false, [false]],
[0, [0]],
['', ['']],
[[], []],
['foo', ['foo']],
[['foo'], ['foo']],
])('%s => %s', (input, expected) => {
expect(toArray(input)).toEqual(expected)
})
})
it('flattenArrayable', () => {
expect(flattenArrayable()).toEqual([])
expect(flattenArrayable([])).toEqual([])
expect(flattenArrayable(1)).toEqual([1])
expect(flattenArrayable([1, '2', 3])).toEqual([1, '2', 3])
expect(flattenArrayable([1, [1, 2]])).toEqual([1, 1, 2])
expect(flattenArrayable([1, [1, [2]]])).toEqual([1, 1, [2]])
})
it('range', () => {
expect(range(0)).toEqual([])
expect(range(2)).toEqual([0, 1])
expect(range(2, 5)).toEqual([2, 3, 4])
expect(range(2, 10, 2)).toEqual([2, 4, 6, 8])
})
it('partition', () => {
const data = range(10)
expect(
partition(data, i => i % 2),
).toEqual([
[1, 3, 5, 7, 9],
[0, 2, 4, 6, 8],
])
expect(
partition(
data,
i => i % 3 === 0,
i => i % 2 === 0,
),
).toEqual([
[0, 3, 6, 9],
[2, 4, 8],
[1, 5, 7],
])
expect(
partition(
data,
i => i,
),
).toHaveLength(2)
expect(
partition(
data,
i => i,
i => i,
i => i,
i => i,
i => i,
),
).toHaveLength(6)
})