Skip to content

Allow to configure a baseline unit #11

Description

@rxaviers

Currently, it's possible to use this library and ignore large units by using the threshold configuration [1]. It should also be possible to ignore small units potentially by using a baseline (any better word?) configuration [2].

1:

const relativeTime = new RelativeTime();
relativeTime.format(aTwoYearsAgoDate));
// > '2 years ago'

RelativeTime.threshold.month = Infinity;
relativeTime.format(aTwoYearsAgoDate);
// > '24 months ago'

2:

relativeTime.format(new Date()));
// > "now"

RelativeTime.baseline.minute = 0
RelativeTime.threshold.second = -1
relativeTime.format(new Date()));
// > "this minute"

That could be accomplish with the following change:

diff --git a/src/relative-time.js b/src/relative-time.js
index b49e5eb..1fe5a1c 100644
--- a/src/relative-time.js
+++ b/src/relative-time.js
@@ -117,14 +117,15 @@ export default class RelativeTime {
 
 RelativeTime.bestFit = function(absDiff) {
   let threshold = this.threshold;
+  let baseline = this.baseline;
   switch(true) {
-    case absDiff.years > 0 && absDiff.months > threshold.month: return "year";
-    case absDiff.months > 0 && absDiff.days > threshold.day: return "month";
-    // case absDiff.months > 0 && absDiff.weeks > threshold.week: return "month";
-    // case absDiff.weeks > 0 && absDiff.days > threshold.day: return "week";
-    case absDiff.days > 0 && absDiff.hours > threshold.hour: return "day";
-    case absDiff.hours > 0 && absDiff.minutes > threshold.minute: return "hour";
-    case absDiff.minutes > 0 && absDiff.seconds > threshold.second: return "minute";
+    case absDiff.years >= baseline.year && absDiff.months > threshold.month: return "year";
+    case absDiff.months >= baseline.month && absDiff.days > threshold.day: return "month";
+    // case absDiff.months >= 1 && absDiff.weeks > threshold.week: return "month";
+    // case absDiff.weeks >= 1 && absDiff.days > threshold.day: return "week";
+    case absDiff.days >= baseline.day && absDiff.hours > threshold.hour: return "day";
+    case absDiff.hours >= baseline.hour && absDiff.minutes > threshold.minute: return "hour";
+    case absDiff.minutes >= baseline.minute && absDiff.seconds > threshold.second: return "minute";
     default: return "second";
   }
 };
@@ -138,6 +139,15 @@ RelativeTime.threshold = {
   second: 59 // at least 59 seconds before using minute.
 };
 
+RelativeTime.baseline = {
+  year: 1,
+  month: 1, // at least 2 months before using year. TODO
+  day: 1, // at least 6 days before using month. TODO
+  hour: 1, // at least 6 hours before using day. TODO
+  minute: 1 // at least 59 minutes before using hour. TODO
+};
+
 // TODO: Remove redundancy. The only reason this code is that ugly is to get
 // supported by globalize-compiler (which reads the static formatters).
 RelativeTime.initializeFormatters = function(globalize) {

Side-note: It's a good approach to enable "week", because it's possible to keep the existing behavior by configuring threshold and baseline.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions