Skip to content
Browse files

fixed merge issues and incremented version #

  • Loading branch information...
2 parents 6665bc0 + 5970d95 commit c9d2d5232caa516c4654d04c8cfdfa13304eccc7 Nick Fisher committed Mar 28, 2012
Showing with 192 additions and 36 deletions.
  1. +55 −0 README.markdown
  2. +5 −0 example/merge.js
  3. +18 −1 index.js
  4. +0 −21 lib/merge.js
  5. +6 −5 package.json
  6. +0 −9 src/merge.coffee
  7. +108 −0 test/merge.js
View
55 README.markdown
@@ -0,0 +1,55 @@
+deepmerge
+=========
+
+Merge the enumerable attributes of two objects deeply.
+
+example
+=======
+
+``` js
+var merge = require('deepmerge')
+var x = { foo : { 'bar' : 3 } }
+var y = { foo : { 'baz' : 4 }, quux : 5 }
+var merged = merge(x, y)
+console.dir(merged)
+```
+
+output:
+
+```
+{ foo: { bar: 3, baz: 4 }, quux: 5 }
+```
+
+methods
+=======
+
+var merge = require('deepmerge')
+
+merge(x, y)
+-----------
+
+Merge two objects `x` and `y` deeply, returning a new merged object with the
+elements from both `x` and `y`.
+
+If an element at the same key is present for both `x` and `y`, the value from
+`y` will appear in the result.
+
+The merge is immutable, so neither `x` nor `y` will be modified.
+
+install
+=======
+
+With [npm](http://npmjs.org) do:
+
+```
+npm install deepmerge
+```
+
+test
+====
+
+With [npm](http://npmjs.org) do:
+
+```
+npm test
+```
View
5 example/merge.js
@@ -0,0 +1,5 @@
+var merge = require('../')
+var x = { foo : { 'bar' : 3 } }
+var y = { foo : { 'baz' : 4 }, quux : 5 }
+var merged = merge(x, y)
+console.dir(merged)
View
19 index.js
@@ -1 +1,18 @@
-module.exports = require('./lib/merge');
+module.exports = function merge (target, src) {
+ var dst = {}
+ if (target && typeof target === 'object') {
+ 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 {
+ dst[key] = merge(target[key], src[key])
+ }
+ })
+ return dst
+}
View
21 lib/merge.js
@@ -1,21 +0,0 @@
-(function() {
- var merge;
-
- merge = function(target, src) {
- var key, value, _results;
- _results = [];
- for (key in src) {
- value = src[key];
- if (!(value instanceof Object)) {
- _results.push(target[key] = value);
- } else {
- if (!target[key] || !(target[key] instanceof Object)) target[key] = {};
- _results.push(merge(target[key], value));
- }
- }
- return _results;
- };
-
- module.exports = merge;
-
-}).call(this);
View
11 package.json
@@ -2,20 +2,21 @@
"author": "Nick Fisher",
"name": "deepmerge",
"description": "A library for deep (recursive) merging of Javascript objects",
- "version": "0.1.0",
+ "version": "0.2.0",
"homepage": "https://github.com/nrf110/deepmerge",
"repository": {
"type": "git",
"url": "git://github.com/nrf110/deepmerge.git"
},
"main": "index",
"engines": {
- "node": "~0.6.6"
+ "node": ">=0.4.0"
+ },
+ "scripts": {
+ "test" : "tap test/*.js"
},
"dependencies": {},
"devDependencies": {
- "mocha": "1.0.0",
- "coffee-script": "1.2.0",
- "should": "0.6.0"
+ "tap" : "~0.2.4"
}
}
View
9 src/merge.coffee
@@ -1,9 +0,0 @@
-merge = (target, src) ->
- for key, value of src
- unless value instanceof Object
- target[key] = value
- else
- target[key] = {} if not target[key] or target[key] not instanceof Object
- merge target[key], value
-
-module.exports = merge
View
108 test/merge.js
@@ -0,0 +1,108 @@
+var merge = require('../')
+var test = require('tap').test
+
+test('add keys in target that do not exist at the root', function (t) {
+ var src = { key1 : 'value1', key2 : 'value2' }
+ target = {}
+
+ var res = merge(target, src)
+
+ t.deepEqual(target, {}, 'merge should be immutable')
+ t.deepEqual(res, src)
+ t.end()
+})
+
+test('merge existing simple keys in target at the roots', function (t) {
+ var src = { key1 : 'changed', key2 : 'value2' }
+ var target = { key1: 'value1', key3: 'value3' }
+
+ var expected = {
+ key1 : 'changed',
+ key2 : 'value2',
+ key3 : 'value3',
+ }
+
+ t.deepEqual(target, { key1: 'value1', key3: 'value3' })
+ t.deepEqual(merge(target, src), expected)
+ t.end()
+})
+
+test('merge nested objects into target', function (t) {
+ var src = {
+ key1 : {
+ subkey1 : 'changed',
+ subkey3 : 'added',
+ }
+ }
+ var target = {
+ key1 : {
+ subkey1 : 'value1',
+ subkey2 : 'value2',
+ }
+ }
+
+ var expected = {
+ key1 : {
+ subkey1: 'changed',
+ subkey2: 'value2',
+ subkey3: 'added',
+ }
+ }
+
+ t.deepEqual(target, {
+ key1 : {
+ subkey1 : 'value1',
+ subkey2 : 'value2',
+ }
+ })
+ t.deepEqual(merge(target, src), expected)
+ t.end()
+})
+
+test('replace simple key with nested object in target', function (t) {
+ var src = {
+ key1 : {
+ subkey1 : 'subvalue1',
+ subkey2 : 'subvalue2',
+ }
+ }
+ var target = {
+ key1 : 'value1',
+ key2 : 'value2',
+ }
+
+ var expected = {
+ key1 : {
+ subkey1 : 'subvalue1',
+ subkey2 : 'subvalue2',
+ },
+ key2 : 'value2',
+ }
+
+ t.deepEqual(target, { key1 : 'value1', key2 : 'value2' })
+ 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 = {
+ key1 : {
+ subkey1: 'subvalue1',
+ subkey2: 'subvalue2',
+ },
+ key2 : 'value2',
+ }
+
+ var expected = { key1 : 'value1', key2 : 'value2' }
+
+ t.deepEqual(target, {
+ key1 : {
+ subkey1: 'subvalue1',
+ subkey2: 'subvalue2',
+ },
+ key2 : 'value2',
+ });
+ t.deepEqual(merge(target, src), expected)
+ t.end()
+})

0 comments on commit c9d2d52

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