13 * Parse or format the given `val`.
17 * - `long` verbose formatting [false]
19 * @param {String|Number} val
20 * @param {Object} [options]
21 * @throws {Error} throw an error if val is not a non-empty string or a number
22 * @return {String|Number}
26 module.exports = function(val, options) {
27 options = options || {};
28 var type = typeof val;
29 if (type === 'string' && val.length > 0) {
31 } else if (type === 'number' && isFinite(val)) {
32 return options.long ? fmtLong(val) : fmtShort(val);
35 'val is not a non-empty string or a valid number. val=' +
41 * Parse the given `str` and return milliseconds.
50 if (str.length > 100) {
53 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(
59 var n = parseFloat(match[1]);
60 var type = (match[2] || 'ms').toLowerCase();
106 * Short format for `ms`.
113 function fmtShort(ms) {
114 var msAbs = Math.abs(ms);
116 return Math.round(ms / d) + 'd';
119 return Math.round(ms / h) + 'h';
122 return Math.round(ms / m) + 'm';
125 return Math.round(ms / s) + 's';
131 * Long format for `ms`.
138 function fmtLong(ms) {
139 var msAbs = Math.abs(ms);
141 return plural(ms, msAbs, d, 'day');
144 return plural(ms, msAbs, h, 'hour');
147 return plural(ms, msAbs, m, 'minute');
150 return plural(ms, msAbs, s, 'second');
156 * Pluralization helper.
159 function plural(ms, msAbs, n, name) {
160 var isPlural = msAbs >= n * 1.5;
161 return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');