Fix object merge

This commit is contained in:
Raymond Feng 2015-07-10 13:54:29 -07:00
parent 9d653c1a45
commit 5dc4042300
1 changed files with 23 additions and 13 deletions

View File

@ -361,31 +361,41 @@ function mergeSettings(target, src) {
if (array) {
target = target || [];
// Add items from target into dst
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) {
dst.push(e);
}
});
} else {
if (target && typeof target === 'object') {
Object.keys(target).forEach(function (key) {
if (target != null && typeof target === 'object') {
// Add properties from target to dst
Object.keys(target).forEach(function(key) {
dst[key] = target[key];
});
}
Object.keys(src).forEach(function (key) {
if (typeof src[key] !== 'object' || !src[key]) {
dst[key] = src[key];
}
else {
if (!target[key]) {
dst[key] = src[key]
if (src != null && typeof src === 'object') {
// Source is an object
Object.keys(src).forEach(function(key) {
var srcValue = src[key];
if (srcValue == null || typeof srcValue !== 'object') {
// The source item value is null, undefined or not an object
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 {
dst[key] = mergeSettings(target[key], src[key]);
}
}
});
}
}
return dst;
}