From: Patrick de Wit Date: Tue, 31 Jan 2017 11:04:20 +0000 (+0100) Subject: implements optional threshold for a few seconds to %d seconds: #2558 X-Git-Tag: 2.18.0~27^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9a7ada487f5aa50ab8244a6a1f0ec560e5922b1b;p=thirdparty%2Fmoment.git implements optional threshold for a few seconds to %d seconds: #2558 --- diff --git a/src/lib/duration/humanize.js b/src/lib/duration/humanize.js index 5bae088b3..e28eeea47 100644 --- a/src/lib/duration/humanize.js +++ b/src/lib/duration/humanize.js @@ -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; } diff --git a/src/lib/locale/relative.js b/src/lib/locale/relative.js index 4c485ea22..431466b2d 100644 --- a/src/lib/locale/relative.js +++ b/src/lib/locale/relative.js @@ -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', diff --git a/src/test/moment/relative_time.js b/src/test/moment/relative_time.js index 7579e909f..c2bbf6fa2 100644 --- a/src/test/moment/relative_time.js +++ b/src/test/moment/relative_time.js @@ -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');