From 5dc4042300ec09e24c23fd8535c3a509c65e6274 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 10 Jul 2015 13:54:29 -0700 Subject: [PATCH] Fix object merge --- lib/utils.js | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 3d0c5467..3398fabd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -361,30 +361,40 @@ 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 { - dst[key] = mergeSettings(target[key], src[key]); + // 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;