]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix wrong humanize format with weird custom relative thresholds
authorIskren Chernev <iskren.chernev@gmail.com>
Thu, 24 Sep 2015 04:36:28 +0000 (21:36 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 9 Nov 2015 02:24:34 +0000 (18:24 -0800)
src/lib/duration/humanize.js
src/test/moment/relative_time.js

index d6dd83b043a49fde5d738650dc4c9972713cdbd8..eb03311de67ffd43dd4e0368a71e75e4a37545ab 100644 (file)
@@ -24,15 +24,15 @@ function relativeTime (posNegDuration, withoutSuffix, locale) {
     var years    = round(duration.as('y'));
 
     var a = seconds < thresholds.s && ['s', seconds]  ||
-            minutes === 1          && ['m']           ||
+            minutes <= 1           && ['m']           ||
             minutes < thresholds.m && ['mm', minutes] ||
-            hours   === 1          && ['h']           ||
+            hours   <= 1           && ['h']           ||
             hours   < thresholds.h && ['hh', hours]   ||
-            days    === 1          && ['d']           ||
+            days    <= 1           && ['d']           ||
             days    < thresholds.d && ['dd', days]    ||
-            months  === 1          && ['M']           ||
+            months  <= 1           && ['M']           ||
             months  < thresholds.M && ['MM', months]  ||
-            years   === 1          && ['y']           || ['yy', years];
+            years   <= 1           && ['y']           || ['yy', years];
 
     a[2] = withoutSuffix;
     a[3] = +posNegDuration > 0;
index 0270ab1753d62d65fe8131d64333493451389b66..b2b52351e2406590daef125d8c01ac24084a1bd2 100644 (file)
@@ -80,10 +80,21 @@ test('default thresholds toNow', function (assert) {
 });
 
 test('custom thresholds', function (assert) {
+    var a;
+
+    // Seconds to minute threshold, under 30
+    moment.relativeTimeThreshold('s', 25);
+
+    a = moment();
+    a.subtract(24, 'seconds');
+    assert.equal(a.fromNow(), 'a few seconds ago', 'Below custom seconds to minute threshold, s < 30');
+    a.subtract(1, 'seconds');
+    assert.equal(a.fromNow(), 'a minute ago', 'Above custom seconds to minute threshold, s < 30');
+
     // Seconds to minutes threshold
     moment.relativeTimeThreshold('s', 55);
 
-    var a = moment();
+    a = moment();
     a.subtract(54, 'seconds');
     assert.equal(a.fromNow(), 'a few seconds ago', 'Below custom seconds to minutes threshold');
     a.subtract(1, 'seconds');