Skip to content

Commit

Permalink
Merge pull request #3 from selfcontained/clone-multiple-args
Browse files Browse the repository at this point in the history
Clone with multiple args
  • Loading branch information
selfcontained committed Mar 21, 2014
2 parents dde7ac2 + 8686aa3 commit ed2b114
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/deap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
Expand Down
34 changes: 34 additions & 0 deletions test/clone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit ed2b114

Please sign in to comment.