From: Ramsay Stirling II Date: Mon, 24 Aug 2015 00:03:05 +0000 (-0400) Subject: Allow user to get/set the rounding method used when calculating relative time X-Git-Tag: 2.14.0~41^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f3cd368890b85c583ce01202471f5a8d6fcdd3df;p=thirdparty%2Fmoment.git Allow user to get/set the rounding method used when calculating relative time --- diff --git a/src/lib/duration/duration.js b/src/lib/duration/duration.js index 24440bb2b..528b56812 100644 --- a/src/lib/duration/duration.js +++ b/src/lib/duration/duration.js @@ -3,10 +3,14 @@ import './prototype'; import { createDuration } from './create'; import { isDuration } from './constructor'; -import { getSetRelativeTimeThreshold } from './humanize'; +import { + getSetRelativeTimeRounding, + getSetRelativeTimeThreshold +} from './humanize'; export { createDuration, isDuration, + getSetRelativeTimeRounding, getSetRelativeTimeThreshold }; diff --git a/src/lib/duration/humanize.js b/src/lib/duration/humanize.js index eb03311de..6120d404b 100644 --- a/src/lib/duration/humanize.js +++ b/src/lib/duration/humanize.js @@ -40,6 +40,18 @@ function relativeTime (posNegDuration, withoutSuffix, locale) { return substituteTimeAgo.apply(null, a); } +// This function allows you to set the rounding function for relative time strings +export function getSetRelativeTimeRounding (roundingFunction) { + if (roundingFunction === undefined) { + return round; + } + if (typeof(roundingFunction) === 'function') { + round = roundingFunction; + return true; + } + return false; +} + // This function allows you to set a threshold for relative time strings export function getSetRelativeTimeThreshold (threshold, limit) { if (thresholds[threshold] === undefined) { diff --git a/src/moment.js b/src/moment.js index ebe17525a..6d330956b 100644 --- a/src/moment.js +++ b/src/moment.js @@ -37,6 +37,7 @@ import { import { isDuration, createDuration as duration, + getSetRelativeTimeRounding as relativeTimeRounding, getSetRelativeTimeThreshold as relativeTimeThreshold } from './lib/duration/duration'; @@ -69,6 +70,7 @@ moment.updateLocale = updateLocale; moment.locales = locales; moment.weekdaysShort = weekdaysShort; moment.normalizeUnits = normalizeUnits; +moment.relativeTimeRounding = relativeTimeRounding; moment.relativeTimeThreshold = relativeTimeThreshold; moment.prototype = fn; diff --git a/src/test/moment/relative_time.js b/src/test/moment/relative_time.js index b2b52351e..3f92e6f7e 100644 --- a/src/test/moment/relative_time.js +++ b/src/test/moment/relative_time.js @@ -139,6 +139,64 @@ test('custom thresholds', function (assert) { moment.relativeTimeThreshold('M', 11); }); +test('custom rounding', function (assert) { + var roundingDefault = moment.relativeTimeRounding(); + + // Round relative time evaluation down + moment.relativeTimeRounding(Math.floor); + + moment.relativeTimeThreshold('s', 60); + moment.relativeTimeThreshold('m', 60); + moment.relativeTimeThreshold('h', 24); + moment.relativeTimeThreshold('d', 31); + moment.relativeTimeThreshold('M', 12); + + var a = moment(); + a.subtract({minutes: 59, seconds: 59}); + assert.equal(a.toNow(), 'in 59 minutes', 'Round down towards the nearest minute'); + + a = moment(); + a.subtract({hours: 23, minutes: 59, seconds: 59}); + assert.equal(a.toNow(), 'in 23 hours', 'Round down towards the nearest hour'); + + a = moment(); + a.subtract({days: 30, hours: 23, minutes: 59}); + assert.equal(a.toNow(), 'in 30 days', 'Round down towards the nearest day'); + + a = moment(); + a.subtract({days: 364}); + assert.equal(a.toNow(), 'in 11 months', 'Round down towards the nearest month'); + + a = moment(); + a.subtract({years: 1, days: 364}); + assert.equal(a.toNow(), 'in a year', 'Round down towards the nearest year'); + + // Do not round relative time evaluation + var retainValue = function (value) { + return value; + }; + moment.relativeTimeRounding(retainValue); + + a = moment(); + a.subtract({hours: 39}); + assert.equal(a.toNow(), 'in 1.625 days', 'Round down towards the nearest year'); + + // Restore defaults + moment.relativeTimeThreshold('s', 45); + moment.relativeTimeThreshold('m', 45); + moment.relativeTimeThreshold('h', 22); + moment.relativeTimeThreshold('d', 26); + moment.relativeTimeThreshold('M', 11); + moment.relativeTimeRounding(roundingDefault); +}); + +test('retrive 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) { moment.relativeTimeThreshold('m', 45); var minuteThreshold = moment.relativeTimeThreshold('m');