From: Stephen Huenneke Date: Fri, 16 May 2014 04:48:35 +0000 (-0700) Subject: Adding settings to configure relative time thresholds between time units. X-Git-Tag: 2.7.0~12^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=50df85e0c5f96fe156793d5e8ee62262cfba9efe;p=thirdparty%2Fmoment.git Adding settings to configure relative time thresholds between time units. --- diff --git a/moment.js b/moment.js index 117d53f5f..6908eaa36 100644 --- a/moment.js +++ b/moment.js @@ -144,6 +144,16 @@ // 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(' '), @@ -1550,15 +1560,15 @@ 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; @@ -1818,6 +1828,15 @@ // 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 index 000000000..200b90ba7 --- /dev/null +++ b/test/moment/relative_time.js @@ -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(); + } +};