Permalink
Browse files

tests are passing. need 1 more test case

  • Loading branch information...
nfisher@trafficland.com
nfisher@trafficland.com committed Jan 31, 2012
1 parent 589de32 commit ae767447c5eca24e90f1df0dee047c8e7936ee0f
Showing with 83 additions and 6 deletions.
  1. +1 −0 index.js
  2. +22 −0 lib/merge.js
  3. +5 −3 package.json
  4. +51 −1 spec/unit/merge.spec.coffee
  5. +4 −2 src/merge.coffee
View
@@ -0,0 +1 @@
+module.exports = require('./lib/merge');
View
@@ -0,0 +1,22 @@
+(function() {
+ var merge;
+
+ merge = function(target, src) {
+ var key, value, _results;
+ _results = [];
+ for (key in src) {
+ value = src[key];
+ console.log("key: " + key + ", type: " + (typeof value));
+ 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
@@ -1,8 +1,8 @@
{
- "author": "",
+ "author": "Nick Fisher",
"name": "deepmerge",
"description": "A library for deep (recursive) merging of Javascript objects",
- "version": "0.0.0",
+ "version": "0.0.1",
"repository": {
"url": ""
},
@@ -11,5 +11,7 @@
"node": "~0.6.6"
},
"dependencies": {},
- "devDependencies": {}
+ "devDependencies": {
+ "jasmine-node": "~1.0.20"
+ }
}
@@ -1,2 +1,52 @@
+merge = require '../../lib/merge'
+
describe 'merge', ->
- it 'should', ->
+
+ it 'should add keys in target that do not exist at the root', ->
+ src =
+ key1: 'value1'
+ key2: 'value2'
+ target = {}
+
+
+ merge target, src
+
+ expect(target).toEqual(target)
+
+ it 'should merge existing simple keys in target at the roots', ->
+ src =
+ key1: 'changed'
+ key2: 'value2'
+ target =
+ key1: 'value1'
+ key3: 'value3'
+
+ expected =
+ key1: 'changed'
+ key2: 'value2'
+ key3: 'value3'
+
+ merge target, src
+
+ expect(target).toEqual(expected)
+
+ it 'should merge nested objects into target', ->
+ src =
+ key1:
+ subkey1: 'changed'
+ subkey3: 'added'
+ target =
+ key1:
+ subkey1: 'value1'
+ subkey2: 'value2'
+
+ expected =
+ key1:
+ subkey1: 'changed'
+ subkey2: 'value2'
+ subkey3: 'added'
+
+ merge target, src
+
+ expect(target).toEqual(expected)
+
View
@@ -1,7 +1,9 @@
-exports.merge = (target, src) ->
+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
+ merge target[key], value
+
+module.exports = merge

0 comments on commit ae76744

Please sign in to comment.