diff --git a/lib/deap.js b/lib/deap.js index 3c2f382..6ac55f0 100755 --- a/lib/deap.js +++ b/lib/deap.js @@ -15,7 +15,9 @@ module.exports = { function clone(val) { switch(typeOf(val)) { case 'object': - return extend({}, val); + var args = slice.call(arguments); + args.unshift({}); + return extend.apply(null, args); case 'array': return [].concat(val); case 'date': @@ -30,15 +32,13 @@ function clone(val) { function deepClone(val) { switch(typeOf(val)) { case 'object': - return deepExtend({}, val); + var args = slice.call(arguments); + args.unshift({}); + return deepExtend.apply(null, args); case 'array': - return val.map(deepClone); - case 'date': - return new Date(val.getTime()); - case 'regexp': - return new RegExp(val); + return val.map(function(v) { return deepClone(v); }); default: - return val; + return clone(val); } } @@ -85,7 +85,7 @@ function deepUpdate(a, b /*, [b2..n] */) { deepUpdate(ap, bp); else if(tb === 'array' && ta === 'array') { ap.length = 0; - ap.push.apply(ap, bp.map(deepClone)); + ap.push.apply(ap, bp.map(function(v) { return deepClone(v); })); } else a[p] = deepClone(bp); } diff --git a/test/clone.test.js b/test/clone.test.js index ec12ec5..9f19d82 100644 --- a/test/clone.test.js +++ b/test/clone.test.js @@ -76,6 +76,23 @@ describe('shallow clone', function() { assert.notStrictEqual(result, a); }); + it('should work for multiple arguments', function() { + var a = { doom: 'song' }, + b = { burp: 'adurp' }, + c = { grr: { doh: 'argh' } }; + + var result = shallow(a, b, c); + + assert.deepEqual(a, { doom: 'song' }); + assert.deepEqual(b, { burp: 'adurp' }); + assert.deepEqual(c, { grr: { doh: 'argh' } }); + assert.sameMembers(Object.keys(result), ['doom', 'burp', 'grr']); + assert.equal(result.doom, a.doom); + assert.equal(result.burp, b.burp); + assert.deepEqual(result.grr, c.grr); + assert.strictEqual(result.grr, c.grr); + }); + describe('on an array', function() { it('should preserve references', function() { @@ -186,6 +203,23 @@ describe('clone', function() { assert.notStrictEqual(result, a); }); + it('should work for multiple arguments', function() { + var a = { doom: 'song' }, + b = { burp: 'adurp' }, + c = { grr: { doh: 'argh' } }; + + var result = clone(a, b, c); + + assert.deepEqual(a, { doom: 'song' }); + assert.deepEqual(b, { burp: 'adurp' }); + assert.deepEqual(c, { grr: { doh: 'argh' } }); + assert.sameMembers(Object.keys(result), ['doom', 'burp', 'grr']); + assert.equal(result.doom, a.doom); + assert.equal(result.burp, b.burp); + assert.deepEqual(result.grr, c.grr); + assert.notStrictEqual(result.grr, c.grr); + }); + describe('on an array', function() { it('should not preserve references', function() {