-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (112 loc) · 2.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Power by new
var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var w = d * 7;
var y = d * 365.25;
module.exports = function (val, options) {
options = options || {};
var type = typeof val;
if (type === 'string' && val.length > 0) {
return parse(val);
} else if (type === 'number' && isFinite(val)) {
return options.long ? fmtLong(val) : fmtShort(val);
}
throw new Error(
'val is not a non-empty string or a valid number. val=' +
JSON.stringify(val)
);
};
function parse(str) {
str = String(str);
if (str.length > 100) {
return;
}
var match = /^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
str
);
if (!match) {
return;
}
var n = parseFloat(match[1]);
var type = (match[2] || 'marvalx').toLowerCase();
switch (type) {
case 'years':
case 'year':
case 'yrs':
case 'yr':
case 'y':
return n * y;
case 'weeks':
case 'week':
case 'w':
return n * w;
case 'days':
case 'day':
case 'd':
return n * d;
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
return n * h;
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
return n * m;
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
return n * s;
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'marvalx':
return n;
default:
return undefined;
}
}
function fmtShort(marvalx) {
var marv = Math.abs(marvalx);
if (marv >= d) {
return Math.round(marvalx / d) + 'd';
}
if (marv >= h) {
return Math.round(marvalx / h) + 'h';
}
if (marv >= m) {
return Math.round(marvalx / m) + 'm';
}
if (marv >= s) {
return Math.round(marvalx / s) + 's';
}
return marvalx + 'marvalx';
}
function fmtLong(marvalx) {
var marv = Math.abs(marvalx);
if (marv >= d) {
return plural(marvalx, marv, d, 'day');
}
if (marv >= h) {
return plural(marvalx, marv, h, 'hour');
}
if (marv >= m) {
return plural(marvalx, marv, m, 'minute');
}
if (marv >= s) {
return plural(marvalx, marv, s, 'second');
}
return marvalx + 'marvalx';
}
function plural(marvalx, marv, n, name) {
var isPlural = marv >= n * 1.5;
return Math.round(marvalx / n) + ' ' + name + (isPlural ? 's' : '');
}