]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fixed cloning a modified duration 1243/head
authorIskren Chernev <iskren.chernev@gmail.com>
Thu, 31 Oct 2013 18:30:32 +0000 (11:30 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Thu, 31 Oct 2013 18:32:10 +0000 (11:32 -0700)
Storing the _input for creating a duration doesn't work for cloning when the
original duration is modified, because input no longer corresponds to the new
duration. Instead use the ms, days and month values which uniquely identify
a duration.

This also happens to fix #1242

moment.js
test/moment/duration.js

index 4858e241bb1760a2ed0928b951c2b9a0191c3630..fd842457901a944ab9b14a36c6d01bdafa20d47f 100644 (file)
--- a/moment.js
+++ b/moment.js
             seconds = normalizedInput.second || 0,
             milliseconds = normalizedInput.millisecond || 0;
 
-        // store reference to input for deterministic cloning
-        this._input = duration;
-
         // representation for dateAddRemove
         this._milliseconds = +milliseconds +
             seconds * 1e3 + // 1000
 
     // duration
     moment.duration = function (input, key) {
-        var isDuration = moment.isDuration(input),
-            isNumber = (typeof input === 'number'),
-            duration = (isDuration ? input._input : (isNumber ? {} : input)),
+        var duration = input,
             // matching against regexp is expensive, do it on demand
             match = null,
             sign,
             timeEmpty,
             dateTimeEmpty;
 
-        if (isNumber) {
+        if (moment.isDuration(input)) {
+            duration = {
+                ms: input._milliseconds,
+                d: input._days,
+                M: input._months
+            };
+        } else if (typeof input === 'number') {
+            duration = {};
             if (key) {
                 duration[key] = input;
             } else {
 
         ret = new Duration(duration);
 
-        if (isDuration && input.hasOwnProperty('_lang')) {
+        if (moment.isDuration(input) && input.hasOwnProperty('_lang')) {
             ret._lang = input._lang;
         }
 
index d5e1d4baf4794ec620ab651d2d1e92e7c818534b..e0377054ec8f442e17d649e29a8f2b803fda449f 100644 (file)
@@ -130,12 +130,14 @@ exports.duration = {
                 minutes: 9,
                 seconds: 20,
                 milliseconds: 12
-            });
+            }),
+            modified = moment.duration(1, 'day').add(moment.duration(1, 'day'));
 
-        test.expect(3);
+        test.expect(4);
         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.deepEqual(moment.duration(modified), modified, "cloning modified duration works");
         test.done();
     },