-
Notifications
You must be signed in to change notification settings - Fork 0
/
nest-object-hard-copy.js
162 lines (130 loc) · 3.93 KB
/
nest-object-hard-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
'use strict';
var DEFAULT_CIRCULAR_VALUE = '[Circular]';
/*****************
* HELPERS *
* **************/
/**
* Dynamically sets a deeply nested value in an object.
* @function
* @param {!object} obj - The object which contains the value you want to change/set.
* @param {!array} path - The array representation of path to the value you want to change/set.
* @param {!mixed} value - The value you want to set it to.
*/
function setDeep(obj, path, value) {
var level = 0;
path.reduce((a, b)=>{
level++;
if (level === path.length){
a[b] = value;
return value;
} else {
return a[b];
}
}, obj);
}
/**
* Dynamically gets a deeply nested value from an object.
* @function
* @param {!array} path - The array representation of path to the value you want to change/set.
* @param {!object} obj - The object which contains the value you want to get.
*/
function getDeep(path, obj) {
return path.reduce(function(prev, curr) {
return prev[curr]
}, obj);
}
function checkCircular(obj){
var containCircular = false;
try {
JSON.parse(JSON.stringify(obj));
} catch (e) {
if (e instanceof TypeError && e.message.indexOf('Converting circular structure to JSON') !== -1) {
containCircular = true;
}
}
return containCircular;
}
// Not boolean, number, string, undefined, null, bigint, symbol
function isNonePrimitive(value) {
return typeof value === 'object' && value && value !== null && !Array.isArray(value);
}
function getKeyChains(obj) {
var keyChain = [];
var subKeyChain = [];
var circularKeyChain = [];
var containCircularRefs = checkCircular(obj);
var referenceObjs = [obj];
function iterateKeys(obj) {
Object.keys(obj).forEach(key => {
var currentKeyChainValue = obj[key];
if (isNonePrimitive(currentKeyChainValue)) {
if (containCircularRefs) {
if (referenceObjs.indexOf(currentKeyChainValue) > -1) {
circularKeyChain.push([...subKeyChain, key]);
return;
} else {
subKeyChain.push(key);
referenceObjs.push(currentKeyChainValue);
}
}
subKeyChain.push(key);
iterateKeys(obj[key]);
return;
}
return;
});
}
Object.keys(obj).forEach(key => {
var currentKeyChainValue = obj[key];
if ( isNonePrimitive(currentKeyChainValue)) {
if (containCircularRefs) {
if (referenceObjs.indexOf(currentKeyChainValue) > -1) {
circularKeyChain.push([key]);
return;
} else {
referenceObjs.push(currentKeyChainValue);
}
}
subKeyChain = [...[key]];
iterateKeys(obj[key]);
keyChain.push([...subKeyChain]);
} else {
keyChain.push([key]);
}
});
return {
keyChain,
circularKeyChain
};
}
/*****************
* END OF HELPERS *
* **************/
/*****************
* MAIN CLOSURE *
* **************/
function deepCopy(obj) {
var copiedObj = Object.assign({});
copiedObj.__proto__ = Object.getPrototypeOf(obj);
var keyChainAndCircularChain = getKeyChains(obj);
var keyChain = keyChainAndCircularChain.keyChain;
var circularKeyChain = keyChainAndCircularChain.circularKeyChain;
keyChain.forEach(keyLevelArray => {
copiedObj[keyLevelArray[0]] = {...obj[keyLevelArray[0]]};
keyLevelArray.forEach((currentKey, index) => {
var currentCutDownSubKeyChain = [...keyLevelArray].slice(0, index + 1);
var valueToAssign = getDeep(currentCutDownSubKeyChain, obj);
setDeep(copiedObj, currentCutDownSubKeyChain, isNonePrimitive(valueToAssign) ? {...valueToAssign} : valueToAssign);
});
});
if (circularKeyChain.length) {
circularKeyChain.forEach(keyLevelArray => {
setDeep(copiedObj, keyLevelArray, DEFAULT_CIRCULAR_VALUE);
});
}
return copiedObj;
}
/*****************
* END OF MAIN CLOSURE *
* **************/
module.exports = deepCopy;