Skip to content

Commit

Permalink
Fixed _.clone() support for arguments object (using Array slice).
Browse files Browse the repository at this point in the history
Added tests for the changes to clone().
  • Loading branch information
cederberg committed May 6, 2012
1 parent 4118285 commit 536efe2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions test/objects.js
Expand Up @@ -79,6 +79,18 @@ $(document).ready(function() {
equal(_.clone(undefined), void 0, 'non objects should not be changed by clone');
equal(_.clone(1), 1, 'non objects should not be changed by clone');
equal(_.clone(null), null, 'non objects should not be changed by clone');
equal(_.clone(_.clone), _.clone, 'functions should not be changed by clone');

var dttm = new Date(4711)
equal(_.clone(dttm).toString(), dttm.toString(), 'dates should be cloned to new instance');
notStrictEqual(_.clone(dttm), dttm, 'dates should be cloned to new instance');

var re = /test/im
equal(_.clone(re).toString(), re.toString(), 'regexps should be cloned to new instance');
notStrictEqual(_.clone(re), re, 'regexps should be cloned to new instance');

var func = function () { return _.clone(arguments); };
deepEqual(func(1, 2, 3), [1, 2, 3], 'argument object is cloned to array');
});

test("objects: isEqual", function() {
Expand Down
3 changes: 2 additions & 1 deletion underscore.js
Expand Up @@ -675,9 +675,10 @@
// Create a (shallow-cloned) duplicate of an object.
_.clone = function(obj) {
if (!_.isObject(obj) || _.isFunction(obj)) return obj;
if (_.isArray(obj) || _.isArguments(obj)) return slice.call(obj);
if (_.isDate(obj)) return new Date(obj.getTime());
if (_.isRegExp(obj)) return new RegExp(obj.source, obj.toString().replace(/.*\//, ""));
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
return _.extend({}, obj);
};

// Invokes interceptor with the obj, and then returns obj.
Expand Down

0 comments on commit 536efe2

Please sign in to comment.