39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
||
|
|
||
|
module.exports = function mergeRecursive(items) {
|
||
|
var merge = function merge(target, source) {
|
||
|
var merged = {};
|
||
|
|
||
|
var mergedKeys = Object.keys(Object.assign({}, target, source));
|
||
|
|
||
|
mergedKeys.forEach(function (key) {
|
||
|
if (target[key] === undefined && source[key] !== undefined) {
|
||
|
merged[key] = source[key];
|
||
|
} else if (target[key] !== undefined && source[key] === undefined) {
|
||
|
merged[key] = target[key];
|
||
|
} else if (target[key] !== undefined && source[key] !== undefined) {
|
||
|
if (target[key] === source[key]) {
|
||
|
merged[key] = target[key];
|
||
|
} else if (!Array.isArray(target[key]) && _typeof(target[key]) === 'object' && !Array.isArray(source[key]) && _typeof(source[key]) === 'object') {
|
||
|
merged[key] = merge(target[key], source[key]);
|
||
|
} else {
|
||
|
merged[key] = [].concat(target[key], source[key]);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return merged;
|
||
|
};
|
||
|
|
||
|
if (!items) {
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
if (items.constructor.name === 'Collection') {
|
||
|
return new this.constructor(merge(this.items, items.all()));
|
||
|
}
|
||
|
|
||
|
return new this.constructor(merge(this.items, items));
|
||
|
};
|