@cloudcome/utils-core 0.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/LICENSE +21 -0
- package/package.json +277 -6
- package/src/array.ts +312 -0
- package/src/async.ts +379 -0
- package/src/base64.ts +20 -0
- package/src/cache.ts +146 -0
- package/src/color/contrast.ts +20 -0
- package/src/color/distance.ts +28 -0
- package/src/color/helpers.ts +23 -0
- package/src/color/hex-hsl.ts +11 -0
- package/src/color/hex-hsv.ts +28 -0
- package/src/color/hex-hwb.ts +31 -0
- package/src/color/hex-rgb.ts +39 -0
- package/src/color/hsl-lighten.ts +15 -0
- package/src/color/hsv-brighten.ts +17 -0
- package/src/color/luminance.ts +17 -0
- package/src/color/mix.ts +26 -0
- package/src/color/rgb-hsl.ts +53 -0
- package/src/color/rgb-hsv.ts +52 -0
- package/src/color/rgb-hwb.ts +56 -0
- package/src/color/rgb-lab.ts +33 -0
- package/src/color/rgb-whiter.ts +22 -0
- package/src/color/rgb-xyz.ts +62 -0
- package/src/color/types.ts +65 -0
- package/src/color/xyz-lab.ts +54 -0
- package/src/color.ts +19 -0
- package/src/crypto/md5.mjs +357 -0
- package/src/crypto/sha1.mjs +300 -0
- package/src/crypto/sha256.mjs +310 -0
- package/src/crypto/sha512.mjs +459 -0
- package/src/crypto.ts +60 -0
- package/src/date/const.ts +6 -0
- package/src/date/core.ts +162 -0
- package/src/date/days.ts +51 -0
- package/src/date/is.ts +186 -0
- package/src/date/relative.ts +92 -0
- package/src/date/start-end.ts +246 -0
- package/src/date/timezone.ts +220 -0
- package/src/date/weeks.ts +100 -0
- package/src/date.ts +8 -0
- package/src/dict.ts +1 -0
- package/src/dts/global.d.ts +27 -0
- package/src/easing.ts +166 -0
- package/src/emitter.ts +117 -0
- package/src/enum.ts +171 -0
- package/src/env.ts +62 -0
- package/src/error.ts +31 -0
- package/src/exception.ts +68 -0
- package/src/fn.ts +197 -0
- package/src/index.ts +1 -0
- package/src/number.ts +236 -0
- package/src/object/each.ts +56 -0
- package/src/object/get-set.ts +273 -0
- package/src/object/is.ts +128 -0
- package/src/object/merge.ts +180 -0
- package/src/object/process.ts +80 -0
- package/src/object.ts +5 -0
- package/src/path.ts +188 -0
- package/src/promise.ts +111 -0
- package/src/qs.ts +119 -0
- package/src/regexp.ts +156 -0
- package/src/string.ts +146 -0
- package/src/time/from.ts +57 -0
- package/src/time/to.ts +106 -0
- package/src/time.ts +2 -0
- package/src/timer.ts +226 -0
- package/src/tree.ts +394 -0
- package/src/type.ts +197 -0
- package/src/types.ts +78 -0
- package/src/unique.ts +77 -0
- package/src/url.ts +93 -0
- package/src/version.ts +71 -0
- package/test/array.test.ts +332 -0
- package/test/async-real.test.ts +39 -0
- package/test/async.test.ts +375 -0
- package/test/base64.test.ts +32 -0
- package/test/cache.test.ts +83 -0
- package/test/color.test.ts +163 -0
- package/test/crypto.test.ts +34 -0
- package/test/date-tz.test.ts +206 -0
- package/test/date.test.ts +353 -0
- package/test/easing.test.ts +33 -0
- package/test/emitter.test.ts +71 -0
- package/test/enum.test.ts +113 -0
- package/test/env.test.ts +69 -0
- package/test/error.test.ts +58 -0
- package/test/exception.test.ts +43 -0
- package/test/fn.test.ts +263 -0
- package/test/helpers.ts +23 -0
- package/test/index.test.ts +6 -0
- package/test/number.test.ts +213 -0
- package/test/object.test.ts +309 -0
- package/test/path.test.ts +156 -0
- package/test/promise.test.ts +199 -0
- package/test/qs.test.ts +79 -0
- package/test/regexp.test.ts +97 -0
- package/test/string.test.ts +150 -0
- package/test/time.test.ts +214 -0
- package/test/timer.test.ts +114 -0
- package/test/tree.test.ts +348 -0
- package/test/type.test.ts +226 -0
- package/test/unique.test.ts +71 -0
- package/test/url.test.ts +136 -0
- package/test/version.test.ts +52 -0
- package/tsconfig.json +31 -0
- package/vite.config.mts +114 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { type TTimeDuration, timeFrom, timeParse, timeToDays, timeToHours, timeToMinutes, timeToSeconds } from '@/time';
|
|
2
|
+
|
|
3
|
+
describe('timeToDays', () => {
|
|
4
|
+
test('解析123456789毫秒', () => {
|
|
5
|
+
expect(timeToDays(123456789)).toMatchObject({
|
|
6
|
+
days: 1,
|
|
7
|
+
hours: 10,
|
|
8
|
+
minutes: 17,
|
|
9
|
+
seconds: 36,
|
|
10
|
+
milliseconds: 789,
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('0毫秒', () => {
|
|
15
|
+
expect(timeToDays(0)).toMatchObject({
|
|
16
|
+
days: 0,
|
|
17
|
+
hours: 0,
|
|
18
|
+
minutes: 0,
|
|
19
|
+
seconds: 0,
|
|
20
|
+
milliseconds: 0,
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('精确1天', () => {
|
|
25
|
+
expect(timeToDays(86400000)).toMatchObject({
|
|
26
|
+
days: 1,
|
|
27
|
+
hours: 0,
|
|
28
|
+
minutes: 0,
|
|
29
|
+
seconds: 0,
|
|
30
|
+
milliseconds: 0,
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('timeToHours', () => {
|
|
36
|
+
test('解析123456789毫秒', () => {
|
|
37
|
+
expect(timeToHours(123456789)).toMatchObject({
|
|
38
|
+
days: 0,
|
|
39
|
+
hours: 34,
|
|
40
|
+
minutes: 17,
|
|
41
|
+
seconds: 36,
|
|
42
|
+
milliseconds: 789,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('精确3小时', () => {
|
|
47
|
+
expect(timeToHours(3 * 3600000)).toMatchObject({
|
|
48
|
+
days: 0,
|
|
49
|
+
hours: 3,
|
|
50
|
+
minutes: 0,
|
|
51
|
+
seconds: 0,
|
|
52
|
+
milliseconds: 0,
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('timeToMinutes', () => {
|
|
58
|
+
test('解析123456789毫秒', () => {
|
|
59
|
+
expect(timeToMinutes(123456789)).toMatchObject({
|
|
60
|
+
hours: 0,
|
|
61
|
+
minutes: 2057,
|
|
62
|
+
seconds: 36,
|
|
63
|
+
milliseconds: 789,
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('精确5分钟', () => {
|
|
68
|
+
expect(timeToMinutes(5 * 60000)).toMatchObject({
|
|
69
|
+
hours: 0,
|
|
70
|
+
minutes: 5,
|
|
71
|
+
seconds: 0,
|
|
72
|
+
milliseconds: 0,
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('timeToSeconds', () => {
|
|
78
|
+
test('解析123456789毫秒', () => {
|
|
79
|
+
expect(timeToSeconds(123456789)).toMatchObject({
|
|
80
|
+
seconds: 123456,
|
|
81
|
+
milliseconds: 789,
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('精确10秒', () => {
|
|
86
|
+
expect(timeToSeconds(10000)).toMatchObject({
|
|
87
|
+
seconds: 10,
|
|
88
|
+
milliseconds: 0,
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('timeFrom', () => {
|
|
94
|
+
test('解析简单时间字符串', () => {
|
|
95
|
+
expect(timeFrom('1d2h30m')).toBe(95400000);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('解析复杂时间字符串', () => {
|
|
99
|
+
expect(timeFrom('1y2M3d4h5m6s')).toBe(36993906000);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('解析单个时间单位', () => {
|
|
103
|
+
expect(timeFrom('1h')).toBe(3600000);
|
|
104
|
+
expect(timeFrom('30m')).toBe(1800000);
|
|
105
|
+
expect(timeFrom('10s')).toBe(10000);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('解析大小写不敏感', () => {
|
|
109
|
+
expect(timeFrom('1D2H30M')).toBe(77853600000);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('解析空字符串', () => {
|
|
113
|
+
expect(timeFrom('')).toBe(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('解析无效字符串', () => {
|
|
117
|
+
expect(timeFrom('invalid')).toBe(0);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('应正确计算完整时间对象的毫秒数', () => {
|
|
121
|
+
const duration: TTimeDuration = {
|
|
122
|
+
years: 1,
|
|
123
|
+
months: 2,
|
|
124
|
+
days: 3,
|
|
125
|
+
hours: 4,
|
|
126
|
+
minutes: 5,
|
|
127
|
+
seconds: 6,
|
|
128
|
+
milliseconds: 0,
|
|
129
|
+
};
|
|
130
|
+
expect(timeFrom(duration)).toBe(
|
|
131
|
+
1 * 31536000000 + // 1年
|
|
132
|
+
2 * 2592000000 + // 2个月
|
|
133
|
+
3 * 86400000 + // 3天
|
|
134
|
+
4 * 3600000 + // 4小时
|
|
135
|
+
5 * 60000 + // 5分钟
|
|
136
|
+
6 * 1000, // 6秒
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('应处理部分时间单位', () => {
|
|
141
|
+
const duration: TTimeDuration = {
|
|
142
|
+
years: 0,
|
|
143
|
+
months: 0,
|
|
144
|
+
days: 2,
|
|
145
|
+
hours: 3,
|
|
146
|
+
minutes: 0,
|
|
147
|
+
seconds: 0,
|
|
148
|
+
milliseconds: 0,
|
|
149
|
+
};
|
|
150
|
+
expect(timeFrom(duration)).toBe(
|
|
151
|
+
2 * 86400000 + // 2天
|
|
152
|
+
3 * 3600000, // 3小时
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('应处理空对象', () => {
|
|
157
|
+
const duration: TTimeDuration = {
|
|
158
|
+
years: 0,
|
|
159
|
+
months: 0,
|
|
160
|
+
days: 0,
|
|
161
|
+
hours: 0,
|
|
162
|
+
minutes: 0,
|
|
163
|
+
seconds: 0,
|
|
164
|
+
milliseconds: 0,
|
|
165
|
+
};
|
|
166
|
+
expect(timeFrom(duration)).toBe(0);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('timeParse', () => {
|
|
171
|
+
it('应正确解析完整时间字符串', () => {
|
|
172
|
+
expect(timeParse('1y2M3d4h5m6s')).toEqual({
|
|
173
|
+
years: 1,
|
|
174
|
+
months: 2,
|
|
175
|
+
days: 3,
|
|
176
|
+
hours: 4,
|
|
177
|
+
minutes: 5,
|
|
178
|
+
seconds: 6,
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('应处理部分时间单位', () => {
|
|
183
|
+
expect(timeParse('2d3h')).toEqual({
|
|
184
|
+
years: 0,
|
|
185
|
+
months: 0,
|
|
186
|
+
days: 2,
|
|
187
|
+
hours: 3,
|
|
188
|
+
minutes: 0,
|
|
189
|
+
seconds: 0,
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('应处理空字符串', () => {
|
|
194
|
+
expect(timeParse('')).toEqual({
|
|
195
|
+
years: 0,
|
|
196
|
+
months: 0,
|
|
197
|
+
days: 0,
|
|
198
|
+
hours: 0,
|
|
199
|
+
minutes: 0,
|
|
200
|
+
seconds: 0,
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('应忽略无效格式', () => {
|
|
205
|
+
expect(timeParse('invalid')).toEqual({
|
|
206
|
+
years: 0,
|
|
207
|
+
months: 0,
|
|
208
|
+
days: 0,
|
|
209
|
+
hours: 0,
|
|
210
|
+
minutes: 0,
|
|
211
|
+
seconds: 0,
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { timeInterval } from '../src/timer';
|
|
3
|
+
|
|
4
|
+
describe('timeInterval 定时器', () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
vi.useRealTimers();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('应该按照指定间隔时间执行回调', async () => {
|
|
10
|
+
vi.useFakeTimers();
|
|
11
|
+
const mockFn = vi.fn();
|
|
12
|
+
const timer = timeInterval(mockFn, 1000);
|
|
13
|
+
|
|
14
|
+
// 立即执行
|
|
15
|
+
timer.start();
|
|
16
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
17
|
+
|
|
18
|
+
// 第一次执行
|
|
19
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
20
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
21
|
+
|
|
22
|
+
// 第二次执行
|
|
23
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
24
|
+
expect(mockFn).toHaveBeenCalledTimes(2);
|
|
25
|
+
|
|
26
|
+
timer.stop();
|
|
27
|
+
expect(mockFn).toHaveBeenCalledTimes(2);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('immediate=true 时应立即执行回调', async () => {
|
|
31
|
+
vi.useFakeTimers();
|
|
32
|
+
const mockFn = vi.fn();
|
|
33
|
+
const timer = timeInterval(mockFn, 1000, { leading: true, trailing: true });
|
|
34
|
+
|
|
35
|
+
// 立即执行
|
|
36
|
+
timer.start();
|
|
37
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
38
|
+
|
|
39
|
+
// 第一次间隔执行
|
|
40
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
41
|
+
expect(mockFn).toHaveBeenCalledTimes(2);
|
|
42
|
+
|
|
43
|
+
// 停止执行
|
|
44
|
+
timer.stop();
|
|
45
|
+
expect(mockFn).toHaveBeenCalledTimes(3);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('调用 stop 后应停止定时器', async () => {
|
|
49
|
+
vi.useFakeTimers();
|
|
50
|
+
const mockFn = vi.fn();
|
|
51
|
+
const timer = timeInterval(mockFn, 1000);
|
|
52
|
+
|
|
53
|
+
timer.start();
|
|
54
|
+
expect(mockFn).toHaveBeenCalledTimes(0);
|
|
55
|
+
|
|
56
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
57
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
58
|
+
|
|
59
|
+
timer.stop();
|
|
60
|
+
|
|
61
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
62
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('可以暂停和恢复定时器', async () => {
|
|
66
|
+
vi.useFakeTimers();
|
|
67
|
+
const mockFn = vi.fn();
|
|
68
|
+
const timer = timeInterval(mockFn, 1000);
|
|
69
|
+
|
|
70
|
+
timer.start();
|
|
71
|
+
expect(mockFn).toHaveBeenCalledTimes(0);
|
|
72
|
+
|
|
73
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
74
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
75
|
+
|
|
76
|
+
timer.pause();
|
|
77
|
+
|
|
78
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
79
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
80
|
+
|
|
81
|
+
timer.resume();
|
|
82
|
+
|
|
83
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
84
|
+
expect(mockFn).toHaveBeenCalledTimes(2);
|
|
85
|
+
|
|
86
|
+
timer.stop();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('回调函数应接收正确的参数', async () => {
|
|
90
|
+
vi.useFakeTimers();
|
|
91
|
+
const mockFn = vi.fn();
|
|
92
|
+
const timer = timeInterval(mockFn, 1000);
|
|
93
|
+
|
|
94
|
+
timer.start();
|
|
95
|
+
|
|
96
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
97
|
+
const arg = mockFn.mock.calls[0][0];
|
|
98
|
+
expect(Object.keys(arg)).toEqual(
|
|
99
|
+
expect.arrayContaining([
|
|
100
|
+
'times',
|
|
101
|
+
'startAt',
|
|
102
|
+
'stopAt',
|
|
103
|
+
'pauseAt',
|
|
104
|
+
'resumeAt',
|
|
105
|
+
'currentAt',
|
|
106
|
+
'elapsedTime',
|
|
107
|
+
'runningTime',
|
|
108
|
+
'intervalTime',
|
|
109
|
+
]),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
timer.stop();
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { type TTreeItem, deepFlat, treeEach, treeFind, treeFrom } from '@/tree';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
type TestTreeItem = TTreeItem & {
|
|
5
|
+
id: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const treeList: TestTreeItem[] = [
|
|
9
|
+
{
|
|
10
|
+
id: '1',
|
|
11
|
+
children: [
|
|
12
|
+
{
|
|
13
|
+
id: '1-1',
|
|
14
|
+
children: [
|
|
15
|
+
{
|
|
16
|
+
id: '1-1-1',
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
id: '1-2',
|
|
22
|
+
children: [
|
|
23
|
+
{
|
|
24
|
+
id: '1-2-1',
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
describe('treeEach', () => {
|
|
33
|
+
it('应正确执行深度优先遍历', () => {
|
|
34
|
+
const results: string[] = [];
|
|
35
|
+
|
|
36
|
+
treeEach(treeList, (info) => {
|
|
37
|
+
results.push(info.item.id);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(results).toEqual(['1', '1-1', '1-1-1', '1-2', '1-2-1']);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('应正确执行广度优先遍历', () => {
|
|
44
|
+
const results: string[] = [];
|
|
45
|
+
|
|
46
|
+
treeEach(
|
|
47
|
+
treeList,
|
|
48
|
+
(info) => {
|
|
49
|
+
results.push(info.item.id);
|
|
50
|
+
},
|
|
51
|
+
true,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(results).toEqual(['1', '1-1', '1-2', '1-1-1', '1-2-1']);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('应支持提前终止深度遍历', () => {
|
|
58
|
+
const results: string[] = [];
|
|
59
|
+
|
|
60
|
+
treeEach(treeList, (info) => {
|
|
61
|
+
results.push(info.item.id);
|
|
62
|
+
if (info.item.id === '1-1-1') return false;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(results).toEqual(['1', '1-1', '1-1-1']);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('应支持提前终止深度遍历', () => {
|
|
69
|
+
const results: string[] = [];
|
|
70
|
+
|
|
71
|
+
treeEach(
|
|
72
|
+
treeList,
|
|
73
|
+
(info) => {
|
|
74
|
+
results.push(info.item.id);
|
|
75
|
+
if (info.item.id === '1-2') return false;
|
|
76
|
+
},
|
|
77
|
+
true,
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(results).toEqual(['1', '1-1', '1-2']);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('深度优先信息', () => {
|
|
84
|
+
const idList: string[] = [];
|
|
85
|
+
const parentList: (TestTreeItem | null)[] = [];
|
|
86
|
+
const levelList: number[] = [];
|
|
87
|
+
const pathList: string[][] = [];
|
|
88
|
+
|
|
89
|
+
treeEach(treeList, ({ item, index, list, parent, level, path }) => {
|
|
90
|
+
idList.push(item.id);
|
|
91
|
+
parentList.push(parent);
|
|
92
|
+
levelList.push(level);
|
|
93
|
+
pathList.push(path.map((item) => item.id));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
let i = 0;
|
|
97
|
+
// console.log(idList);
|
|
98
|
+
expect(idList[i++]).toEqual('1');
|
|
99
|
+
expect(idList[i++]).toEqual('1-1');
|
|
100
|
+
expect(idList[i++]).toEqual('1-1-1');
|
|
101
|
+
expect(idList[i++]).toEqual('1-2');
|
|
102
|
+
expect(idList[i++]).toEqual('1-2-1');
|
|
103
|
+
expect(idList[i++]).toEqual(undefined);
|
|
104
|
+
let j = 0;
|
|
105
|
+
// console.log(parentList);
|
|
106
|
+
expect(parentList[j++]).toEqual(null);
|
|
107
|
+
expect(parentList[j++]?.id).toEqual('1');
|
|
108
|
+
expect(parentList[j++]?.id).toEqual('1-1');
|
|
109
|
+
expect(parentList[j++]?.id).toEqual('1');
|
|
110
|
+
expect(parentList[j++]?.id).toEqual('1-2');
|
|
111
|
+
expect(parentList[j++]).toEqual(undefined);
|
|
112
|
+
let k = 0;
|
|
113
|
+
// console.log(levelList);
|
|
114
|
+
expect(levelList[k++]).toEqual(1);
|
|
115
|
+
expect(levelList[k++]).toEqual(2);
|
|
116
|
+
expect(levelList[k++]).toEqual(3);
|
|
117
|
+
expect(levelList[k++]).toEqual(2);
|
|
118
|
+
expect(levelList[k++]).toEqual(3);
|
|
119
|
+
expect(levelList[k++]).toEqual(undefined);
|
|
120
|
+
|
|
121
|
+
expect(pathList).toEqual([
|
|
122
|
+
//
|
|
123
|
+
['1'],
|
|
124
|
+
['1', '1-1'],
|
|
125
|
+
['1', '1-1', '1-1-1'],
|
|
126
|
+
['1', '1-2'],
|
|
127
|
+
['1', '1-2', '1-2-1'],
|
|
128
|
+
]);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('广度优先信息', () => {
|
|
132
|
+
const idList: string[] = [];
|
|
133
|
+
const parentList: (TestTreeItem | null)[] = [];
|
|
134
|
+
const levelList: number[] = [];
|
|
135
|
+
const pathList: string[][] = [];
|
|
136
|
+
|
|
137
|
+
treeEach(
|
|
138
|
+
treeList,
|
|
139
|
+
({ item, index, list, parent, level, path }) => {
|
|
140
|
+
idList.push(item.id);
|
|
141
|
+
parentList.push(parent);
|
|
142
|
+
levelList.push(level);
|
|
143
|
+
pathList.push(path.map((item) => item.id));
|
|
144
|
+
},
|
|
145
|
+
true,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
let i = 0;
|
|
149
|
+
// console.log(idList);
|
|
150
|
+
expect(idList[i++]).toEqual('1');
|
|
151
|
+
expect(idList[i++]).toEqual('1-1');
|
|
152
|
+
expect(idList[i++]).toEqual('1-2');
|
|
153
|
+
expect(idList[i++]).toEqual('1-1-1');
|
|
154
|
+
expect(idList[i++]).toEqual('1-2-1');
|
|
155
|
+
expect(idList[i++]).toEqual(undefined);
|
|
156
|
+
let j = 0;
|
|
157
|
+
// console.log(parentList);
|
|
158
|
+
expect(parentList[j++]).toEqual(null);
|
|
159
|
+
expect(parentList[j++]?.id).toEqual('1');
|
|
160
|
+
expect(parentList[j++]?.id).toEqual('1');
|
|
161
|
+
expect(parentList[j++]?.id).toEqual('1-1');
|
|
162
|
+
expect(parentList[j++]?.id).toEqual('1-2');
|
|
163
|
+
expect(parentList[j++]).toEqual(undefined);
|
|
164
|
+
let k = 0;
|
|
165
|
+
// console.log(levelList);
|
|
166
|
+
expect(levelList[k++]).toEqual(1);
|
|
167
|
+
expect(levelList[k++]).toEqual(2);
|
|
168
|
+
expect(levelList[k++]).toEqual(2);
|
|
169
|
+
expect(levelList[k++]).toEqual(3);
|
|
170
|
+
expect(levelList[k++]).toEqual(3);
|
|
171
|
+
expect(levelList[k++]).toEqual(undefined);
|
|
172
|
+
|
|
173
|
+
expect(pathList).toEqual([
|
|
174
|
+
//
|
|
175
|
+
['1'],
|
|
176
|
+
['1', '1-1'],
|
|
177
|
+
['1', '1-2'],
|
|
178
|
+
['1', '1-1', '1-1-1'],
|
|
179
|
+
['1', '1-2', '1-2-1'],
|
|
180
|
+
]);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe('treeFind', () => {
|
|
185
|
+
it('深度优先', () => {
|
|
186
|
+
const expected = treeList[0].children?.[1].children?.[0];
|
|
187
|
+
const found2 = treeFind(treeList, (info) => info.item.id === '1-2-1');
|
|
188
|
+
|
|
189
|
+
expect(found2?.item).toBe(expected);
|
|
190
|
+
expect(found2?.item.id).toEqual('1-2-1');
|
|
191
|
+
expect(found2?.index).toEqual(0);
|
|
192
|
+
expect(found2?.list).toBe(treeList[0].children?.[1].children);
|
|
193
|
+
expect(found2?.parent).not.toBe(null);
|
|
194
|
+
expect(found2?.parent?.id).toEqual('1-2');
|
|
195
|
+
expect(found2?.level).toEqual(3);
|
|
196
|
+
expect(found2?.path.map((el) => el.id)).toEqual(['1', '1-2', '1-2-1']);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('广度优先', () => {
|
|
200
|
+
const expected = treeList[0].children?.[1].children?.[0];
|
|
201
|
+
const found2 = treeFind(treeList, (info) => info.item.id === '1-2-1', true);
|
|
202
|
+
|
|
203
|
+
expect(found2?.item).toBe(expected);
|
|
204
|
+
expect(found2?.item.id).toEqual('1-2-1');
|
|
205
|
+
expect(found2?.index).toEqual(0);
|
|
206
|
+
expect(found2?.list).toBe(treeList[0].children?.[1].children);
|
|
207
|
+
expect(found2?.parent).not.toBe(null);
|
|
208
|
+
expect(found2?.parent?.id).toEqual('1-2');
|
|
209
|
+
expect(found2?.level).toEqual(3);
|
|
210
|
+
expect(found2?.path.map((el) => el.id)).toEqual(['1', '1-2', '1-2-1']);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('应返回 undefined 如果没有找到满足条件的节点', () => {
|
|
214
|
+
const found = treeFind(treeList, (info) => info.item.id === 'nonexistent');
|
|
215
|
+
expect(found).toBeUndefined();
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('treeFlat', () => {
|
|
220
|
+
it('深度优先', () => {
|
|
221
|
+
const shallowList = deepFlat(treeList, ({ item }) => item.id);
|
|
222
|
+
expect(shallowList).toEqual(['1', '1-1', '1-1-1', '1-2', '1-2-1']);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('广度优先', () => {
|
|
226
|
+
const shallowList = deepFlat(treeList, ({ item }) => item.id, true);
|
|
227
|
+
expect(shallowList).toEqual(['1', '1-1', '1-2', '1-1-1', '1-2-1']);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe('treeFrom', () => {
|
|
232
|
+
it('应从扁平列表构建树形结构', () => {
|
|
233
|
+
const list = [
|
|
234
|
+
{ id: 1, parentId: 0, name: 'Root' },
|
|
235
|
+
{ id: 2, parentId: 1, name: 'Child 1' },
|
|
236
|
+
{ id: 3, parentId: 1, name: 'Child 2' },
|
|
237
|
+
{ id: 4, parentId: 2, name: 'Grandchild 1' },
|
|
238
|
+
];
|
|
239
|
+
|
|
240
|
+
const tree = treeFrom(list, {
|
|
241
|
+
getSelfKey: (item) => item.id,
|
|
242
|
+
getParentKey: (item) => (item.parentId === 0 ? null : item.parentId),
|
|
243
|
+
appendChild: (parent, info) => {
|
|
244
|
+
const parentItem = parent.item as typeof parent.item & { children: (typeof parent.item)[] };
|
|
245
|
+
if (!parentItem.children) parentItem.children = [];
|
|
246
|
+
parentItem.children.push(info.item);
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
expect(tree).toEqual([
|
|
251
|
+
{
|
|
252
|
+
id: 1,
|
|
253
|
+
parentId: 0,
|
|
254
|
+
name: 'Root',
|
|
255
|
+
children: [
|
|
256
|
+
{
|
|
257
|
+
id: 2,
|
|
258
|
+
parentId: 1,
|
|
259
|
+
name: 'Child 1',
|
|
260
|
+
children: [{ id: 4, parentId: 2, name: 'Grandchild 1' }],
|
|
261
|
+
},
|
|
262
|
+
{ id: 3, parentId: 1, name: 'Child 2' },
|
|
263
|
+
],
|
|
264
|
+
},
|
|
265
|
+
]);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('应处理游离节点', () => {
|
|
269
|
+
const list = [
|
|
270
|
+
{ id: 2, parentId: 3, name: 'Child 1' },
|
|
271
|
+
{ id: 1, parentId: 0, name: 'Root' },
|
|
272
|
+
{ id: 3, parentId: 1, name: 'Child 2' },
|
|
273
|
+
];
|
|
274
|
+
|
|
275
|
+
const tree = treeFrom(list, {
|
|
276
|
+
getSelfKey: (item) => item.id,
|
|
277
|
+
getParentKey: (item) => (item.parentId === 0 ? null : item.parentId),
|
|
278
|
+
appendChild: (parent, info) => {
|
|
279
|
+
const parentItem = parent.item as typeof parent.item & { children: (typeof parent.item)[] };
|
|
280
|
+
if (!parentItem.children) parentItem.children = [];
|
|
281
|
+
parentItem.children.push(info.item);
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
expect(tree).toEqual([
|
|
286
|
+
{
|
|
287
|
+
id: 1,
|
|
288
|
+
parentId: 0,
|
|
289
|
+
name: 'Root',
|
|
290
|
+
children: [
|
|
291
|
+
{
|
|
292
|
+
id: 3,
|
|
293
|
+
parentId: 1,
|
|
294
|
+
name: 'Child 2',
|
|
295
|
+
children: [{ id: 2, parentId: 3, name: 'Child 1' }],
|
|
296
|
+
},
|
|
297
|
+
],
|
|
298
|
+
},
|
|
299
|
+
]);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('应返回 undefined 如果未找到根节点', () => {
|
|
303
|
+
const list = [
|
|
304
|
+
{ id: 11, parentId: 2, name: 'Child 1' },
|
|
305
|
+
{ id: 12, parentId: 1, name: 'Child 2' },
|
|
306
|
+
{ id: 13, parentId: 3, name: 'Child 3' },
|
|
307
|
+
{ id: 1, parentId: 0, name: 'Root' },
|
|
308
|
+
{ id: 2, parentId: 0, name: 'Root' },
|
|
309
|
+
];
|
|
310
|
+
|
|
311
|
+
const tree = treeFrom(list, {
|
|
312
|
+
getSelfKey: (item) => item.id,
|
|
313
|
+
getParentKey: (item) => (item.parentId === 0 ? null : item.parentId),
|
|
314
|
+
appendChild: (parent, info) => {
|
|
315
|
+
const parentItem = parent.item as typeof parent.item & { children: (typeof parent.item)[] };
|
|
316
|
+
if (!parentItem.children) parentItem.children = [];
|
|
317
|
+
parentItem.children.push(info.item);
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
expect(tree).toEqual([
|
|
322
|
+
{
|
|
323
|
+
id: 1,
|
|
324
|
+
parentId: 0,
|
|
325
|
+
name: 'Root',
|
|
326
|
+
children: [
|
|
327
|
+
{
|
|
328
|
+
id: 12,
|
|
329
|
+
parentId: 1,
|
|
330
|
+
name: 'Child 2',
|
|
331
|
+
},
|
|
332
|
+
],
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
id: 2,
|
|
336
|
+
parentId: 0,
|
|
337
|
+
name: 'Root',
|
|
338
|
+
children: [
|
|
339
|
+
{
|
|
340
|
+
id: 11,
|
|
341
|
+
parentId: 2,
|
|
342
|
+
name: 'Child 1',
|
|
343
|
+
},
|
|
344
|
+
],
|
|
345
|
+
},
|
|
346
|
+
]);
|
|
347
|
+
});
|
|
348
|
+
});
|