VUE_GabenParadise/node_modules/ms/index.js

163 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-08-10 12:35:19 +00:00
/**
* Helpers.
*/
var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
2020-08-13 08:31:15 +00:00
var w = d * 7;
2020-08-10 12:35:19 +00:00
var y = d * 365.25;
/**
* Parse or format the given `val`.
*
* Options:
*
* - `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
* @return {String|Number}
* @api public
*/
module.exports = function(val, options) {
options = options || {};
var type = typeof val;
if (type === 'string' && val.length > 0) {
return parse(val);
2020-08-13 08:31:15 +00:00
} else if (type === 'number' && isFinite(val)) {
2020-08-10 12:35:19 +00:00
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.
*
* @param {String} str
* @return {Number}
* @api private
*/
function parse(str) {
str = String(str);
if (str.length > 100) {
return;
}
2020-08-13 08:31:15 +00:00
var match = /^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
2020-08-10 12:35:19 +00:00
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;
2020-08-13 08:31:15 +00:00
case 'weeks':
case 'week':
case 'w':
return n * w;
2020-08-10 12:35:19 +00:00
case 'days':
case 'day':
case 'd':
return n * d;
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
return n * h;
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
return n * m;
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
return n * s;
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
return n;
default:
return undefined;
}
}
/**
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function fmtShort(ms) {
2020-08-13 08:31:15 +00:00
var msAbs = Math.abs(ms);
if (msAbs >= d) {
2020-08-10 12:35:19 +00:00
return Math.round(ms / d) + 'd';
}
2020-08-13 08:31:15 +00:00
if (msAbs >= h) {
2020-08-10 12:35:19 +00:00
return Math.round(ms / h) + 'h';
}
2020-08-13 08:31:15 +00:00
if (msAbs >= m) {
2020-08-10 12:35:19 +00:00
return Math.round(ms / m) + 'm';
}
2020-08-13 08:31:15 +00:00
if (msAbs >= s) {
2020-08-10 12:35:19 +00:00
return Math.round(ms / s) + 's';
}
return ms + 'ms';
}
/**
* Long format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function fmtLong(ms) {
2020-08-13 08:31:15 +00:00
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return plural(ms, msAbs, d, 'day');
}
if (msAbs >= h) {
return plural(ms, msAbs, h, 'hour');
}
if (msAbs >= m) {
return plural(ms, msAbs, m, 'minute');
}
if (msAbs >= s) {
return plural(ms, msAbs, s, 'second');
}
return ms + ' ms';
2020-08-10 12:35:19 +00:00
}
/**
* Pluralization helper.
*/
2020-08-13 08:31:15 +00:00
function plural(ms, msAbs, n, name) {
var isPlural = msAbs >= n * 1.5;
return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
2020-08-10 12:35:19 +00:00
}