]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Adding settings to configure relative time thresholds between time units.
authorStephen Huenneke <stephen.huenneke@gmail.com>
Fri, 16 May 2014 04:48:35 +0000 (21:48 -0700)
committerStephen Huenneke <stephen.huenneke@gmail.com>
Fri, 16 May 2014 04:48:35 +0000 (21:48 -0700)
moment.js
test/moment/relative_time.js [new file with mode: 0644]

index 117d53f5f7a04a8238f7397d44cf30f4f0c64d9c..6908eaa364729e1a1959f135565f1535b6b9b83f 100644 (file)
--- a/moment.js
+++ b/moment.js
         // format function strings
         formatFunctions = {},
 
+        // default relative time thresholds
+        relativeTimeThresholds = {
+          s: 45,   //seconds to minutes
+          m: 45,   //minutes to hours
+          h: 22,   //hours to days
+          dd: 25,  //days to month (month == 1)
+          dm: 45,  //days to months (months > 1)
+          dy: 345  //days to year
+        },
+
         // tokens to ordinalize and pad
         ordinalizeTokens = 'DDD w W M D d'.split(' '),
         paddedTokens = 'M D H h m s w W'.split(' '),
             hours = round(minutes / 60),
             days = round(hours / 24),
             years = round(days / 365),
-            args = seconds < 45 && ['s', seconds] ||
+            args = seconds < relativeTimeThresholds.s  && ['s', seconds] ||
                 minutes === 1 && ['m'] ||
-                minutes < 45 && ['mm', minutes] ||
+                minutes < relativeTimeThresholds.m && ['mm', minutes] ||
                 hours === 1 && ['h'] ||
-                hours < 22 && ['hh', hours] ||
+                hours < relativeTimeThresholds.h && ['hh', hours] ||
                 days === 1 && ['d'] ||
-                days <= 25 && ['dd', days] ||
-                days <= 45 && ['M'] ||
-                days < 345 && ['MM', round(days / 30)] ||
+                days <= relativeTimeThresholds.dd && ['dd', days] ||
+                days <= relativeTimeThresholds.dm && ['M'] ||
+                days < relativeTimeThresholds.dy && ['MM', round(days / 30)] ||
                 years === 1 && ['y'] || ['yy', years];
         args[2] = withoutSuffix;
         args[3] = milliseconds > 0;
     // It is intended to keep the offset in sync with the timezone.
     moment.updateOffset = function () {};
 
+    // This function allows you to set a threshold for relative time strings
+    moment.relativeTimeThreshold = function(threshold, limit) {
+      if (relativeTimeThresholds[threshold] === undefined) {
+        return false;
+      }
+      relativeTimeThresholds[threshold] = limit;
+      return true;
+    };
+
     // This function will load languages and then set the global language.  If
     // no arguments are passed in, it will simply return the current global
     // language key.
diff --git a/test/moment/relative_time.js b/test/moment/relative_time.js
new file mode 100644 (file)
index 0000000..200b90b
--- /dev/null
@@ -0,0 +1,47 @@
+var moment = require("../../moment");
+
+exports.relativeTime = {
+    setUp : function (done) {
+        done();
+    },
+
+    "default thresholds" : function (test) {
+        test.expect(5);
+
+        var a = moment();
+        a.subtract('seconds', 10);
+        test.equal(a.fromNow(), "a few seconds ago", "Seconds friendly relative time");
+        a.subtract('seconds', 35);
+        test.equal(a.fromNow(), "a minute ago", "Minute friendly relative time");
+        a.subtract('minutes', 1);
+        test.equal(a.fromNow(), "2 minutes ago", "Minutes friendly relative time");
+        a.subtract('minutes', 45);
+        test.equal(a.fromNow(), "an hour ago", "1 hour friendly relative time");
+        a.subtract('hours', 22);
+        test.equal(a.fromNow(), "a day ago", "1 day friendly relative time");
+        test.done();
+    },
+
+    "custom thresholds" : function (test) {
+        test.expect(3);
+
+        var a = moment();
+        moment.relativeTimeThreshold('s', 55);
+        a.subtract('seconds', 54);
+        test.equal(a.fromNow(), "a few seconds ago", "Custom <1 minute friendly relative time");
+        moment.relativeTimeThreshold('s', 45);
+
+        a = moment();
+        moment.relativeTimeThreshold('m', 55);
+        a.subtract('minutes', 54);
+        test.equal(a.fromNow(), "54 minutes ago", "Custom <1 hour friendly relative time");
+        moment.relativeTimeThreshold('m', 45);
+
+        a = moment();
+        moment.relativeTimeThreshold('h', 24);
+        a.subtract('hours', 23);
+        test.equal(a.fromNow(), "23 hours ago", "Custom <1 day friendly relative time");
+        moment.relativeTimeThreshold('h', 22);
+        test.done();
+    }
+};