]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
bubbling up values on durations when adding or subtracting
authorTim Wood <washwithcare@gmail.com>
Thu, 27 Jun 2013 18:56:30 +0000 (11:56 -0700)
committerTim Wood <washwithcare@gmail.com>
Thu, 27 Jun 2013 18:56:30 +0000 (11:56 -0700)
moment.js
test/moment/duration.js

index 15580a4ced7353880abaa163eb74ac69e560b21e..6ce46d6c8b29e7598ac6282c712d2dfb572183ca 100644 (file)
--- a/moment.js
+++ b/moment.js
 
     // Duration Constructor
     function Duration(duration) {
-        var data = this._data = {},
-            years = duration.years || duration.year || duration.y || 0,
+        var years = duration.years || duration.year || duration.y || 0,
             months = duration.months || duration.month || duration.M || 0,
             weeks = duration.weeks || duration.week || duration.w || 0,
             days = duration.days || duration.day || duration.d || 0,
         this._months = months +
             years * 12;
 
-        // The following code bubbles up values, see the tests for
-        // examples of what that means.
-        data.milliseconds = milliseconds % 1000;
-        seconds += absRound(milliseconds / 1000);
+        this._data = {};
 
-        data.seconds = seconds % 60;
-        minutes += absRound(seconds / 60);
-
-        data.minutes = minutes % 60;
-        hours += absRound(minutes / 60);
-
-        data.hours = hours % 24;
-        days += absRound(hours / 24);
-
-        days += weeks * 7;
-        data.days = days % 30;
-
-        months += absRound(days / 30);
-
-        data.months = months % 12;
-        years += absRound(months / 12);
-
-        data.years = years;
+        this._bubble();
     }
 
 
 
 
     moment.duration.fn = Duration.prototype = {
+        _bubble : function () {
+            var milliseconds = this._milliseconds,
+                days = this._days,
+                months = this._months,
+                data = this._data,
+                seconds, minutes, hours, years;
+
+            // The following code bubbles up values, see the tests for
+            // examples of what that means.
+            data.milliseconds = milliseconds % 1000;
+
+            seconds = absRound(milliseconds / 1000);
+            data.seconds = seconds % 60;
+
+            minutes = absRound(seconds / 60);
+            data.minutes = minutes % 60;
+
+            hours = absRound(minutes / 60);
+            data.hours = hours % 24;
+
+            days += absRound(hours / 24);
+            data.days = days % 30;
+
+            months += absRound(days / 30);
+            data.months = months % 12;
+
+            years = absRound(months / 12);
+            data.years = years;
+        },
+
         weeks : function () {
             return absRound(this.days() / 7);
         },
             this._days += dur._days;
             this._months += dur._months;
 
+            this._bubble();
+
             return this;
         },
 
             this._days -= dur._days;
             this._months -= dur._months;
 
+            this._bubble();
+
             return this;
         },
 
index eee2b59e1f346bce75880f1d73ae6446ce7a7b91..cbac9da851fd032a4cb58c22ebf6a569c65a614b 100644 (file)
@@ -112,7 +112,7 @@ exports.duration = {
         test.deepEqual(moment.duration(complicated), complicated, "complicated clones are equal");
         test.done();
     },
-    
+
     "instatiation from serialized C# TimeSpan zero" : function(test) {
         test.expect(6);
         test.equal(moment.duration("00:00:00").years(), 0, "0 years");
@@ -123,7 +123,7 @@ exports.duration = {
         test.equal(moment.duration("00:00:00").milliseconds(), 0, "0 milliseconds");
         test.done();
     },
-    
+
     "instatiation from serialized C# TimeSpan with days" : function(test) {
         test.expect(6);
         test.equal(moment.duration("1.02:03:04.9999999").years(), 0, "0 years");
@@ -134,7 +134,7 @@ exports.duration = {
         test.equal(moment.duration("1.02:03:04.9999999").milliseconds(), 999, "999 milliseconds");
         test.done();
     },
-    
+
     "instatiation from serialized C# TimeSpan without days" : function(test) {
         test.expect(6);
         test.equal(moment.duration("01:02:03.9999999").years(), 0, "0 years");
@@ -369,6 +369,28 @@ exports.duration = {
         test.done();
     },
 
+    "add and bubble" : function(test) {
+        test.expect(4);
+
+        test.equal(moment.duration(1, 'second').add(1000, 'milliseconds').seconds(), 2, 'Adding milliseconds should bubble up to seconds');
+        test.equal(moment.duration(1, 'minute').add(60, 'second').minutes(), 2, 'Adding seconds should bubble up to minutes');
+        test.equal(moment.duration(1, 'hour').add(60, 'minutes').hours(), 2, 'Adding minutes should bubble up to hours');
+        test.equal(moment.duration(1, 'day').add(24, 'hours').days(), 2, 'Adding hours should bubble up to days');
+
+        test.done();
+    },
+
+    "subtract and bubble" : function(test) {
+        test.expect(4);
+
+        test.equal(moment.duration(2, 'second').subtract(1000, 'milliseconds').seconds(), 1, 'Subtracting milliseconds should bubble up to seconds');
+        test.equal(moment.duration(2, 'minute').subtract(60, 'second').minutes(), 1, 'Subtracting seconds should bubble up to minutes');
+        test.equal(moment.duration(2, 'hour').subtract(60, 'minutes').hours(), 1, 'Subtracting minutes should bubble up to hours');
+        test.equal(moment.duration(2, 'day').subtract(24, 'hours').days(), 1, 'Subtracting hours should bubble up to days');
+
+        test.done();
+    },
+
     "subtract" : function(test) {
         test.expect(4);