From 81adf44cb3b8d0af0647d94127562913fc883d8d Mon Sep 17 00:00:00 2001 From: Ivan Krechetov Date: Sat, 10 Nov 2012 12:07:09 +0100 Subject: [PATCH] Avoid mutating the passed array on initialization --- moment.js | 2 +- test/moment/create.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/moment.js b/moment.js index 87123e3f6..12ae51f48 100644 --- a/moment.js +++ b/moment.js @@ -806,7 +806,7 @@ } else if (typeof input === 'string') { makeDateFromString(config); } else if (isArray(input)) { - config._a = input; + config._a = input.slice(0); dateFromArray(config); } else { config._d = input instanceof Date ? input : new Date(input); diff --git a/test/moment/create.js b/test/moment/create.js index 099ef018b..41defc753 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -14,6 +14,14 @@ exports.create = { test.done(); }, + "array copying": function(test) { + var importantArray = [2009, 11]; + test.expect(1); + moment(importantArray); + test.deepEqual(importantArray, [2009, 11], "initializer should not mutate the original array"); + test.done(); + }, + "number" : function(test) { test.expect(3); test.ok(moment(1000).toDate() instanceof Date, "1000"); -- 2.47.2