From: Fraser Tweedale Date: Thu, 20 Jun 2013 08:58:35 +0000 (+1000) Subject: fix durations not being cloned properly X-Git-Tag: 2.1.0~6^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F863%2Fhead;p=thirdparty%2Fmoment.git 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. --- 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(); },