2016-10-10 03:43:44 +02:00
|
|
|
/**
|
|
|
|
* Helpers.
|
|
|
|
*/
|
|
|
|
|
2017-05-24 15:10:37 +02:00
|
|
|
var s = 1000;
|
|
|
|
var m = s * 60;
|
|
|
|
var h = m * 60;
|
|
|
|
var d = h * 24;
|
|
|
|
var y = d * 365.25;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse or format the given `val`.
|
|
|
|
*
|
|
|
|
* Options:
|
|
|
|
*
|
|
|
|
* - `long` verbose formatting [false]
|
|
|
|
*
|
|
|
|
* @param {String|Number} val
|
2017-05-24 15:10:37 +02:00
|
|
|
* @param {Object} [options]
|
2017-04-20 03:09:25 +02:00
|
|
|
* @throws {Error} throw an error if val is not a non-empty string or a number
|
2016-10-10 03:43:44 +02:00
|
|
|
* @return {String|Number}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2017-05-24 15:10:37 +02:00
|
|
|
module.exports = function(val, options) {
|
|
|
|
options = options || {};
|
|
|
|
var type = typeof val;
|
2017-04-20 03:09:25 +02:00
|
|
|
if (type === 'string' && val.length > 0) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return parse(val);
|
2017-04-20 03:09:25 +02:00
|
|
|
} else if (type === 'number' && isNaN(val) === false) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return options.long ? fmtLong(val) : fmtShort(val);
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
2017-05-24 15:10:37 +02:00
|
|
|
throw new Error(
|
|
|
|
'val is not a non-empty string or a valid number. val=' +
|
|
|
|
JSON.stringify(val)
|
|
|
|
);
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the given `str` and return milliseconds.
|
|
|
|
*
|
|
|
|
* @param {String} str
|
|
|
|
* @return {Number}
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
|
|
|
function parse(str) {
|
2017-05-24 15:10:37 +02:00
|
|
|
str = String(str);
|
|
|
|
if (str.length > 100) {
|
|
|
|
return;
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
2017-05-24 15:10:37 +02:00
|
|
|
var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(
|
|
|
|
str
|
|
|
|
);
|
2017-04-20 03:09:25 +02:00
|
|
|
if (!match) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return;
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
2017-05-24 15:10:37 +02:00
|
|
|
var n = parseFloat(match[1]);
|
|
|
|
var type = (match[2] || 'ms').toLowerCase();
|
2016-10-10 03:43:44 +02:00
|
|
|
switch (type) {
|
|
|
|
case 'years':
|
|
|
|
case 'year':
|
|
|
|
case 'yrs':
|
|
|
|
case 'yr':
|
|
|
|
case 'y':
|
2017-05-24 15:10:37 +02:00
|
|
|
return n * y;
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'days':
|
|
|
|
case 'day':
|
|
|
|
case 'd':
|
2017-05-24 15:10:37 +02:00
|
|
|
return n * d;
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'hours':
|
|
|
|
case 'hour':
|
|
|
|
case 'hrs':
|
|
|
|
case 'hr':
|
|
|
|
case 'h':
|
2017-05-24 15:10:37 +02:00
|
|
|
return n * h;
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'minutes':
|
|
|
|
case 'minute':
|
|
|
|
case 'mins':
|
|
|
|
case 'min':
|
|
|
|
case 'm':
|
2017-05-24 15:10:37 +02:00
|
|
|
return n * m;
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'seconds':
|
|
|
|
case 'second':
|
|
|
|
case 'secs':
|
|
|
|
case 'sec':
|
|
|
|
case 's':
|
2017-05-24 15:10:37 +02:00
|
|
|
return n * s;
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'milliseconds':
|
|
|
|
case 'millisecond':
|
|
|
|
case 'msecs':
|
|
|
|
case 'msec':
|
|
|
|
case 'ms':
|
2017-05-24 15:10:37 +02:00
|
|
|
return n;
|
2017-04-20 03:09:25 +02:00
|
|
|
default:
|
2017-05-24 15:10:37 +02:00
|
|
|
return undefined;
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Short format for `ms`.
|
|
|
|
*
|
|
|
|
* @param {Number} ms
|
|
|
|
* @return {String}
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
function fmtShort(ms) {
|
|
|
|
if (ms >= d) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return Math.round(ms / d) + 'd';
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
|
|
|
if (ms >= h) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return Math.round(ms / h) + 'h';
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
|
|
|
if (ms >= m) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return Math.round(ms / m) + 'm';
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
|
|
|
if (ms >= s) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return Math.round(ms / s) + 's';
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
2017-05-24 15:10:37 +02:00
|
|
|
return ms + 'ms';
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Long format for `ms`.
|
|
|
|
*
|
|
|
|
* @param {Number} ms
|
|
|
|
* @return {String}
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
function fmtLong(ms) {
|
|
|
|
return plural(ms, d, 'day') ||
|
|
|
|
plural(ms, h, 'hour') ||
|
|
|
|
plural(ms, m, 'minute') ||
|
|
|
|
plural(ms, s, 'second') ||
|
2017-05-24 15:10:37 +02:00
|
|
|
ms + ' ms';
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pluralization helper.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function plural(ms, n, name) {
|
2017-04-20 03:09:25 +02:00
|
|
|
if (ms < n) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return;
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
|
|
|
if (ms < n * 1.5) {
|
2017-05-24 15:10:37 +02:00
|
|
|
return Math.floor(ms / n) + ' ' + name;
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
2017-05-24 15:10:37 +02:00
|
|
|
return Math.ceil(ms / n) + ' ' + name + 's';
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|