Last change
on this file since 484 was
484,
checked in by hendrikvanantwerpen, 11 years ago
|
Commit node_modules, to make checkouts and builds more deterministic.
|
File size:
2.0 KB
|
Line | |
---|
1 | /** |
---|
2 | * Helpers. |
---|
3 | */ |
---|
4 | |
---|
5 | var s = 1000; |
---|
6 | var m = s * 60; |
---|
7 | var h = m * 60; |
---|
8 | var d = h * 24; |
---|
9 | var y = d * 365.25; |
---|
10 | |
---|
11 | /** |
---|
12 | * Parse or format the given `val`. |
---|
13 | * |
---|
14 | * Options: |
---|
15 | * |
---|
16 | * - `long` verbose formatting [false] |
---|
17 | * |
---|
18 | * @param {String|Number} val |
---|
19 | * @param {Object} options |
---|
20 | * @return {String|Number} |
---|
21 | * @api public |
---|
22 | */ |
---|
23 | |
---|
24 | module.exports = function(val, options){ |
---|
25 | options = options || {}; |
---|
26 | if ('string' == typeof val) return parse(val); |
---|
27 | return options.long |
---|
28 | ? long(val) |
---|
29 | : short(val); |
---|
30 | }; |
---|
31 | |
---|
32 | /** |
---|
33 | * Parse the given `str` and return milliseconds. |
---|
34 | * |
---|
35 | * @param {String} str |
---|
36 | * @return {Number} |
---|
37 | * @api private |
---|
38 | */ |
---|
39 | |
---|
40 | function parse(str) { |
---|
41 | var match = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)?$/i.exec(str); |
---|
42 | if (!match) return; |
---|
43 | var n = parseFloat(match[1]); |
---|
44 | var type = (match[2] || 'ms').toLowerCase(); |
---|
45 | switch (type) { |
---|
46 | case 'years': |
---|
47 | case 'year': |
---|
48 | case 'y': |
---|
49 | return n * y; |
---|
50 | case 'days': |
---|
51 | case 'day': |
---|
52 | case 'd': |
---|
53 | return n * d; |
---|
54 | case 'hours': |
---|
55 | case 'hour': |
---|
56 | case 'h': |
---|
57 | return n * h; |
---|
58 | case 'minutes': |
---|
59 | case 'minute': |
---|
60 | case 'm': |
---|
61 | return n * m; |
---|
62 | case 'seconds': |
---|
63 | case 'second': |
---|
64 | case 's': |
---|
65 | return n * s; |
---|
66 | case 'ms': |
---|
67 | return n; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Short format for `ms`. |
---|
73 | * |
---|
74 | * @param {Number} ms |
---|
75 | * @return {String} |
---|
76 | * @api private |
---|
77 | */ |
---|
78 | |
---|
79 | function short(ms) { |
---|
80 | if (ms >= d) return Math.round(ms / d) + 'd'; |
---|
81 | if (ms >= h) return Math.round(ms / h) + 'h'; |
---|
82 | if (ms >= m) return Math.round(ms / m) + 'm'; |
---|
83 | if (ms >= s) return Math.round(ms / s) + 's'; |
---|
84 | return ms + 'ms'; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Long format for `ms`. |
---|
89 | * |
---|
90 | * @param {Number} ms |
---|
91 | * @return {String} |
---|
92 | * @api private |
---|
93 | */ |
---|
94 | |
---|
95 | function long(ms) { |
---|
96 | return plural(ms, d, 'day') |
---|
97 | || plural(ms, h, 'hour') |
---|
98 | || plural(ms, m, 'minute') |
---|
99 | || plural(ms, s, 'second') |
---|
100 | || ms + ' ms'; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Pluralization helper. |
---|
105 | */ |
---|
106 | |
---|
107 | function plural(ms, n, name) { |
---|
108 | if (ms < n) return; |
---|
109 | if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name; |
---|
110 | return Math.ceil(ms / n) + ' ' + name + 's'; |
---|
111 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.