From: Isaac Cambron Date: Mon, 8 Apr 2013 03:48:48 +0000 (-0400) Subject: unit normalization for diff() X-Git-Tag: 2.1.0~40^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=594022c690944793c19f17b8e5ef95b8e54c229b;p=thirdparty%2Fmoment.git unit normalization for diff() --- diff --git a/moment.js b/moment.js index daaafe730..81a91da57 100644 --- a/moment.js +++ b/moment.js @@ -341,7 +341,10 @@ } function normalizeUnits(units) { - return unitAliases[units] || units.toLowerCase().replace(/(.)s$/, '$1'); + if (units) { + return unitAliases[units] || units.toLowerCase().replace(/(.)s$/, '$1'); + } + return units; } @@ -1179,10 +1182,7 @@ zoneDiff = (this.zone() - that.zone()) * 6e4, diff, output; - if (units) { - // standardize on singular form - units = units.replace(/s$/, ''); - } + units = normalizeUnits(units); if (units === 'year' || units === 'month') { diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2 diff --git a/test/moment/diff.js b/test/moment/diff.js index 81edaad49..957624582 100644 --- a/test/moment/diff.js +++ b/test/moment/diff.js @@ -67,6 +67,22 @@ exports.diff = { test.done(); }, + "diff key before abbreviated" : function(test) { + test.expect(10); + + test.equal(moment([2011]).diff([2010], 'y'), 1, "year diff abbreviated"); + test.equal(moment([2010, 2]).diff([2010], 'M'), 2, "month diff abbreviated"); + test.equal(moment([2010, 0, 4]).diff([2010], 'd'), 3, "day diff abbreviated"); + test.equal(moment([2010, 0, 7]).diff([2010], 'w'), 0, "week diff abbreviated"); + test.equal(moment([2010, 0, 8]).diff([2010], 'w'), 1, "week diff abbreviated"); + test.equal(moment([2010, 0, 21]).diff([2010], 'w'), 2, "week diff abbreviated"); + test.equal(moment([2010, 0, 22]).diff([2010], 'w'), 3, "week diff abbreviated"); + test.equal(moment([2010, 0, 1, 4]).diff([2010], 'h'), 4, "hour diff abbreviated"); + test.equal(moment([2010, 0, 1, 0, 5]).diff([2010], 'm'), 5, "minute diff abbreviated"); + test.equal(moment([2010, 0, 1, 0, 0, 6]).diff([2010], 's'), 6, "second diff abbreviated"); + test.done(); + }, + "diff month" : function(test) { test.expect(1);