]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[feature] Add week as possible relative time unit
authorTim <timmyzsearcy@gmail.com>
Thu, 13 Dec 2018 07:11:29 +0000 (23:11 -0800)
committerIskren Chernev <iskren.chernev@gmail.com>
Fri, 24 Apr 2020 20:55:02 +0000 (23:55 +0300)
src/lib/duration/duration.js
src/lib/duration/humanize.js
src/lib/locale/relative.js
src/moment.js
src/test/moment/relative_time.js

index 528b568121fbd4a98a270ae8fd54bd738ece2aa1..27acf6985a5dc251ec6e2f1ab31c98f1ab9d1e8e 100644 (file)
@@ -5,12 +5,14 @@ import { createDuration } from './create';
 import { isDuration } from './constructor';
 import {
     getSetRelativeTimeRounding,
-    getSetRelativeTimeThreshold
+    getSetRelativeTimeThreshold,
+    getSetRelativeTimeIncludeWeeks
 } from './humanize';
 
 export {
     createDuration,
     isDuration,
     getSetRelativeTimeRounding,
-    getSetRelativeTimeThreshold
+    getSetRelativeTimeThreshold,
+    getSetRelativeTimeIncludeWeeks
 };
index 454b01ae516b7d1b12eb40e5bc2a7c66a2c0a02e..aec40437c3cf41faf8e65743c37942417e899254 100644 (file)
@@ -1,28 +1,32 @@
 import { createDuration } from './create';
 
 var round = Math.round;
+
 var thresholds = {
     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
+    d : 26,          // days to week
     M : 11          // months to year
 };
 
+var includeWeeks = false;
+
 // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
 function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
     return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
 }
 
 function relativeTime (posNegDuration, withoutSuffix, locale) {
-    var duration = createDuration(posNegDuration).abs();
-    var seconds  = round(duration.as('s'));
-    var minutes  = round(duration.as('m'));
-    var hours    = round(duration.as('h'));
-    var days     = round(duration.as('d'));
-    var months   = round(duration.as('M'));
-    var years    = round(duration.as('y'));
+    var duration   = createDuration(posNegDuration).abs();
+    var seconds    = round(duration.as('s'));
+    var minutes    = round(duration.as('m'));
+    var hours      = round(duration.as('h'));
+    var days       = round(duration.as('d'));
+    var weeks      = round(duration.as('w'));
+    var months     = round(duration.as('M'));
+    var years      = round(duration.as('y'));
 
     var a = seconds <= thresholds.ss && ['s', seconds]  ||
             seconds < thresholds.s   && ['ss', seconds] ||
@@ -31,10 +35,15 @@ function relativeTime (posNegDuration, withoutSuffix, locale) {
             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];
+            days    < thresholds.d   && ['dd', days];
+
+    if (includeWeeks) {
+        a = a || weeks <= 1             && ['w']         ||
+                 weeks < thresholds.w   && ['ww', weeks];
+    }
+    a = a || months  <= 1             && ['M']           ||
+             months  < thresholds.M   && ['MM', months]  ||
+             years   <= 1             && ['y']           || ['yy', years];
 
     a[2] = withoutSuffix;
     a[3] = +posNegDuration > 0;
@@ -68,8 +77,25 @@ export function getSetRelativeTimeThreshold (threshold, limit) {
     }
     return true;
 }
