From: Tim Date: Thu, 13 Dec 2018 07:11:29 +0000 (-0800) Subject: [feature] Add week as possible relative time unit X-Git-Tag: 2.25.0~47^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a255377b7951d649ab600008fe228028dfa6d4d;p=thirdparty%2Fmoment.git [feature] Add week as possible relative time unit --- diff --git a/src/lib/duration/duration.js b/src/lib/duration/duration.js index 528b56812..27acf6985 100644 --- a/src/lib/duration/duration.js +++ b/src/lib/duration/duration.js @@ -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 }; diff --git a/src/lib/duration/humanize.js b/src/lib/duration/humanize.js index 454b01ae5..aec40437c 100644 --- a/src/lib/duration/humanize.js +++ b/src/lib/duration/humanize.js @@ -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(); } diff --git a/src/lib/locale/relative.js b/src/lib/locale/relative.js index 431466b2d..e9560a405 100644 --- a/src/lib/locale/relative.js +++ b/src/lib/locale/relative.js @@ -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', diff --git a/src/moment.js b/src/moment.js index 14e64e93d..b9dd37c22 100644 --- a/src/moment.js +++ b/src/moment.js @@ -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 = { diff --git a/src/test/moment/relative_time.js b/src/test/moment/relative_time.js index f1ea491cb..7d69284fb 100644 --- a/src/test/moment/relative_time.js +++ b/src/test/moment/relative_time.js @@ -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);