Skip to content
Browse files

fixed bug with merging nested objects

  • Loading branch information...
1 parent 8805ce0 commit e0e2ff61055ee23b5e40efb44041b431841c7320 Nick Fisher committed
Showing with 28 additions and 3 deletions.
  1. +5 −2 index.js
  2. +1 −1 package.json
  3. +22 −0 test/merge.js
View
7 index.js
@@ -20,13 +20,16 @@ module.exports = function merge (target, src) {
dst[key] = target[key]
})
}
-
Object.keys(src).forEach(function (key) {
if (typeof src[key] !== 'object' || !src[key]) {
dst[key] = src[key]
}
else {
- dst[key] = merge(target[key], src[key])
+ if (!target[key]) {
+ dst[key] = src[key]
+ } else {
+ dst[key] = merge(target[key], src[key])
+ }
}
})
}
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Nick Fisher",
"name": "deepmerge",
"description": "A library for deep (recursive) merging of Javascript objects",
- "version": "0.2.4",
+ "version": "0.2.5",
"homepage": "https://github.com/nrf110/deepmerge",
"repository": {
"type": "git",
View
22 test/merge.js
@@ -84,6 +84,28 @@ test('replace simple key with nested object in target', function (t) {
t.end()
})
+test('should add nested object in target', function(t) {
+ var src = {
+ "b": {
+ "c": {}
+ }
+ }
+
+ var target = {
+ "a": {}
+ }
+
+ var expected = {
+ "a": {},
+ "b": {
+ "c": {}
+ }
+ }
+
+ t.deepEqual(merge(target, src), expected)
+ t.end()
+})
+
test('should replace object with simple key in target', function (t) {
var src = { key1: 'value1' }
var target = {

0 comments on commit e0e2ff6

Please sign in to comment.
Something went wrong with that request. Please try again.