forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.test.ts
More file actions
70 lines (61 loc) · 1.9 KB
/
array.test.ts
File metadata and controls
70 lines (61 loc) · 1.9 KB
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
/**
* Copyright (c) 2022 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { allMatch, anyMatch, average, groupBy, rotate, sum } from "@util/array"
describe("util/array", () => {
test('group by', () => {
const arr = [
[1, 2],
[1, 3],
[2, 3],
[3, 4],
[2, 9]
]
// Find the max value of each group
const maxMap = groupBy(arr, a => a[0], arr => Math.max(...arr.map(a => a[1])))
expect(maxMap).toEqual({
1: 3,
2: 9,
3: 4
})
const allVal = groupBy(arr, _ => undefined, arr => arr.map(a => a[1]))
expect(allVal).toEqual({})
})
test("rotate", () => {
const arr = [1, 2, 3, 4, 5, 6]
// Left rotate for 1 time
rotate(arr)
expect(arr).toEqual([2, 3, 4, 5, 6, 1])
// Left rotate again for 2 times
rotate(arr, 2, false)
expect(arr).toEqual([4, 5, 6, 1, 2, 3])
// Right rotate for 3 times
rotate(arr, 3, true)
expect(arr).toEqual([1, 2, 3, 4, 5, 6])
})
test("sum", () => {
let arr: number[] = [1, undefined, 2, 3, 4, null, NaN]
expect(10).toEqual(sum(arr))
arr = undefined
expect(0).toEqual(sum(arr))
})
test("average", () => {
expect(average([10, 1])).toEqual(11 / 2)
expect(average(null)).toBeNull()
expect(average([])).toBeNull()
expect(average([null])).toEqual(0)
})
test("allMatch", () => {
const arr = [100, 20, 30]
expect(allMatch(arr, a => a >= 20)).toBeTruthy()
expect(allMatch(arr, a => a > 20)).toBeFalsy()
})
test("anyMatch", () => {
const arr = [100, 20, 30]
expect(anyMatch(arr, a => a >= 100)).toBeTruthy()
expect(anyMatch(arr, a => a > 100)).toBeFalsy()
})
})