2016-10-10 03:43:44 +02:00
|
|
|
/**
|
|
|
|
* Helpers.
|
|
|
|
*/
|
|
|
|
|
2017-04-20 03:09:25 +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
|
|
|
|
* @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-04-20 03:09:25 +02:00
|
|
|
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))
|
|
|
|
}
|
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-04-20 03:09:25 +02:00
|
|
|
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()
|
2016-10-10 03:43:44 +02:00
|
|
|
switch (type) {
|
|
|
|
case 'years':
|
|
|
|
case 'year':
|
|
|
|
case 'yrs':
|
|
|
|
case 'yr':
|
|
|
|
case 'y':
|
2017-04-20 03:09:25 +02:00
|
|
|
return n * y
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'days':
|
|
|
|
case 'day':
|
|
|
|
case 'd':
|
2017-04-20 03:09:25 +02:00
|
|
|
return n * d
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'hours':
|
|
|
|
case 'hour':
|
|
|
|
case 'hrs':
|
|
|
|
case 'hr':
|
|
|
|
case 'h':
|
2017-04-20 03:09:25 +02:00
|
|
|
return n * h
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'minutes':
|
|
|
|
case 'minute':
|
|
|
|
case 'mins':
|
|
|
|
case 'min':
|
|
|
|
case 'm':
|
2017-04-20 03:09:25 +02:00
|
|
|
return n * m
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'seconds':
|
|
|
|
case 'second':
|
|
|
|
case 'secs':
|
|
|
|
case 'sec':
|
|
|
|
case 's':
|
2017-04-20 03:09:25 +02:00
|
|
|
return n * s
|
2016-10-10 03:43:44 +02:00
|
|
|
case 'milliseconds':
|
|
|
|
case 'millisecond':
|
|
|
|
case 'msecs':
|
|
|
|
case 'msec':
|
|
|
|
case 'ms':
|
2017-04-20 03:09:25 +02:00
|
|
|
return n
|
|
|
|
default:
|
|
|
|
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) {
|
|
|
|
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'
|
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') ||
|
|
|
|
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) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (ms < n * 1.5) {
|
|
|
|
return Math.floor(ms / n) + ' ' + name
|
|
|
|
}
|
|
|
|
return Math.ceil(ms / n) + ' ' + name + 's'
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|