You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.5 KiB
50 lines
1.5 KiB
'use strict';
|
|
|
|
module.exports = function replaceRecursive(items) {
|
|
const replace = (target, source) => {
|
|
const replaced = Object.assign({}, target);
|
|
|
|
const mergedKeys = Object.keys(Object.assign({}, target, source));
|
|
|
|
mergedKeys.forEach((key) => {
|
|
if (!Array.isArray(source[key]) && typeof source[key] === 'object') {
|
|
replaced[key] = replace(target[key], source[key]);
|
|
} else if (target[key] === undefined && source[key] !== undefined) {
|
|
if (typeof target[key] === 'object') {
|
|
replaced[key] = Object.assign({}, source[key]);
|
|
} else {
|
|
replaced[key] = source[key];
|
|
}
|
|
} else if (target[key] !== undefined && source[key] === undefined) {
|
|
if (typeof target[key] === 'object') {
|
|
replaced[key] = Object.assign({}, target[key]);
|
|
} else {
|
|
replaced[key] = target[key];
|
|
}
|
|
} else if (target[key] !== undefined && source[key] !== undefined) {
|
|
if (typeof source[key] === 'object') {
|
|
replaced[key] = Object.assign({}, source[key]);
|
|
} else {
|
|
replaced[key] = source[key];
|
|
}
|
|
}
|
|
});
|
|
|
|
return replaced;
|
|
};
|
|
|
|
if (!items) {
|
|
return this;
|
|
}
|
|
|
|
if (!Array.isArray(items) && typeof items !== 'object') {
|
|
return new this.constructor(replace(this.items, [items]));
|
|
}
|
|
|
|
if (items.constructor.name === 'Collection') {
|
|
return new this.constructor(replace(this.items, items.all()));
|
|
}
|
|
|
|
return new this.constructor(replace(this.items, items));
|
|
};
|