]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Allow user to get/set the rounding method used when calculating relative time
authorRamsay Stirling II <nervousxcircuits@gmail.com>
Mon, 24 Aug 2015 00:03:05 +0000 (20:03 -0400)
committerIskren Chernev <iskren.chernev@gmail.com>
Tue, 14 Jun 2016 08:20:19 +0000 (01:20 -0700)
src/lib/duration/duration.js
src/lib/duration/humanize.js
src/moment.js
src/test/moment/relative_time.js

index 24440bb2bcdf612a7bf1bdf4d04b20d4b6b33e40..528b568121fbd4a98a270ae8fd54bd738ece2aa1 100644 (file)
@@ -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
 };
index eb03311de67ffd43dd4e0368a71e75e4a37545ab..6120d404bcaead521c767015b299693bd4c804a9 100644 (file)
@@ -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) {
index ebe17525a82c0333cddf541c512fbb541a165a60..6d330956bbbd05a2fdf28bb76dc0ab65690d1be4 100644 (file)
@@ -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;
 
index b2b52351e2406590daef125d8c01ac24084a1bd2..3f92e6f7edbc866a0b5755534718df6417b8a08a 100644 (file)
@@ -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');