aboutsummaryrefslogtreecommitdiff
path: root/node_modules/ms/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/ms/index.js')
-rw-r--r--node_modules/ms/index.js102
1 files changed, 63 insertions, 39 deletions
diff --git a/node_modules/ms/index.js b/node_modules/ms/index.js
index 4f9277169..824b37eba 100644
--- a/node_modules/ms/index.js
+++ b/node_modules/ms/index.js
@@ -2,11 +2,11 @@
* Helpers.
*/
-var s = 1000;
-var m = s * 60;
-var h = m * 60;
-var d = h * 24;
-var y = d * 365.25;
+var s = 1000
+var m = s * 60
+var h = m * 60
+var d = h * 24
+var y = d * 365.25
/**
* Parse or format the given `val`.
@@ -17,17 +17,23 @@ var y = d * 365.25;
*
* @param {String|Number} val
* @param {Object} options
+ * @throws {Error} throw an error if val is not a non-empty string or a number
* @return {String|Number}
* @api public
*/
-module.exports = function(val, options){
- options = options || {};
- if ('string' == typeof val) return parse(val);
- return options.long
- ? long(val)
- : short(val);
-};
+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))
+}
/**
* Parse the given `str` and return milliseconds.
@@ -38,47 +44,53 @@ module.exports = function(val, options){
*/
function parse(str) {
- 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();
+ str = String(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) {
case 'years':
case 'year':
case 'yrs':
case 'yr':
case 'y':
- return n * y;
+ return n * y
case 'days':
case 'day':
case 'd':
- return n * d;
+ return n * d
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
- return n * h;
+ return n * h
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
- return n * m;
+ return n * m
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
- return n * s;
+ return n * s
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
- return n;
+ return n
+ default:
+ return undefined
}
}
@@ -90,12 +102,20 @@ function parse(str) {
* @api private
*/
-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';
+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'
+ }
+ return ms + 'ms'
}
/**
@@ -106,12 +126,12 @@ function short(ms) {
* @api private
*/
-function long(ms) {
- return plural(ms, d, 'day')
- || plural(ms, h, 'hour')
- || plural(ms, m, 'minute')
- || plural(ms, s, 'second')
- || ms + ' ms';
+function fmtLong(ms) {
+ return plural(ms, d, 'day') ||
+ plural(ms, h, 'hour') ||
+ plural(ms, m, 'minute') ||
+ plural(ms, s, 'second') ||
+ ms + ' ms'
}
/**
@@ -119,7 +139,11 @@ function long(ms) {
*/
function plural(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';
+ if (ms < n) {
+ return
+ }
+ if (ms < n * 1.5) {
+ return Math.floor(ms / n) + ' ' + name
+ }
+ return Math.ceil(ms / n) + ' ' + name + 's'
}