Support EJSON.clone(arguments).

This enables (eg) Meteor.apply('foo', arguments). Fixes #946.
This commit is contained in:
David Glasser
2013-04-15 17:24:05 -07:00
parent 49822939c2
commit 583508e10d
2 changed files with 24 additions and 5 deletions

View File

@@ -301,11 +301,9 @@ EJSON.clone = function (v) {
} }
return ret; return ret;
} }
if (_.isArray(v)) { // Clone arrays (and turn 'arguments' into an array).
ret = v.slice(0); if (_.isArray(v) || _.isArguments(v)) {
for (var i = 0; i < v.length; i++) return _.map(v, EJSON.clone);
ret[i] = EJSON.clone(ret[i]);
return ret;
} }
// handle general user-defined typed Objects if they have a clone method // handle general user-defined typed Objects if they have a clone method
if (typeof v.clone === 'function') { if (typeof v.clone === 'function') {

View File

@@ -51,3 +51,24 @@ Tinytest.add("ejson - equality and falsiness", function (test) {
test.isFalse(EJSON.equals(undefined, {foo: "foo"})); test.isFalse(EJSON.equals(undefined, {foo: "foo"}));
test.isFalse(EJSON.equals({foo: "foo"}, undefined)); 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]);
});