import { createDuration } from './create';
var round = Math.round;
+var secondsThresholdChanged = false;
var thresholds = {
- s: 45, // seconds to minute
- m: 45, // minutes to hour
- h: 22, // hours to day
- d: 26, // days to month
- M: 11 // months to year
+ ss: 44, // a few seconds to seconds
+ s : 45, // seconds to minute
+ m : 45, // minutes to hour
+ h : 22, // hours to day
+ d : 26, // days to month
+ M : 11 // months to year
};
// helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
var months = round(duration.as('M'));
var years = round(duration.as('y'));
- var a = seconds < thresholds.s && ['s', seconds] ||
- minutes <= 1 && ['m'] ||
- minutes < thresholds.m && ['mm', minutes] ||
- hours <= 1 && ['h'] ||
- hours < thresholds.h && ['hh', hours] ||
- days <= 1 && ['d'] ||
- days < thresholds.d && ['dd', days] ||
- months <= 1 && ['M'] ||
- months < thresholds.M && ['MM', months] ||
- years <= 1 && ['y'] || ['yy', years];
+ var a = seconds <= thresholds.ss && ['s', seconds] ||
+ seconds < thresholds.s && ['ss', seconds] ||
+ minutes <= 1 && ['m'] ||
+ minutes < thresholds.m && ['mm', minutes] ||
+ hours <= 1 && ['h'] ||
+ hours < thresholds.h && ['hh', hours] ||
+ days <= 1 && ['d'] ||
+ days < thresholds.d && ['dd', days] ||
+ months <= 1 && ['M'] ||
+ months < thresholds.M && ['MM', months] ||
+ years <= 1 && ['y'] || ['yy', years];
a[2] = withoutSuffix;
a[3] = +posNegDuration > 0;
if (limit === undefined) {
return thresholds[threshold];
}
+ if (threshold === 's' && !secondsThresholdChanged) {
+ thresholds.ss = limit - 1;
+ } else if (threshold === 'ss') {
+ secondsThresholdChanged = true;
+ }
thresholds[threshold] = limit;
return true;
}
moment.relativeTimeThreshold('s', 45);
+ // A few seconds to seconds threshold
+ moment.relativeTimeThreshold('ss', 3);
+
+ a = moment();
+ a.subtract(3, 'seconds');
+ assert.equal(a.fromNow(), 'a few seconds ago', 'Below custom a few seconds to seconds threshold');
+ a.subtract(1, 'seconds');
+ assert.equal(a.fromNow(), '4 seconds ago', 'Above custom a few seconds to seconds threshold');
+
+ moment.relativeTimeThreshold('ss', 44);
+
// Minutes to hours threshold
moment.relativeTimeThreshold('m', 55);
a = moment();
moment.relativeTimeRounding(roundingDefault);
});
-test('retrive rounding settings', function (assert) {
+test('retrieve 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) {
+test('retrieve threshold settings', function (assert) {
moment.relativeTimeThreshold('m', 45);
var minuteThreshold = moment.relativeTimeThreshold('m');