{"ast":null,"code":"/**\n * Helpers.\n */\nvar s = 1000;\nvar m = s * 60;\nvar h = m * 60;\nvar d = h * 24;\nvar w = d * 7;\nvar y = d * 365.25;\n/**\n * Parse or format the given `val`.\n *\n * Options:\n *\n * - `long` verbose formatting [false]\n *\n * @param {String|Number} val\n * @param {Object} [options]\n * @throws {Error} throw an error if val is not a non-empty string or a number\n * @return {String|Number}\n * @api public\n */\n\nmodule.exports = function (val, options) {\n options = options || {};\n var type = typeof val;\n\n if (type === 'string' && val.length > 0) {\n return parse(val);\n } else if (type === 'number' && isFinite(val)) {\n return options.long ? fmtLong(val) : fmtShort(val);\n }\n\n throw new Error('val is not a non-empty string or a valid number. val=' + JSON.stringify(val));\n};\n/**\n * Parse the given `str` and return milliseconds.\n *\n * @param {String} str\n * @return {Number}\n * @api private\n */\n\n\nfunction parse(str) {\n str = String(str);\n\n if (str.length > 100) {\n return;\n }\n\n 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(str);\n\n if (!match) {\n return;\n }\n\n var n = parseFloat(match[1]);\n var type = (match[2] || 'ms').toLowerCase();\n\n switch (type) {\n case 'years':\n case 'year':\n case 'yrs':\n case 'yr':\n case 'y':\n return n * y;\n\n case 'weeks':\n case 'week':\n case 'w':\n return n * w;\n\n case 'days':\n case 'day':\n case 'd':\n return n * d;\n\n case 'hours':\n case 'hour':\n case 'hrs':\n case 'hr':\n case 'h':\n return n * h;\n\n case 'minutes':\n case 'minute':\n case 'mins':\n case 'min':\n case 'm':\n return n * m;\n\n case 'seconds':\n case 'second':\n case 'secs':\n case 'sec':\n case 's':\n return n * s;\n\n case 'milliseconds':\n case 'millisecond':\n case 'msecs':\n case 'msec':\n case 'ms':\n return n;\n\n default:\n return undefined;\n }\n}\n/**\n * Short format for `ms`.\n *\n * @param {Number} ms\n * @return {String}\n * @api private\n */\n\n\nfunction fmtShort(ms) {\n var msAbs = Math.abs(ms);\n\n if (msAbs >= d) {\n return Math.round(ms / d) + 'd';\n }\n\n if (msAbs >= h) {\n return Math.round(ms / h) + 'h';\n }\n\n if (msAbs >= m) {\n return Math.round(ms / m) + 'm';\n }\n\n if (msAbs >= s) {\n return Math.round(ms / s) + 's';\n }\n\n return ms + 'ms';\n}\n/**\n * Long format for `ms`.\n *\n * @param {Number} ms\n * @return {String}\n * @api private\n */\n\n\nfunction fmtLong(ms) {\n var msAbs = Math.abs(ms);\n\n if (msAbs >= d) {\n return plural(ms, msAbs, d, 'day');\n }\n\n if (msAbs >= h) {\n return plural(ms, msAbs, h, 'hour');\n }\n\n if (msAbs >= m) {\n return plural(ms, msAbs, m, 'minute');\n }\n\n if (msAbs >= s) {\n return plural(ms, msAbs, s, 'second');\n }\n\n return ms + ' ms';\n}\n/**\n * Pluralization helper.\n */\n\n\nfunction plural(ms, msAbs, n, name) {\n var isPlural = msAbs >= n * 1.5;\n return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');\n}","map":null,"metadata":{},"sourceType":"script"}