MessagesForMacintosh/JS/node_modules/apollo-utilities/lib/util/mergeDeep.js

64 lines
2.1 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var hasOwnProperty = Object.prototype.hasOwnProperty;
function mergeDeep() {
var sources = [];
for (var _i = 0; _i < arguments.length; _i++) {
sources[_i] = arguments[_i];
}
return mergeDeepArray(sources);
}
exports.mergeDeep = mergeDeep;
function mergeDeepArray(sources) {
var target = sources[0] || {};
var count = sources.length;
if (count > 1) {
var pastCopies = [];
target = shallowCopyForMerge(target, pastCopies);
for (var i = 1; i < count; ++i) {
target = mergeHelper(target, sources[i], pastCopies);
}
}
return target;
}
exports.mergeDeepArray = mergeDeepArray;
function isObject(obj) {
return obj !== null && typeof obj === 'object';
}
function mergeHelper(target, source, pastCopies) {
if (isObject(source) && isObject(target)) {
if (Object.isExtensible && !Object.isExtensible(target)) {
target = shallowCopyForMerge(target, pastCopies);
}
Object.keys(source).forEach(function (sourceKey) {
var sourceValue = source[sourceKey];
if (hasOwnProperty.call(target, sourceKey)) {
var targetValue = target[sourceKey];
if (sourceValue !== targetValue) {
target[sourceKey] = mergeHelper(shallowCopyForMerge(targetValue, pastCopies), sourceValue, pastCopies);
}
}
else {
target[sourceKey] = sourceValue;
}
});
return target;
}
return source;
}
function shallowCopyForMerge(value, pastCopies) {
if (value !== null &&
typeof value === 'object' &&
pastCopies.indexOf(value) < 0) {
if (Array.isArray(value)) {
value = value.slice(0);
}
else {
value = tslib_1.__assign({ __proto__: Object.getPrototypeOf(value) }, value);
}
pastCopies.push(value);
}
return value;
}
//# sourceMappingURL=mergeDeep.js.map