Debian Codegraph @

vendor/static/moment/src/lib/duration/humanize.js

raw
1 import { createDuration } from './create';
2
3 var round = Math.round,
4 thresholds = {
5 ss: 44, // a few seconds to seconds
6 s: 45, // seconds to minute
7 m: 45, // minutes to hour
8 h: 22, // hours to day
9 d: 26, // days to month/week
10 w: null, // weeks to month
11 M: 11, // months to year
12 };
13
14 // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
15 function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
16 return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
17 }
18
19 function relativeTime(posNegDuration, withoutSuffix, thresholds, locale) {
20 var duration = createDuration(posNegDuration).abs(),
21 seconds = round(duration.as('s')),
22 minutes = round(duration.as('m')),
23 hours = round(duration.as('h')),
24 days = round(duration.as('d')),
25 months = round(duration.as('M')),
26 weeks = round(duration.as('w')),
27 years = round(duration.as('y')),
28 a =
29 (seconds <= thresholds.ss && ['s', seconds]) ||
30 (seconds < thresholds.s && ['ss', seconds]) ||
31 (minutes <= 1 && ['m']) ||
32 (minutes < thresholds.m && ['mm', minutes]) ||
33 (hours <= 1 && ['h']) ||
34 (hours < thresholds.h && ['hh', hours]) ||
35 (days <= 1 && ['d']) ||
36 (days < thresholds.d && ['dd', days]);
37
38 if (thresholds.w != null) {
39 a =
40 a ||
41 (weeks <= 1 && ['w']) ||
42 (weeks < thresholds.w && ['ww', weeks]);
43 }
44 a = a ||
45 (months <= 1 && ['M']) ||
46 (months < thresholds.M && ['MM', months]) ||
47 (years <= 1 && ['y']) || ['yy', years];
48
49 a[2] = withoutSuffix;
50 a[3] = +posNegDuration > 0;
51 a[4] = locale;
52 return substituteTimeAgo.apply(null, a);
53 }
54
55 // This function allows you to set the rounding function for relative time strings
56 export function getSetRelativeTimeRounding(roundingFunction) {
57 if (roundingFunction === undefined) {
58 return round;
59 }
60 if (typeof roundingFunction === 'function') {
61 round = roundingFunction;
62 return true;
63 }
64 return false;
65 }
66
67 // This function allows you to set a threshold for relative time strings
68 export function getSetRelativeTimeThreshold(threshold, limit) {
69 if (thresholds[threshold] === undefined) {
70 return false;
71 }
72 if (limit === undefined) {
73 return thresholds[threshold];
74 }
75 thresholds[threshold] = limit;
76 if (threshold === 's') {
77 thresholds.ss = limit - 1;
78 }
79 return true;
80 }
81
82 export function humanize(argWithSuffix, argThresholds) {
83 if (!this.isValid()) {
84 return this.localeData().invalidDate();
85 }
86
87 var withSuffix = false,
88 th = thresholds,
89 locale,
90 output;
91
92 if (typeof argWithSuffix === 'object') {
93 argThresholds = argWithSuffix;
94 argWithSuffix = false;
95 }
96 if (typeof argWithSuffix === 'boolean') {
97 withSuffix = argWithSuffix;
98 }
99 if (typeof argThresholds === 'object') {
100 th = Object.assign({}, thresholds, argThresholds);
101 if (argThresholds.s != null && argThresholds.ss == null) {
102 th.ss = argThresholds.s - 1;
103 }
104 }
105
106 locale = this.localeData();
107 output = relativeTime(this, !withSuffix, th, locale);
108
109 if (withSuffix) {
110 output = locale.pastFuture(+this, output);
111 }
112
113 return locale.postformat(output);
114 }