]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[feature] Accept custom relative thresholds in duration.humanize
authorTed Driggs <ted.driggs@outlook.com>
Wed, 8 Nov 2017 17:56:31 +0000 (09:56 -0800)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 25 Apr 2020 22:17:06 +0000 (01:17 +0300)
Fixes #4295

moment.d.ts
src/lib/duration/humanize.js
src/test/moment/duration.js

index 9198eda6bd173d097169d68d5537a38a3e18f7d1..78a76b2adcf63d8e7dacd2269f933ea4d0734cdf 100644 (file)
@@ -154,10 +154,23 @@ declare namespace moment {
     milliseconds: number;
   }
 
+  interface HumanizeOptions {
+    withSuffix?: boolean;
+    thresholds?: {
+      ss?: number;
+      s?: number;
+      m?: number;
+      h?: number;
+      d?: number;
+      w?: number | void;
+      M?: number;
+    };
+  }
+
   interface Duration {
     clone(): Duration;
 
-    humanize(withSuffix?: boolean): string;
+    humanize(withSuffixOrOptions?: boolean | HumanizeOptions): string;
 
     abs(): Duration;
 
index 947bb91fbaddceb6b1043278e94d64e87215bbc9..e0e3d8a6c2c961fcc84bd772aa14b1431f8de306 100644 (file)
@@ -16,7 +16,7 @@ function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
     return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
 }
 
-function relativeTime (posNegDuration, withoutSuffix, locale) {
+function relativeTime (posNegDuration, withoutSuffix, thresholds, locale) {
     var duration = createDuration(posNegDuration).abs();
     var seconds  = round(duration.as('s'));
     var minutes  = round(duration.as('m'));
@@ -78,13 +78,35 @@ export function getSetRelativeTimeThreshold (threshold, limit) {
     return true;
 }
 
-export function humanize (withSuffix) {
+export function humanize (withSuffixOrOptions) {
     if (!this.isValid()) {
         return this.localeData().invalidDate();
     }
 
+    var withSuffix = false;
+    var th = thresholds;
+
+    if (typeof withSuffixOrOptions === 'boolean') {
+        withSuffix = withSuffixOrOptions;
+    }
+    else if (typeof withSuffixOrOptions === 'object') {
+        var ws = withSuffixOrOptions.withSuffix;
+        if (typeof ws === 'boolean') {
+            withSuffix = ws;
+        }
+
+        var t = withSuffixOrOptions.thresholds;
+        if (typeof t === 'object') {
+            // Fill in missing keys with the current values
+            th = Object.assign({}, thresholds, t);
+            if (typeof t.s === 'number') {
+                th.ss = t.s - 1;
+            }
+        }
+    }
+
     var locale = this.localeData();
-    var output = relativeTime(this, !withSuffix, locale);
+    var output = relativeTime(this, !withSuffix, th, locale);
 
     if (withSuffix) {
         output = locale.pastFuture(+this, output);
index 135a9a3c2ebdb40c7422b321de74c5d1fd81f4b8..e0f5212ea7572062516effff999035de506610b5 100644 (file)
@@ -464,6 +464,29 @@ test('humanize duration with suffix', function (assert) {
     assert.equal(moment.duration({seconds:  44}).humanize(true),  'in a few seconds', '44 seconds = a few seconds');
     assert.equal(moment.duration({seconds: -44}).humanize(true),  'a few seconds ago', '44 seconds = a few seconds');
     assert.equal(moment.duration({seconds: +44}).humanize(true),  'in a few seconds', '44 seconds = a few seconds');
+    assert.equal(moment.duration({seconds:  44}).humanize({withSuffix: true}),  'in a few seconds', '44 seconds = a few seconds');
+    assert.equal(moment.duration({seconds: -44}).humanize({withSuffix: true}),  'a few seconds ago', '44 seconds = a few seconds');
+    assert.equal(moment.duration({seconds: +44}).humanize({withSuffix: true}),  'in a few seconds', '44 seconds = a few seconds');
+});
+
+test('humanize duration with options', function (assert) {
+    var thresholds = {s: 9};
+    moment.locale('en');
+    assert.equal(
+        moment.duration({seconds: -10}).humanize({thresholds: thresholds}),
+        'a minute',
+        '10 seconds = a minute (with thresholds)'
+    );
+    assert.equal(
+        moment.duration({seconds: 10}).humanize({thresholds: thresholds, withSuffix: true}),
+        'in a minute',
+        '10 seconds = a minute (with thresholds and suffix)'
+    );
+    assert.equal(
+        moment.duration({weeks: 3}).humanize({thresholds: {d: 7, w: 4}, withSuffix: true}),
+        'in 3 weeks',
+        'in 3 weeks = in 3 weeks (with thresholds and suffix)'
+    );
 });
 
 test('bubble value up', function (assert) {