From b864420eab59b45dde8a4b9d33f9b6f81fc7b9b0 Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Thu, 20 Jun 2013 18:58:35 +1000 Subject: [PATCH] fix durations not being cloned properly ``moment.duration`` does not return an equivalent Duration for all inputs. Add a test for this and fix the issue by reusing the Duration's constructor argument. --- moment.js | 5 ++++- test/moment/duration.js | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/moment.js b/moment.js index 74b93e165..65de1d7b0 100644 --- a/moment.js +++ b/moment.js @@ -254,6 +254,9 @@ seconds = duration.seconds || duration.second || duration.s || 0, milliseconds = duration.milliseconds || duration.millisecond || duration.ms || 0; + // store reference to input for deterministic cloning + this._input = duration; + // representation for dateAddRemove this._milliseconds = milliseconds + seconds * 1e3 + // 1000 @@ -1070,7 +1073,7 @@ moment.duration = function (input, key) { var isDuration = moment.isDuration(input), isNumber = (typeof input === 'number'), - duration = (isDuration ? input._data : (isNumber ? {} : input)), + duration = (isDuration ? input._input : (isNumber ? {} : input)), matched = aspNetTimeSpanJsonRegex.exec(input), sign, ret; diff --git a/test/moment/duration.js b/test/moment/duration.js index eee2b59e1..66ba62358 100644 --- a/test/moment/duration.js +++ b/test/moment/duration.js @@ -96,6 +96,7 @@ exports.duration = { "instantiation from another duration" : function(test) { var simple = moment.duration(1234), + lengthy = moment.duration(60 * 60 * 24 * 360 * 1e3), complicated = moment.duration({ years: 2, months: 3, @@ -107,8 +108,9 @@ exports.duration = { milliseconds: 12 }); - test.expect(2); + test.expect(3); test.deepEqual(moment.duration(simple), simple, "simple clones are equal"); + test.deepEqual(moment.duration(lengthy), lengthy, "lengthy clones are equal"); test.deepEqual(moment.duration(complicated), complicated, "complicated clones are equal"); test.done(); }, -- 2.47.2