12 * Parse or format the given `val`.
16 * - `long` verbose formatting [false]
18 * @param {String|Number} val
19 * @param {Object} options
20 * @throws {Error} throw an error if val is not a non-empty string or a number
21 * @return {String|Number}
25 module.exports = function (val, options) {
26 options = options || {}
28 if (type === 'string' && val.length > 0) {
30 } else if (type === 'number' && isNaN(val) === false) {
35 throw new Error('val is not a non-empty string or a valid number. val=' + JSON.stringify(val))
39 * Parse the given `str` and return milliseconds.
48 if (str.length > 10000) {
51 var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str)
55 var n = parseFloat(match[1])
56 var type = (match[2] || 'ms').toLowerCase()
98 * Short format for `ms`.
105 function fmtShort(ms) {
107 return Math.round(ms / d) + 'd'
110 return Math.round(ms / h) + 'h'
113 return Math.round(ms / m) + 'm'
116 return Math.round(ms / s) + 's'
122 * Long format for `ms`.
129 function fmtLong(ms) {
130 return plural(ms, d, 'day') ||
131 plural(ms, h, 'hour') ||
132 plural(ms, m, 'minute') ||
133 plural(ms, s, 'second') ||
138 * Pluralization helper.
141 function plural(ms, n, name) {
146 return Math.floor(ms / n) + ' ' + name
148 return Math.ceil(ms / n) + ' ' + name + 's'