forked from smartface/sf-extension-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.js
168 lines (147 loc) · 5.28 KB
/
copy.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
/**
* Smartface Copy helper module
* @module copy
* @type {function}
* @author Alper Ozisik <alper.ozisik@smartface.io>
* @copyright Smartface 2018
*/
const isFunction = value => typeof value === 'function';
const isObject = value => value !== null && typeof value === 'object';
const isArray = value => value instanceof Array;
const getPrototypeOf = value => value.__proto__ || value.constructor.prototype;
const isBlankObject = value => value !== null && typeof value === 'object' && !getPrototypeOf(value);
/**
* Creates a deep high-performing copy of a variable
* @public
* @function copy
* @params {*} source
* @params {*} [destination]
* @returns {*} copy of the source
* @example
* const copy = require("sf-extension-utils/lib/copy");
* var src = {nested: {x: 4}}; //deep nested object
* var cpy = copy(src);
*
* console.log(src === cpy); //false
* console.log(src.nested === cpy.nested); //false
* @example
* const copy = require("sf-extension-utils/lib/copy");
* var src = {nested: {x: 4}}; //deep nested object
* var cpy;
* //targeting
* copy(src, cpy);
*/
function copy(source, destination) {
var stackSource = [];
var stackDest = [];
if (destination) {
// Empty the destination object
if (isArray(destination)) {
destination.length = 0;
}
stackSource.push(source);
stackDest.push(destination);
return copyRecurse(source, destination);
}
return copyElement(source);
function copyRecurse(source, destination) {
var key;
if (isArray(source)) {
for (var i = 0, ii = source.length; i < ii; i++) {
destination.push(copyElement(source[i]));
}
}
else if (isBlankObject(source)) {
// createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
for (key in source) {
destination[key] = copyElement(source[key]);
}
}
else if (source && typeof source.hasOwnProperty === 'function') {
// Slow path, which must rely on hasOwnProperty
for (key in source) {
if (source.hasOwnProperty(key)) {
destination[key] = copyElement(source[key]);
}
}
}
else {
// Slowest path --- hasOwnProperty can't be called as a method
for (key in source) {
if (hasOwnProperty.call(source, key)) {
destination[key] = copyElement(source[key]);
}
}
}
// setHashKey(destination, h);
return destination;
}
function copyElement(source) {
// Simple values
if (!isObject(source)) {
return source;
}
// Already copied values
var index = stackSource.indexOf(source);
if (index !== -1) {
return stackDest[index];
}
// if (isWindow(source) || isScope(source)) {
// throw ngMinErr('cpws',
// 'Can\'t copy! Making copies of Window or Scope instances is not supported.');
// }
var needsRecurse = false;
var destination = copyType(source);
if (destination === undefined) {
destination = isArray(source) ? [] : Object.create(getPrototypeOf(source));
needsRecurse = true;
}
stackSource.push(source);
stackDest.push(destination);
return needsRecurse ?
copyRecurse(source, destination) :
destination;
}
function copyType(source) {
switch (toString.call(source)) {
case '[object Int8Array]':
case '[object Int16Array]':
case '[object Int32Array]':
case '[object Float32Array]':
case '[object Float64Array]':
case '[object Uint8Array]':
case '[object Uint8ClampedArray]':
case '[object Uint16Array]':
case '[object Uint32Array]':
return new source.constructor(copyElement(source.buffer), source.byteOffset, source.length);
case '[object ArrayBuffer]':
// Support: IE10
if (!source.slice) {
// If we're in this case we know the environment supports ArrayBuffer
/* eslint-disable no-undef */
var copied = new ArrayBuffer(source.byteLength);
new Uint8Array(copied).set(new Uint8Array(source));
/* eslint-enable */
return copied;
}
return source.slice(0);
case '[object Boolean]':
case '[object Number]':
case '[object String]':
case '[object Date]':
return new source.constructor(source.valueOf());
case '[object RegExp]':
var re = new RegExp(source.source, source.toString().match(/[^/]*$/)[0]);
re.lastIndex = source.lastIndex;
return re;
case '[object Blob]':
return new source.constructor([source], {
type: source.type
});
}
if (isFunction(source.cloneNode)) {
return source.cloneNode(true);
}
}
}
module.exports = exports = copy;