-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdeepClone.js
163 lines (143 loc) · 3.24 KB
/
deepClone.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const iterableTypes = new Set(['object', 'array', 'map', 'set'])
// const notIterableTypes = new Set([
// "boolean",
// "date",
// "number",
// "string",
// "symbol",
// "error",
// "regexp",
// "function",
// ])
function _typeof (value) {
return Object.prototype.toString
.call(value)
.replace(/\[\w+\s(.*)\]/, '$1')
.toLowerCase()
}
function forEach (array, iteratee) {
let index = -1
const length = array.length
while (++index < length) {
if (iteratee(array[index], index, array) === false) { // 手动返回 false break 掉 forEeach
break
}
}
return array
}
function isObject (value) {
const type = typeof value
return value != null && (type === 'object' || type === 'function')
}
const symbolValueOf = Symbol.prototype.valueOf
function cloneSymbol (symbol) {
return Object(symbolValueOf.call(symbol))
}
function cloneRegExp (regexp) {
const reFlags = /\w*$/
const result = new regexp.constructor(regexp.source, reFlags.exec(regexp))
result.lastIndex = regexp.lastIndex
return result
}
function cloneNotIterableType (target) {
const ConstrFun = target.constructor
switch (_typeof(target)) {
case 'boolean':
case 'number':
case 'string':
case 'error':
case 'date':
return new ConstrFun(target)
case 'regexp':
return cloneRegExp(target)
case 'symbol':
return cloneSymbol(target)
case 'function':
return target
default:
return null
}
}
function deepClone (target, map = new WeakMap()) {
// clone primitive types
if (!isObject(target)) {
return target
}
const type = _typeof(target)
let cloneTarget = null
if (map.get(target)) {
return map.get(target)
}
map.set(target, cloneTarget)
if (!iterableTypes.has(type)) {
return cloneNotIterableType(target)
}
// clone Set
if (type === 'set') {
cloneTarget = new Set()
target.forEach(value => {
cloneTarget.add(deepClone(value, map))
})
return cloneTarget
}
// clone Map
if (type === 'map') {
cloneTarget = new Map()
target.forEach((value, key) => {
cloneTarget.set(key, deepClone(value, map))
})
return cloneTarget
}
// clone Array
if (type === 'array') {
cloneTarget = []
forEach(target, (value, index) => {
cloneTarget[index] = deepClone(value, map)
})
}
// clone normal Object
if (type === 'object') {
cloneTarget = {}
forEach(Object.keys(target), (key) => {
cloneTarget[key] = deepClone(target[key], map)
})
}
return cloneTarget
}
// test
const map = new Map()
map.set('key', 'value')
const set = new Set()
set.add('value1')
set.add('value2')
const target = {
field1: 1,
field2: undefined,
field3: {
child: 'child'
},
field4: [2, 4, 8],
empty: null,
map,
set,
bool: new Boolean(true),
num: new Number(2),
str: new String(2),
symbol: Object(Symbol(1)),
date: new Date(),
reg: /\d+/,
error: new Error(),
func1: () => {
console.log('hello friend!')
},
func2: function (a, b) {
return a + b
}
}
const result = deepClone(target)
console.log(result)
console.log(result.field3 === target.field3)
console.log(result.field4 === target.field4)
console.log(result.map === target.map)
console.log(result.num === target.num)
console.log(result.reg === target.reg)