diff --git a/README.md b/README.md index 0533b71..a8d599a 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ cloned.x.y++; console.log(original.x.y, cloned.x.y); // -> 1, 2 + +console.log(merge.recursive(true, original, { x: { z: 2 } })); +// -> {"x": { "y": 1, "z": 2 } } + ``` ## Browser Usage @@ -40,6 +44,10 @@ console.log(original.x.y, cloned.x.y); console.log(original.x.y, cloned.x.y); // -> 1, 2 + + console.log(merge.recursive(true, original, { x: { z: 2 } })); + // -> {"x": { "y": 1, "z": 2 } } + ``` diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..bd5de03 --- /dev/null +++ b/bower.json @@ -0,0 +1,22 @@ +{ + "name": "merge", + "version": "1.2.0", + "homepage": "https://github.com/yeikos/js.merge", + "authors": [ + "yeikos " + ], + "description": "Merge multiple objects into one, optionally creating a new cloned object. Similar to the jQuery.extend but more flexible. Works in Node.js and the browser.", + "main": "merge.js", + "keywords": [ + "merge", + "recursive", + "extend", + "clone", + "object", + "browser" + ], + "license": "MIT", + "ignore": [ + "tests" + ] +} diff --git a/merge.js b/merge.js index 4fef370..d856db6 100644 --- a/merge.js +++ b/merge.js @@ -1,5 +1,5 @@ /*! - * @name JavaScript/NodeJS Merge v1.1.3 + * @name JavaScript/NodeJS Merge v1.2.0 * @author yeikos * @repository https://github.com/yeikos/js.merge @@ -9,31 +9,39 @@ ;(function(isNode) { - function merge() { + /** + * Merge one or more objects + * @param bool? clone + * @param mixed,... arguments + * @return object + */ - var items = Array.prototype.slice.call(arguments), - result = items.shift(), - deep = (result === true), - size = items.length, - item, index, key; + var Public = function(clone) { - if (deep || typeOf(result) !== 'object') + return merge(clone === true, false, arguments); - result = {}; - - for (index=0;index - JavaScript/NodeJS Merge v1.1.3 + JavaScript/NodeJS Merge v1.2.0 - - - + + + diff --git a/tests/tests.js b/tests/tests.js index 7611793..22959aa 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,108 +1,196 @@ test('merge', function() { - deepEqual( + deepEqual( - merge({ a: 1 }, { b: 2 }), + merge({ a: 1 }, { b: 2 }), - { a: 1, b: 2 } + { a: 1, b: 2 } - ); + ); - deepEqual( + deepEqual( - merge({ a: 1 }, { b: { c: { d: 2 } } }), + merge({ a: 1 }, { b: { c: { d: 2 } } }), - { a: 1, b: { c: { d: 2 } } } + { a: 1, b: { c: { d: 2 } } } - ); + ); - deepEqual( + deepEqual( - merge(), {} + merge(), {} - ); + ); - deepEqual( + deepEqual( - merge(undefined), {} + merge(undefined), {} - ); + ); - deepEqual( + deepEqual( - merge([]), {} + merge([]), {} - ); + ); - deepEqual( + deepEqual( - merge(true), {} + merge(true), {} - ); + ); - deepEqual( + deepEqual( - merge(null, true, [0, 1, 2], 3, { a: 1 }, function() {}, undefined, { b: 2 }), + merge(null, true, [0, 1, 2], 3, { a: 1 }, function() {}, undefined, { b: 2 }), - { a: 1, b: 2 } + { a: 1, b: 2 } - ); + ); }); -test('merge (deep)', function() { +test('merge (clone)', function() { - var input = { + var input = { - a: 1, - b: { + a: 1, + b: { - c: { + c: { - d: 2, + d: 2, - e: ['x', 'y', { z: { w: ['k'] }}] + e: ['x', 'y', { z: { w: ['k'] }}] - } + } - }, + }, - f: null, - g: undefined, - h: true + f: null, + g: undefined, + h: true - }, original = { + }, original = { - a: 1, - b: { + a: 1, + b: { - c: { + c: { - d: 2, + d: 2, - e: ['x', 'y', { z: { w: ['k'] }}] + e: ['x', 'y', { z: { w: ['k'] }}] - } + } - }, + }, - f: null, - g: undefined, - h: true + f: null, + g: undefined, + h: true - }, output = merge(true, input); + }, output = merge(true, input); - input.b.c.d++; - input.b.c.e[2].z.w = null; - input.h = null; + input.b.c.d++; + input.b.c.e[2].z.w = null; + input.h = null; - deepEqual(original, output); + deepEqual(original, output); - input = original; + input = original; - output = merge(true, input, { a: 2 }); + output = merge(true, input, { a: 2 }); - deepEqual(output.a, 2); - deepEqual(input.a, 1); + deepEqual(output.a, 2); + deepEqual(input.a, 1); + +}); + +test('merge.recursive', function() { + + deepEqual( + + merge.recursive({ a: { b: 1 } }, { a : { c: 1 }}), + + { a: { b: 1, c: 1 } } + + ); + + deepEqual( + + merge.recursive({ a: { b: 1, c: 1 } }, { a : { b: 2 }}), + + { a: { b: 2, c: 1 } } + + ); + + deepEqual( + + merge.recursive({ a: { b: [1, 2, 3], c: 1 } }, { a : { b: ['a'] }}), + + { a: { b: ['a'], c: 1 } } + + ); + + deepEqual( + + merge.recursive({ a: { b: { b: 2 }, c: 1 } }, { a : { b: 2 }}), + + { a: { b: 2, c: 1 } } + + ); + +}); + +test('merge.recursive (clone)', function() { + + var input = { a: { b: 1 } }; + + deepEqual( + + merge.recursive(true, input, { a : { c: 1 }}), + + { a: { b: 1, c: 1 } } + + ); + + deepEqual({ a: { b: 1 } }, input); + + input = { a: { b: 1, c: 1 } }; + + deepEqual( + + merge.recursive(true, input, { a : { b: 2 }}), + + { a: { b: 2, c: 1 } } + + ); + + deepEqual({ a: { b: 1, c: 1 } }, input); + + input = { a: { b: [1, 2, 3], c: 1 } }; + + deepEqual( + + merge.recursive(true, input, { a : { b: ['a'] }}), + + { a: { b: ['a'], c: 1 } } + + ); + + deepEqual({ a: { b: [1, 2, 3], c: 1 } }, input); + + input = { a: { b: { b: 2 }, c: 1 } }; + + deepEqual( + + merge.recursive(true, input, { a : { b: 2 }}), + + { a: { b: 2, c: 1 } } + + ); + + deepEqual({ a: { b: { b: 2 }, c: 1 } }, input); }); \ No newline at end of file