Fix object merge
This commit is contained in:
parent
9d653c1a45
commit
5dc4042300
30
lib/utils.js
30
lib/utils.js
|
@ -361,31 +361,41 @@ function mergeSettings(target, src) {
|
||||||
|
|
||||||
if (array) {
|
if (array) {
|
||||||
target = target || [];
|
target = target || [];
|
||||||
|
// Add items from target into dst
|
||||||
dst = dst.concat(target);
|
dst = dst.concat(target);
|
||||||
src.forEach(function (e) {
|
// Add non-existent items from source into dst
|
||||||
|
src.forEach(function(e) {
|
||||||
if (dst.indexOf(e) === -1) {
|
if (dst.indexOf(e) === -1) {
|
||||||
dst.push(e);
|
dst.push(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (target && typeof target === 'object') {
|
if (target != null && typeof target === 'object') {
|
||||||
Object.keys(target).forEach(function (key) {
|
// Add properties from target to dst
|
||||||
|
Object.keys(target).forEach(function(key) {
|
||||||
dst[key] = target[key];
|
dst[key] = target[key];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Object.keys(src).forEach(function (key) {
|
if (src != null && typeof src === 'object') {
|
||||||
if (typeof src[key] !== 'object' || !src[key]) {
|
// Source is an object
|
||||||
dst[key] = src[key];
|
Object.keys(src).forEach(function(key) {
|
||||||
}
|
var srcValue = src[key];
|
||||||
else {
|
if (srcValue == null || typeof srcValue !== 'object') {
|
||||||
if (!target[key]) {
|
// The source item value is null, undefined or not an object
|
||||||
dst[key] = src[key]
|
dst[key] = srcValue;
|
||||||
|
} else {
|
||||||
|
// The source item value is an object
|
||||||
|
if (target == null || typeof target !== 'object' ||
|
||||||
|
target[key] == null) {
|
||||||
|
// If target is not an object or target item value
|
||||||
|
dst[key] = srcValue;
|
||||||
} else {
|
} else {
|
||||||
dst[key] = mergeSettings(target[key], src[key]);
|
dst[key] = mergeSettings(target[key], src[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue