From: Ash Searle Date: Tue, 8 Aug 2017 08:06:11 +0000 (+0100) Subject: Rewrite for clarity, and add more unit tests X-Git-Tag: 2.19.0~8^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=10518ac1a0278665efe63a4090729ff0846cb890;p=thirdparty%2Fmoment.git Rewrite for clarity, and add more unit tests --- diff --git a/src/lib/duration/iso-string.js b/src/lib/duration/iso-string.js index 6f706c9b0..067453975 100644 --- a/src/lib/duration/iso-string.js +++ b/src/lib/duration/iso-string.js @@ -42,17 +42,17 @@ export function toISOString() { var s = seconds ? seconds.toFixed(3).replace(/\.?0+$/, '') : ''; var total = this.asSeconds(); - var totalSign = total < 0 ? '-' : ''; - var ymSign = sign(total) === sign(this._months) ? '' : '-'; - var daysSign = sign(total) === sign(this._days) ? '' : '-'; - var hmsSign = sign(total) === sign(this._milliseconds) ? '' : '-'; - if (!total) { // this is the same as C#'s (Noda) and python (isodate)... // but not other JS (goog.date) return 'P0D'; } + var totalSign = total < 0 ? '-' : ''; + var ymSign = sign(this._months) != sign(total) ? '-' : ''; + var daysSign = sign(this._days) != sign(total) ? '-' : ''; + var hmsSign = sign(this._milliseconds) != sign(total) ? '-' : ''; + return totalSign + 'P' + (Y ? ymSign + Y + 'Y' : '') + (M ? ymSign + M + 'M' : '') + diff --git a/src/test/moment/duration.js b/src/test/moment/duration.js index fb7c907ce..9ca6c216e 100644 --- a/src/test/moment/duration.js +++ b/src/test/moment/duration.js @@ -330,6 +330,11 @@ test('serialization to ISO 8601 duration strings', function (assert) { assert.equal(moment.duration({m: -1}).toISOString(), '-PT1M', 'one minute ago'); assert.equal(moment.duration({s: -0.5}).toISOString(), '-PT0.5S', 'one half second ago'); assert.equal(moment.duration({y: -1, M: 1}).toISOString(), '-P11M', 'a month after a year ago'); + assert.equal(moment.duration({y: -1, h: 1}).toISOString(), '-P1YT-1H', 'an hour after a year ago'); + assert.equal(moment.duration({y: -1, h: 1, m: -1}).toISOString(), '-P1YT-59M', '59 minutes after a year ago'); + assert.equal(moment.duration({y: -1, h: 1, s: -1}).toISOString(), '-P1YT-59M-59S', '59 minutes 59 seconds after a year ago'); + assert.equal(moment.duration({y: -1, h: -1, s: 1}).toISOString(), '-P1YT59M59S', '59 minutes 59 seconds after a year ago'); + assert.equal(moment.duration({y: -1, d: 2}).toISOString(), '-P1Y-2D', '1 year less 2 days ago'); assert.equal(moment.duration({M: +1}).toISOString(), 'P1M', 'one month ago'); assert.equal(moment.duration({m: +1}).toISOString(), 'PT1M', 'one minute ago'); assert.equal(moment.duration({s: +0.5}).toISOString(), 'PT0.5S', 'one half second ago');