From: Tim Wood Date: Thu, 27 Jun 2013 18:56:30 +0000 (-0700) Subject: bubbling up values on durations when adding or subtracting X-Git-Tag: 2.1.0~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=875650f4b5e61e95fe024ba036b6e9e17c817392;p=thirdparty%2Fmoment.git bubbling up values on durations when adding or subtracting --- diff --git a/moment.js b/moment.js index 15580a4ce..6ce46d6c8 100644 --- a/moment.js +++ b/moment.js @@ -244,8 +244,7 @@ // 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, @@ -269,29 +268,9 @@ 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(); } @@ -1522,6 +1501,36 @@ 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); }, @@ -1552,6 +1561,8 @@ this._days += dur._days; this._months += dur._months; + this._bubble(); + return this; }, @@ -1562,6 +1573,8 @@ this._days -= dur._days; this._months -= dur._months; + this._bubble(); + return this; }, diff --git a/test/moment/duration.js b/test/moment/duration.js index eee2b59e1..cbac9da85 100644 --- a/test/moment/duration.js +++ b/test/moment/duration.js @@ -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);