Skip to content

Commit

Permalink
Support EJSON.clone(arguments).
Browse files Browse the repository at this point in the history
This enables (eg) Meteor.apply('foo', arguments). Fixes meteor#946.
  • Loading branch information
glasser committed Apr 16, 2013
1 parent 4982293 commit 583508e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
8 changes: 3 additions & 5 deletions packages/ejson/ejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,9 @@ EJSON.clone = function (v) {
}
return ret;
}
if (_.isArray(v)) {
ret = v.slice(0);
for (var i = 0; i < v.length; i++)
ret[i] = EJSON.clone(ret[i]);
return ret;
// Clone arrays (and turn 'arguments' into an array).
if (_.isArray(v) || _.isArguments(v)) {
return _.map(v, EJSON.clone);
}
// handle general user-defined typed Objects if they have a clone method
if (typeof v.clone === 'function') {
Expand Down
21 changes: 21 additions & 0 deletions packages/ejson/ejson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,24 @@ Tinytest.add("ejson - equality and falsiness", function (test) {
test.isFalse(EJSON.equals(undefined, {foo: "foo"}));
test.isFalse(EJSON.equals({foo: "foo"}, undefined));
});

Tinytest.add("ejson - clone", function (test) {
var cloneTest = function (x, identical) {
var y = EJSON.clone(x);
test.isTrue(EJSON.equals(x, y));
test.equal(x === y, !!identical);
};
cloneTest(null, true);
cloneTest(undefined, true);
cloneTest(42, true);
cloneTest("asdf", true);
cloneTest([1, 2, 3]);
cloneTest([1, "fasdf", {foo: 42}]);
cloneTest({x: 42, y: "asdf"});

var testCloneArgs = function (/*arguments*/) {
var clonedArgs = EJSON.clone(arguments);
test.equal(clonedArgs, [1, 2, "foo", [4]]);
};
testCloneArgs(1, 2, "foo", [4]);
});

0 comments on commit 583508e

Please sign in to comment.