Skip to content

Commit

Permalink
Allowing multiple args passed to clone functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Prestaul committed Mar 21, 2014
1 parent b5da55e commit 6c57d85
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
12 changes: 8 additions & 4 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,9 +32,11 @@ 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);
return val.map(function(v) { return deepClone(v); });
case 'date':
return new Date(val.getTime());
case 'regexp':
Expand Down Expand Up @@ -85,7 +89,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 6c57d85

Please sign in to comment.