-
-export function humanize (withSuffix) {
+export function getSetRelativeTimeIncludeWeeks(setIncludeWeeks) {
+    if (setIncludeWeeks === undefined) {
+        return includeWeeks;
+    }
+    if (typeof setIncludeWeeks !== 'boolean') {
+        return false;
+    }
+    includeWeeks = setIncludeWeeks;
+    if (includeWeeks === true) {
+        thresholds.w = 4;
+        thresholds.d = 7;
+    }
+    if (includeWeeks === false) {
+        delete thresholds.w;
+        thresholds.d = 26;
+    }
+    return true;
+}
+export function humanize (withSuffix, includeWeeks) {
     if (!this.isValid()) {
         return this.localeData().invalidDate();
     }
index 431466b2da5a04c8668ba365605e139d423f54b6..e9560a405be1b3b87961f453ce11439f8a947a3f 100644 (file)
@@ -9,6 +9,8 @@ export var defaultRelativeTime = {
     hh : '%d hours',
     d  : 'a day',
     dd : '%d days',
+    w  : 'a week',
+    ww : '%d weeks',
     M  : 'a month',
     MM : '%d months',
     y  : 'a year',
index 14e64e93de19af47c8ed2e7499b134b1e3c5fa00..b9dd37c22f8a585c250e084ac3a528a7d1f2b4d8 100644 (file)
@@ -42,7 +42,8 @@ import {
     isDuration,
     createDuration              as duration,
     getSetRelativeTimeRounding  as relativeTimeRounding,
-    getSetRelativeTimeThreshold as relativeTimeThreshold
+    getSetRelativeTimeThreshold as relativeTimeThreshold,
+    getSetRelativeTimeIncludeWeeks as relativeTimeIncludeWeeks
 } from './lib/duration/duration';
 
 import { normalizeUnits } from './lib/units/units';
@@ -51,33 +52,34 @@ import isDate from './lib/utils/is-date';
 
 setHookCallback(local);
 
-moment.fn                    = fn;
-moment.min                   = min;
-moment.max                   = max;
-moment.now                   = now;
-moment.utc                   = utc;
-moment.unix                  = unix;
-moment.months                = months;
-moment.isDate                = isDate;
-moment.locale                = locale;
-moment.invalid               = invalid;
-moment.duration              = duration;
-moment.isMoment              = isMoment;
-moment.weekdays              = weekdays;
-moment.parseZone             = parseZone;
-moment.localeData            = localeData;
-moment.isDuration            = isDuration;
-moment.monthsShort           = monthsShort;
-moment.weekdaysMin           = weekdaysMin;
-moment.defineLocale          = defineLocale;
-moment.updateLocale          = updateLocale;
-moment.locales               = locales;
-moment.weekdaysShort         = weekdaysShort;
-moment.normalizeUnits        = normalizeUnits;
-moment.relativeTimeRounding  = relativeTimeRounding;
-moment.relativeTimeThreshold = relativeTimeThreshold;
-moment.calendarFormat        = getCalendarFormat;
-moment.prototype             = fn;
+moment.fn                       = fn;
+moment.min                      = min;
+moment.max                      = max;
+moment.now                      = now;
+moment.utc                      = utc;
+moment.unix                     = unix;
+moment.months                   = months;
+moment.isDate                   = isDate;
+moment.locale                   = locale;
+moment.invalid                  = invalid;
+moment.duration                 = duration;
+moment.isMoment                 = isMoment;
+moment.weekdays                 = weekdays;
+moment.parseZone                = parseZone;
+moment.localeData               = localeData;
+moment.isDuration               = isDuration;
+moment.monthsShort              = monthsShort;
+moment.weekdaysMin              = weekdaysMin;
+moment.defineLocale             = defineLocale;
+moment.updateLocale             = updateLocale;
+moment.locales                  = locales;
+moment.weekdaysShort            = weekdaysShort;
+moment.normalizeUnits           = normalizeUnits;
+moment.relativeTimeRounding     = relativeTimeRounding;
+moment.relativeTimeThreshold    = relativeTimeThreshold;
+moment.relativeTimeIncludeWeeks = relativeTimeIncludeWeeks;
+moment.calendarFormat           = getCalendarFormat;
+moment.prototype                = fn;
 
 // currently HTML5 input type only supports 24-hour formats
 moment.HTML5_FMT = {
index f1ea491cb0f4d44ecfce860d491b9af38a6be707..7d69284fbb4bb0a2d3fde4cb783ecf5b9566c663 100644 (file)
@@ -82,6 +82,23 @@ test('default thresholds toNow', function (assert) {
 test('custom thresholds', function (assert) {
     var a;
 
+    // including weeks
+    moment.relativeTimeIncludeWeeks(true);
+    // threshold for days to weeks with including weeks
+    a = moment();
+    a.subtract(6, 'days');
+    assert.equal(a.fromNow(), '6 days ago', 'Below threshold days for weeks');
+    a.subtract(1, 'days');
+    assert.equal(a.fromNow(), 'a week ago', 'Above threshold days for weeks');
+
+    // threshold for days to weeks with including weeks
+    a = moment();
+    a.subtract(3, 'weeks');
+    assert.equal(a.fromNow(), '3 weeks ago', 'Below threshold weeks for months');
+    a.subtract(1, 'week');
+    assert.equal(a.fromNow(), 'a month ago', 'Above threshold weeks for months');
+    moment.relativeTimeIncludeWeeks(false);
+    assert.equal(moment.relativeTimeIncludeWeeks(), false);
     // Seconds to minute threshold, under 30
     moment.relativeTimeThreshold('s', 25);