Permalink
Browse files

unit normalization for diff()

  • Loading branch information...
1 parent b9c7674 commit 594022c690944793c19f17b8e5ef95b8e54c229b @icambron icambron committed Apr 8, 2013
Showing with 21 additions and 5 deletions.
  1. +5 −5 moment.js
  2. +16 −0 test/moment/diff.js
View
@@ -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
View
@@ -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);

0 comments on commit 594022c

Please sign in to comment.