deepClone

说明

深拷贝

源码

用法

deepClone.test.ts
1
import { deepClone } from "../../utility/others/deepClone";
2
3
describe('deepClone', () => {
4
test('deepClone should copy the whole object and return', () => {
5
const received = {
6
name: 'duan',
7
age: 20,
8
skills: ['run', 'programm', 'read'],
9
friends: [
10
{
11
name: 'jack',
12
age: 21,
13
},
14
{
15
name: 'lucy',
16
age: 22,
17
},
18
],
19
program: {
20
deepth: 40,
21
related: {
22
website: [
23
{
24
name: 'juejin',
25
address: 'https://juejin.io/timeline',
26
},
27
{
28
name: 'zhihu',
29
address: 'https://zhihu.com'
30
},
31
],
32
},
33
},
34
focus: true,
35
};
36
37
expect(deepClone(received).name).toBe('duan');
38
expect(
39
(deepClone(received).skills as string[])[0]
40
).toBe('run');
41
expect(
42
(deepClone(received).friends as {
43
name: string,
44
age: number,
45
}[])[0].name
46
).toBe('jack');
47
expect(
48
(deepClone(received).program as any).deepth
49
).toBe(40);
50
expect(
51
(deepClone(received) as any).program.related.website[0].name
52
).toBe('juejin');
53
54
received.name = 'received';
55
received.skills[0] = 'received';
56
received.friends[0].name = 'received';
57
received.program.deepth = 20;
58
received.program.related.website[0].name = 'received';
59
});
60
});
61
Copied!
最近更新 1yr ago
复制链接