-
Notifications
You must be signed in to change notification settings - Fork 12
/
objectAssignDeep.js
150 lines (117 loc) · 3.7 KB
/
objectAssignDeep.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
'use strict';
/*
* OBJECT ASSIGN DEEP
* Allows deep cloning of plain objects that contain primitives, nested plain objects, or nested plain arrays.
*/
/*
* A unified way of returning a string that describes the type of the given variable.
*/
function getTypeOf (input) {
if (input === null) {
return 'null';
}
else if (typeof input === 'undefined') {
return 'undefined';
}
else if (typeof input === 'object') {
return (Array.isArray(input) ? 'array' : 'object');
}
return typeof input;
}
/*
* Branching logic which calls the correct function to clone the given value base on its type.
*/
function cloneValue (value) {
// The value is an object so lets clone it.
if (getTypeOf(value) === 'object') {
return quickCloneObject(value);
}
// The value is an array so lets clone it.
else if (getTypeOf(value) === 'array') {
return quickCloneArray(value);
}
// Any other value can just be copied.
return value;
}
/*
* Enumerates the given array and returns a new array, with each of its values cloned (i.e. references broken).
*/
function quickCloneArray (input) {
return input.map(cloneValue);
}
/*
* Enumerates the properties of the given object (ignoring the prototype chain) and returns a new object, with each of
* its values cloned (i.e. references broken).
*/
function quickCloneObject (input) {
const output = {};
for (const key in input) {
if (!input.hasOwnProperty(key)) { continue; }
output[key] = cloneValue(input[key]);
}
return output;
}
/*
* Does the actual deep merging.
*/
function executeDeepMerge (target, _objects = [], _options = {}) {
const options = {
arrayBehaviour: _options.arrayBehaviour || 'replace', // Can be "merge" or "replace".
};
// Ensure we have actual objects for each.
const objects = _objects.map(object => object || {});
const output = target || {};
// Enumerate the objects and their keys.
for (let oindex = 0; oindex < objects.length; oindex++) {
const object = objects[oindex];
const keys = Object.keys(object);
for (let kindex = 0; kindex < keys.length; kindex++) {
const key = keys[kindex];
const value = object[key];
const type = getTypeOf(value);
const existingValueType = getTypeOf(output[key]);
if (type === 'object') {
if (existingValueType !== 'undefined') {
const existingValue = (existingValueType === 'object' ? output[key] : {});
output[key] = executeDeepMerge({}, [existingValue, quickCloneObject(value)], options);
}
else {
output[key] = quickCloneObject(value);
}
}
else if (type === 'array') {
if (existingValueType === 'array') {
const newValue = quickCloneArray(value);
output[key] = (options.arrayBehaviour === 'merge' ? output[key].concat(newValue) : newValue);
}
else {
output[key] = quickCloneArray(value);
}
}
else {
output[key] = value;
}
}
}
return output;
}
/*
* Merge all the supplied objects into the target object, breaking all references, including those of nested objects
* and arrays, and even objects nested inside arrays. The first parameter is not mutated unlike Object.assign().
* Properties in later objects will always overwrite.
*/
module.exports = function objectAssignDeep (target, ...objects) {
return executeDeepMerge(target, objects);
};
/*
* Same as objectAssignDeep() except it doesn't mutate the target object and returns an entirely new object.
*/
module.exports.noMutate = function objectAssignDeepInto (...objects) {
return executeDeepMerge({}, objects);
};
/*
* Allows an options object to be passed in to customise the behaviour of the function.
*/
module.exports.withOptions = function objectAssignDeepInto (target, objects, options) {
return executeDeepMerge(target, objects, options);
};