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
127 lines (108 loc) · 3.73 KB
/
Copy patharray.test.ts
File metadata and controls
127 lines (108 loc) · 3.73 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
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
/**
* Copyright (c) 2022 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { allMatch, anyMatch, groupBy, rotate, sum, toMap } from "@util/array"
describe("util/array", () => {
test('group by', () => {
const arr: [number, number][] = [
[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, 2, 3, 4]
expect(sum(arr)).toEqual(10)
arr = []
expect(sum(arr)).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()
})
})
describe('toMap', () => {
const users = [
{ id: 1, name: 'Alice', role: 'admin' },
{ id: 2, name: 'Bob', role: 'user' },
{ id: 3, name: 'Charlie', role: 'guest' },
]
const products = [
{ id: 'p1', name: 'Laptop', price: 1200 },
{ id: 'p2', name: 'Mouse', price: 25 },
{ id: 'p3', name: 'Keyboard', price: 75 },
]
test('should create a map with array elements as values when valFunc is not provided', () => {
const userMap = toMap(users, u => u.id)
expect(userMap).toEqual({
1: { id: 1, name: 'Alice', role: 'admin' },
2: { id: 2, name: 'Bob', role: 'user' },
3: { id: 3, name: 'Charlie', role: 'guest' },
})
})
test('should create a map with transformed values when valFunc is provided', () => {
const userRolesMap = toMap(users, u => u.id, u => u.role)
expect(userRolesMap).toEqual({
1: 'admin',
2: 'user',
3: 'guest',
})
})
test('should handle string keys correctly', () => {
const productMap = toMap(products, p => p.id)
expect(productMap).toEqual({
'p1': { id: 'p1', name: 'Laptop', price: 1200 },
'p2': { id: 'p2', name: 'Mouse', price: 25 },
'p3': { id: 'p3', name: 'Keyboard', price: 75 },
})
})
test('should return an empty map for an empty array', () => {
const emptyArray: any[] = []
const emptyMap = toMap(emptyArray, i => i.id)
expect(emptyMap).toEqual({})
})
test('should overwrite existing keys with later elements', () => {
const usersWithDuplicateKey = [
{ id: 1, name: 'Alice', role: 'admin' },
{ id: 2, name: 'Bob', role: 'user' },
{ id: 1, name: 'John', role: 'guest' }, // ID 1 重复
]
const userMap = toMap(usersWithDuplicateKey, u => u.id)
expect(userMap).toEqual({
1: { id: 1, name: 'John', role: 'guest' },
2: { id: 2, name: 'Bob', role: 'user' },
})
})
})