wallet-core/node_modules/ms/index.js

126 lines
2.3 KiB
JavaScript
Raw Normal View History

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-27 17:36:13 +02:00
* @param {Object} options
2016-10-10 03:43:44 +02:00
* @return {String|Number}
* @api public
*/
2017-05-27 17:36:13 +02:00
module.exports = function(val, options){
2017-05-24 15:10:37 +02:00
options = options || {};
2017-05-27 17:36:13 +02:00
if ('string' == typeof val) return parse(val);
return options.long
? long(val)
: short(val);
2017-05-24 15:10:37 +02:00
};
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-27 17:36:13 +02:00
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;
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;
2016-10-10 03:43:44 +02:00
}
}
/**
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
2017-05-27 17:36:13 +02:00
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';
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-05-27 17:36:13 +02:00
function long(ms) {
return plural(ms, d, 'day')
|| plural(ms, h, 'hour')
|| plural(ms, m, 'minute')
|| plural(ms, s, 'second')
|| ms + ' ms';
2016-10-10 03:43:44 +02:00
}
/**
* Pluralization helper.
*/
function plural(ms, n, name) {
2017-05-27 17:36:13 +02:00
if (ms < n) return;
if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
2017-05-24 15:10:37 +02:00
return Math.ceil(ms / n) + ' ' + name + 's';
2016-10-10 03:43:44 +02:00
}