Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tests are passing. need 1 more test case

  • Loading branch information...
commit ae767447c5eca24e90f1df0dee047c8e7936ee0f 1 parent 589de32
nfisher@trafficland.com authored
View
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/merge');
View
22 lib/merge.js
@@ -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
8 package.json
@@ -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"
+ }
}
View
52 spec/unit/merge.spec.coffee
@@ -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
6 src/merge.coffee
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.