diff options
Diffstat (limited to 'node_modules/ms/index.js')
-rw-r--r-- | node_modules/ms/index.js | 73 |
1 files changed, 23 insertions, 50 deletions
diff --git a/node_modules/ms/index.js b/node_modules/ms/index.js index 6a522b16b..4f9277169 100644 --- a/node_modules/ms/index.js +++ b/node_modules/ms/index.js @@ -16,24 +16,17 @@ var y = d * 365.25; * - `long` verbose formatting [false] * * @param {String|Number} val - * @param {Object} [options] - * @throws {Error} throw an error if val is not a non-empty string or a number + * @param {Object} options * @return {String|Number} * @api public */ -module.exports = function(val, options) { +module.exports = function(val, options){ options = options || {}; - var type = typeof val; - if (type === 'string' && val.length > 0) { - return parse(val); - } else if (type === 'number' && isNaN(val) === false) { - 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) - ); + if ('string' == typeof val) return parse(val); + return options.long + ? long(val) + : short(val); }; /** @@ -45,16 +38,10 @@ module.exports = function(val, options) { */ 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|years?|yrs?|y)?$/i.exec( - str - ); - if (!match) { - return; - } + str = '' + str; + if (str.length > 10000) return; + var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str); + if (!match) return; var n = parseFloat(match[1]); var type = (match[2] || 'ms').toLowerCase(); switch (type) { @@ -92,8 +79,6 @@ function parse(str) { case 'msec': case 'ms': return n; - default: - return undefined; } } @@ -105,19 +90,11 @@ function parse(str) { * @api private */ -function fmtShort(ms) { - if (ms >= d) { - return Math.round(ms / d) + 'd'; - } - if (ms >= h) { - return Math.round(ms / h) + 'h'; - } - if (ms >= m) { - return Math.round(ms / m) + 'm'; - } - if (ms >= s) { - return Math.round(ms / s) + 's'; - } +function short(ms) { + if (ms >= d) return Math.round(ms / d) + 'd'; + if (ms >= h) return Math.round(ms / h) + 'h'; + if (ms >= m) return Math.round(ms / m) + 'm'; + if (ms >= s) return Math.round(ms / s) + 's'; return ms + 'ms'; } @@ -129,12 +106,12 @@ function fmtShort(ms) { * @api private */ -function fmtLong(ms) { - return plural(ms, d, 'day') || - plural(ms, h, 'hour') || - plural(ms, m, 'minute') || - plural(ms, s, 'second') || - ms + ' ms'; +function long(ms) { + return plural(ms, d, 'day') + || plural(ms, h, 'hour') + || plural(ms, m, 'minute') + || plural(ms, s, 'second') + || ms + ' ms'; } /** @@ -142,11 +119,7 @@ function fmtLong(ms) { */ function plural(ms, n, name) { - if (ms < n) { - return; - } - if (ms < n * 1.5) { - return Math.floor(ms / n) + ' ' + name; - } + if (ms < n) return; + if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name; return Math.ceil(ms / n) + ' ' + name + 's'; } |