]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
implements optional threshold for a few seconds to %d seconds: #2558
authorPatrick de Wit <patrick@drillster.com>
Tue, 31 Jan 2017 11:04:20 +0000 (12:04 +0100)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 11 Mar 2017 21:56:56 +0000 (23:56 +0200)
src/lib/duration/humanize.js
src/lib/locale/relative.js
src/test/moment/relative_time.js

index 5bae088b37147f240ab811abfa996e0129827f5c..e28eeea4730fd212e6c90f0b471464374cd21c7f 100644 (file)
@@ -1,12 +1,14 @@
 import { createDuration } from './create';
 
 var round = Math.round;
+var secondsThresholdChanged = false;
 var thresholds = {
-    s: 45,  // seconds to minute
-    m: 45,  // minutes to hour
-    h: 22,  // hours to day
-    d: 26,  // days to month
-    M: 11   // months to year
+    ss: 44,         // a few seconds to seconds
+    s : 45,         // seconds to minute
+    m : 45,         // minutes to hour
+    h : 22,         // hours to day
+    d : 26,         // days to month
+    M : 11          // months to year
 };
 
 // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
@@ -23,16 +25,17 @@ function relativeTime (posNegDuration, withoutSuffix, locale) {
     var months   = round(duration.as('M'));
     var years    = round(duration.as('y'));
 
-    var a = seconds < thresholds.s && ['s', seconds]  ||
-            minutes <= 1           && ['m']           ||
-            minutes < thresholds.m && ['mm', minutes] ||
-            hours   <= 1           && ['h']           ||
-            hours   < thresholds.h && ['hh', hours]   ||
-            days    <= 1           && ['d']           ||
-            days    < thresholds.d && ['dd', days]    ||
-            months  <= 1           && ['M']           ||
-            months  < thresholds.M && ['MM', months]  ||
-            years   <= 1           && ['y']           || ['yy', years];
+    var a = seconds <= thresholds.ss && ['s', seconds]  ||
+            seconds < thresholds.s   && ['ss', seconds] ||
+            minutes <= 1             && ['m']           ||
+            minutes < thresholds.m   && ['mm', minutes] ||
+            hours   <= 1             && ['h']           ||
+            hours   < thresholds.h   && ['hh', hours]   ||
+            days    <= 1             && ['d']           ||
+            days    < thresholds.d   && ['dd', days]    ||
+            months  <= 1             && ['M']           ||
+            months  < thresholds.M   && ['MM', months]  ||
+            years   <= 1             && ['y']           || ['yy', years];
 
     a[2] = withoutSuffix;
     a[3] = +posNegDuration > 0;
@@ -60,6 +63,11 @@ export function getSetRelativeTimeThreshold (threshold, limit) {
     if (limit === undefined) {
         return thresholds[threshold];
     }
+    if (threshold === 's' && !secondsThresholdChanged) {
+        thresholds.ss = limit - 1;
+    } else if (threshold === 'ss') {
+        secondsThresholdChanged = true;
+    }
     thresholds[threshold] = limit;
     return true;
 }
index 4c485ea2224d50aa9f9287faf75ff415e8019913..431466b2da5a04c8668ba365605e139d423f54b6 100644 (file)
@@ -2,6 +2,7 @@ export var defaultRelativeTime = {
     future : 'in %s',
     past   : '%s ago',
     s  : 'a few seconds',
+    ss : '%d seconds',
     m  : 'a minute',
     mm : '%d minutes',
     h  : 'an hour',
index 7579e909f6c5d6298902a0e2b17df38d310754f6..c2bbf6fa205f8f3ccb145ff3e36faeaad3808a46 100644 (file)
@@ -102,6 +102,17 @@ test('custom thresholds', function (assert) {
 
     moment.relativeTimeThreshold('s', 45);
 
+    // A few seconds to seconds threshold
+    moment.relativeTimeThreshold('ss', 3);
+
+    a = moment();
+    a.subtract(3, 'seconds');
+    assert.equal(a.fromNow(), 'a few seconds ago', 'Below custom a few seconds to seconds threshold');
+    a.subtract(1, 'seconds');
+    assert.equal(a.fromNow(), '4 seconds ago', 'Above custom a few seconds to seconds threshold');
+
+    moment.relativeTimeThreshold('ss', 44);
+
     // Minutes to hours threshold
     moment.relativeTimeThreshold('m', 55);
     a = moment();
@@ -194,14 +205,14 @@ test('custom rounding', function (assert) {
     moment.relativeTimeRounding(roundingDefault);
 });
 
-test('retrive rounding settings', function (assert) {
+test('retrieve rounding settings', function (assert) {
     moment.relativeTimeRounding(Math.round);
     var roundingFunction = moment.relativeTimeRounding();
 
     assert.equal(roundingFunction, Math.round, 'Can retrieve rounding setting');
 });
 
-test('retrive threshold settings', function (assert) {
+test('retrieve threshold settings', function (assert) {
     moment.relativeTimeThreshold('m', 45);
     var minuteThreshold = moment.relativeTimeThreshold('m');