deep-copy
# deep-copy
// 深拷贝
// src 原数据
// 返回拷贝后的数据
module.exports = function deepCopy(src, cache = new WeakMap()) {
// 拷贝原始值,直接返回原始值本身
if (isPrimitiveType(src)) return src
// 解决循环引用的问题
if (cache.has(src)) return src
cache.set(src, true)
// 拷贝数组
if (isArray(src)) {
const ret = []
for (let i = 0, len = src.length; i < len; i++) {
ret.push(deepCopy(src[i], cache))
}
return ret
}
// 拷贝 Map 对象
if (isMap(src)) {
const ret = new Map()
src.forEach((value, key) => {
ret.set(key, deepCopy(value, cache))
})
return ret
}
// 拷贝函数
if (isFunction(src)) {
copyFunction(src)
}
// 拷贝对象
if (isObject(src)) {
// 获取对象上的所有key
const keys = [...Object.keys(src), ...Object.getOwnPropertySymbols(src)]
const ret = {}
// 遍历所有的key,递归调用 deepCopy 拷贝 obj[key] 的值
keys.forEach(item => {
ret[item] = deepCopy(src[item], cache)
})
// 返回拷贝后的对象
return ret
}
}
// 判断数据是否为原始值类型(Number, Boolean,String,Symbol ,BigInt ,Null ,Undefined)
function isPrimitiveType(data) {
const primitiveType = ['Number', 'Boolean', 'String', 'Symbol', 'BigInt', 'Null', 'Undefined']
return primitiveType.includes(getDataType(data))
}
// 判断数据是否为Object类型
function isObject(data) {
return getDataType(data) === 'Object'
}
// 判断数据是否为函数
function isFunction(data) {
return getDataType(data) === 'Function'
}
// 判断数据是否为数组
function isArray(data) {
return getDataType(data) === 'Array'
}
// 判断数据是否为Map
function isMap(data) {
return getDataType(data) === 'Map'
}
// 获取数据类型
// Number,Boolean,String,Symbol,BigInt,Null,Undefined,Object,Array,Function,Date...
function getDataType(data) {
return Object.prototype.toString.apply(data).slice(8, -1)
}
// 拷贝函数
function copyFunction(src) {
const fnName = src.name
let srcStr = src.toString()
// 匹配function fnName, 比如 function fnName() {}
const fnDecExp = new RegExp(`function (${fnName})?`)
// 切除匹配内容,srcStr = (xxx) {} 或 (xxx) => {}
srcStr = srcStr.replace(fnDecExp, '')
// 匹配函数参数
const argsExg = /\((.*)\)/
let args = argsExg.exec(srcStr)
// 函数体
const fnBody = srcStr.replace(argsExg, '').trim()
// { return 'test' } => return 'test'
const fnBodyCode = /^{(.*)}$/.exec(fnBody)
// 得到了函数的名称,参数,函数体,重新声明函数
return new Function(...args[1].split(','), fnBodyCode[1])
}
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
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