-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep-clone.js
84 lines (66 loc) · 1.95 KB
/
deep-clone.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
const deepClone = obj => {
// If we are dealing with a falsy value
// (null, undefined, the empty string, false, 0, etc.),
// return as-is
if(!obj) {
return obj;
}
// Check if we are dealing with a simple primitive/scalar type.
// If so, return it as-is
if(isScalar(obj)) {
return obj;
}
// If we are dealing with an object of a Built-in type,
// e.g., new Boolean(true), new Number(123), new String('Jamie'),
// normalize it to its primitives equivalent
const primitive = normalizeToPrimitive(obj);
// If we got a valid primitive of type Number, String or Boolean,
// just return it
if(typeof primitive !== "undefined") {
return primitive;
}
// We are dealing with a complex object
// (such as an array, Date, HTML DOM Node, or an object literal)
let clone;
if (isArray(obj)) {
clone = [];
obj.forEach((element, index) => clone[index] = deepClone(element));
} else if (obj instanceof Date) {
clone = new Date(obj);
} else if (obj.nodeType && typeof obj.cloneNode === "function") { // handle (HTML) DOM nodes
clone = obj.cloneNode(true);
} else { // We have an object literal
clone = {};
for(const x in obj) {
clone[x] = deepClone(obj[x]);
}
}
return clone;
};
/** Helper functions **/
function isScalar(obj) {
return (
!isArray(obj) && (typeof obj !== "object")
);
}
function isArray(data) {
return ((typeof Array.isArray === 'function')
? Array.isArray(data)
: Object.prototype.toString.call(data) === '[object Array]'
);
}
/**
* Normalize objects of the built-in types -- Boolean, Number, String --
* to their primitive equivalents.
* For example, new Boolean(true), new Number(123), new String('Jamie')
*/
function normalizeToPrimitive(obj) {
let primitive;
for(let type of [Boolean, Number, String]) {
if (obj instanceof type) {
primitive = type(obj);
break;
}
}
return primitive;
}