Brought in another asset for Ray's eye form: moment
[openemr.git] / public / assets / moment-2-13-0 / src / lib / duration / humanize.js
blobeb03311de67ffd43dd4e0368a71e75e4a37545ab
1 import { createDuration } from './create';
3 var round = Math.round;
4 var thresholds = {
5     s: 45,  // seconds to minute
6     m: 45,  // minutes to hour
7     h: 22,  // hours to day
8     d: 26,  // days to month
9     M: 11   // months to year
12 // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
13 function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
14     return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
17 function relativeTime (posNegDuration, withoutSuffix, locale) {
18     var duration = createDuration(posNegDuration).abs();
19     var seconds  = round(duration.as('s'));
20     var minutes  = round(duration.as('m'));
21     var hours    = round(duration.as('h'));
22     var days     = round(duration.as('d'));
23     var months   = round(duration.as('M'));
24     var years    = round(duration.as('y'));
26     var a = seconds < thresholds.s && ['s', seconds]  ||
27             minutes <= 1           && ['m']           ||
28             minutes < thresholds.m && ['mm', minutes] ||
29             hours   <= 1           && ['h']           ||
30             hours   < thresholds.h && ['hh', hours]   ||
31             days    <= 1           && ['d']           ||
32             days    < thresholds.d && ['dd', days]    ||
33             months  <= 1           && ['M']           ||
34             months  < thresholds.M && ['MM', months]  ||
35             years   <= 1           && ['y']           || ['yy', years];
37     a[2] = withoutSuffix;
38     a[3] = +posNegDuration > 0;
39     a[4] = locale;
40     return substituteTimeAgo.apply(null, a);
43 // This function allows you to set a threshold for relative time strings
44 export function getSetRelativeTimeThreshold (threshold, limit) {
45     if (thresholds[threshold] === undefined) {
46         return false;
47     }
48     if (limit === undefined) {
49         return thresholds[threshold];
50     }
51     thresholds[threshold] = limit;
52     return true;
55 export function humanize (withSuffix) {
56     var locale = this.localeData();
57     var output = relativeTime(this, !withSuffix, locale);
59     if (withSuffix) {
60         output = locale.pastFuture(+this, output);
61     }
63     return locale.postformat(output);