Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone with multiple args #3

Merged
merged 2 commits into from
Mar 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, that simplifies things a bit.

}
}

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); }));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious what issue this solved? I'm sure it did, but I can't figure it out.

} 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