12 * Parse or format the given `val`.
16 * - `long` verbose formatting [false]
18 * @param {String|Number} val
19 * @param {Object} [options]
20 * @throws {Error} throw an error if val is not a non-empty string or a number
21 * @return {String|Number}
25 module.exports = function(val, options) {
26 options = options || {};
27 var type = typeof val;
28 if (type === 'string' && val.length > 0) {
30 } else if (type === 'number' && isNaN(val) === false) {
31 return options.long ? fmtLong(val) : fmtShort(val);
34 'val is not a non-empty string or a valid number. val=' +
40 * Parse the given `str` and return milliseconds.
49 if (str.length > 100) {
52 var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(
58 var n = parseFloat(match[1]);
59 var type = (match[2] || 'ms').toLowerCase();
101 * Short format for `ms`.
108 function fmtShort(ms) {
110 return Math.round(ms / d) + 'd';
113 return Math.round(ms / h) + 'h';
116 return Math.round(ms / m) + 'm';
119 return Math.round(ms / s) + 's';
125 * Long format for `ms`.
132 function fmtLong(ms) {
133 return plural(ms, d, 'day') ||
134 plural(ms, h, 'hour') ||
135 plural(ms, m, 'minute') ||
136 plural(ms, s, 'second') ||
141 * Pluralization helper.
144 function plural(ms, n, name) {
149 return Math.floor(ms / n) + ' ' + name;
151 return Math.ceil(ms / n) + ' ' + name + 's';