44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
|
/**
|
||
|
* Simple deep merge function. Takes all arguments and returns a deep copy of the objects merged
|
||
|
* together in the order provided. If an object creates a circular reference, it will assign the
|
||
|
* original reference.
|
||
|
*/
|
||
|
export function merge(target) {
|
||
|
var args = [];
|
||
|
for (var _i = 1; _i < arguments.length; _i++) {
|
||
|
args[_i - 1] = arguments[_i];
|
||
|
}
|
||
|
for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
|
||
|
var arg = args_1[_a];
|
||
|
_merge(target || {}, arg);
|
||
|
}
|
||
|
return target;
|
||
|
}
|
||
|
/**
|
||
|
* The _merge helper iterates through all props on source and assigns them to target.
|
||
|
* When the value is an object, we will create a deep clone of the object. However if
|
||
|
* there is a circular reference, the value will not be deep cloned and will persist
|
||
|
* the reference.
|
||
|
*/
|
||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||
|
function _merge(target, source, circularReferences) {
|
||
|
if (circularReferences === void 0) { circularReferences = []; }
|
||
|
circularReferences.push(source);
|
||
|
for (var name_1 in source) {
|
||
|
if (source.hasOwnProperty(name_1)) {
|
||
|
if (name_1 !== '__proto__' && name_1 !== 'constructor' && name_1 !== 'prototype') {
|
||
|
var value = source[name_1];
|
||
|
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
||
|
var isCircularReference = circularReferences.indexOf(value) > -1;
|
||
|
target[name_1] = (isCircularReference ? value : _merge(target[name_1] || {}, value, circularReferences));
|
||
|
}
|
||
|
else {
|
||
|
target[name_1] = value;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
circularReferences.pop();
|
||
|
return target;
|
||
|
}
|
||
|
//# sourceMappingURL=merge.js.map
|