diff --git a/index.js b/index.js index dfc9b14..f850262 100644 --- a/index.js +++ b/index.js @@ -24,14 +24,26 @@ function getMergeFunction(key, options) { return typeof customMerge === 'function' ? customMerge : deepmerge } +function getEnumerableOwnPropertySymbols(target) { + return Object.getOwnPropertySymbols ? + Object.getOwnPropertySymbols(target).filter(function(symbol) { + return target.propertyIsEnumerable(symbol) + }) + : []; +} + +function getKeys(target) { + return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target)) +} + function mergeObject(target, source, options) { var destination = {} if (options.isMergeableObject(target)) { - Object.keys(target).forEach(function(key) { + getKeys(target).forEach(function(key) { destination[key] = cloneUnlessOtherwiseSpecified(target[key], options) }) } - Object.keys(source).forEach(function(key) { + getKeys(source).forEach(function(key) { if (!options.isMergeableObject(source[key]) || !target[key]) { destination[key] = cloneUnlessOtherwiseSpecified(source[key], options) } else { diff --git a/test/merge.js b/test/merge.js index 21a68e2..27a3f9b 100644 --- a/test/merge.js +++ b/test/merge.js @@ -614,3 +614,14 @@ test('should merge correctly if custom merge is not a valid function', function( t.end() }) + +test('add symbol keys in target that do not exist at the root', function(t) { + var mySymbol = Symbol(); + var src = { [mySymbol]: 'value1' } + var target = {} + + var res = merge(target, src) + + t.deepEqual(Object.getOwnPropertySymbols(res), Object.getOwnPropertySymbols(src)) + t.end() +})