-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
184 lines (155 loc) · 4.63 KB
/
index.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var OPtoString = Object.prototype.toString
var OPHas = Object.prototype.hasOwnProperty
var values = {
'string': true,
'number': true,
'boolean': true,
'symbol': true,
// Kinda a lie, but returning the functions unmodified
// leads to the least confusing behavior.
'function': true
}
var howDoICopy = {
'[object Object]': copyObject,
'[object Array]': copyArray,
'[object Error]': justDont,
'[object Map]': copyMap,
'[object Set]': copySet,
'[object Date]': copyConstructor,
'[object RegExp]': copyConstructor,
'[object Promise]': justDont,
'[object XMLHttpRequest]': justDont,
'[object NodeList]': copyArray,
'[object ArrayBuffer]': copySlice,
'[object Int8Array]': copyConstructor,
'[object Uint8Array]': copyConstructor,
'[object Uint8ClampedArray]': copyConstructor,
'[object Int16Array]': copyConstructor,
'[object Uint16Array]': copyConstructor,
'[object Int32Array]': copyConstructor,
'[object Uint32Array]': copyConstructor,
'[object Float32Array]': copyConstructor,
'[object Float64Array]': copyConstructor
}
module.exports = universalCopy
function universalCopy (anything) {
return deepCopy(anything, new FakeMap())
}
function deepCopy (original, seen) {
var type = typeof original
// Don't need to do anything for values
// that aren't passed by reference.
if (original == null || type in values) {
return original
}
// if this object has already been copied during
// this deep copy, use that first copy.
var extantClone = seen.get(original)
if (typeof extantClone !== 'undefined') {
return extantClone
}
// Copy buffers correctly in pre-TypedArray node versions
if (original.constructor &&
original.constructor.isBuffer &&
original.constructor.isBuffer(original)) {
return copyConstructor(original, seen)
}
if (typeof Element === 'function' &&
original instanceof Element) {
return cloneCopy(original, seen)
}
var copyX = howDoICopy[toStr(original)]
// if none of the special cases hit, copy original as a generic object.
return (copyX || copyObject)(original, seen)
}
function copyConstructor (original, seen) {
var copy = new original.constructor(original)
seen.set(original, copy)
return copy
}
function copySet (original, seen) {
var copy = new (original.constructor || Set)
seen.set(original, copy)
original.forEach(function (v) {
copy.add(deepCopy(v, seen))
})
return copy
}
function copyMap (original, seen) {
var copy = new (original.constructor || Map)
seen.set(original, copy)
original.forEach(function (v, k) {
copy.set(deepCopy(k, seen), deepCopy(v, seen))
})
return copy
}
function copySlice (original, seen) {
var copy = original.slice(0)
seen.set(original, copy)
return copy
}
function copyArray (original, seen) {
var copy = new Array(original.length)
seen.set(original, copy)
moveProps(original, copy, seen)
return copy
}
function cloneCopy (original, seen) {
var copy = original.cloneNode(true)
seen.set(original, copy)
return copy
}
function justDont (original, seen) {
return original
}
function copyObject (original, seen) {
var copy = Object.create(Object.getPrototypeOf(original))
seen.set(original, copy)
moveProps(original, copy, seen)
if (Object.isFrozen(original)) Object.freeze(copy)
if (Object.isSealed(original)) Object.seal(copy)
if (!Object.isExtensible(original)) Object.preventExtensions(copy)
return copy
}
function moveProps (original, copy, seen) {
Object.getOwnPropertyNames(original).forEach(originalToCopy)
if (typeof Object.getOwnPropertySymbols === 'function') {
Object.getOwnPropertySymbols(original).forEach(originalToCopy)
}
function originalToCopy (key) {
var descriptor = Object.getOwnPropertyDescriptor(original, key)
if (has(descriptor, 'value')) {
descriptor.value = deepCopy(descriptor.value, seen)
}
try {
Object.defineProperty(copy, key, descriptor)
} catch (e) {
// When define property fails it means we shouldn't
// have been trying to write the property we were.
// example: the stack of an error object.
}
}
}
function toStr (thing) {
return OPtoString.call(thing)
}
function has (thing, prop) {
return OPHas.call(thing, prop)
}
// Fake map only works for the few scenarios we need it for in this module.
// it is _not_ a good Map polyfil.
function FakeMap () {
if (typeof Map === 'function') return new Map()
this.keys = []
this.values = []
}
FakeMap.prototype.get = function (obj) {
var idx = this.keys.indexOf(obj)
if (idx !== -1) {
return this.values[idx]
}
}
FakeMap.prototype.set = function (key, value) {
this.keys.push(key)
this.values.push(value)
}