arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/server/node_modules/socket.io-client/node_modules/ms/index.js
server/node_modules/socket.io-client/node_modules/ms/index.jsRaw
  1/**
  2 * Helpers.
  3 */
  4
  5var s = 1000
  6var m = s * 60
  7var h = m * 60
  8var d = h * 24
  9var y = d * 365.25
 10
 11/**
 12 * Parse or format the given `val`.
 13 *
 14 * Options:
 15 *
 16 *  - `long` verbose formatting [false]
 17 *
 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}
 22 * @api public
 23 */
 24
 25module.exports = function (val, options) {
 26  options = options || {}
 27  var type = typeof val
 28  if (type === 'string' && val.length > 0) {
 29    return parse(val)
 30  } else if (type === 'number' && isNaN(val) === false) {
 31    return options.long ?
 32			fmtLong(val) :
 33			fmtShort(val)
 34  }
 35  throw new Error('val is not a non-empty string or a valid number. val=' + JSON.stringify(val))
 36}
 37
 38/**
 39 * Parse the given `str` and return milliseconds.
 40 *
 41 * @param {String} str
 42 * @return {Number}
 43 * @api private
 44 */
 45
 46function parse(str) {
 47  str = String(str)
 48  if (str.length > 10000) {
 49    return
 50  }
 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)
 52  if (!match) {
 53    return
 54  }
 55  var n = parseFloat(match[1])
 56  var type = (match[2] || 'ms').toLowerCase()
 57  switch (type) {
 58    case 'years':
 59    case 'year':
 60    case 'yrs':
 61    case 'yr':
 62    case 'y':
 63      return n * y
 64    case 'days':
 65    case 'day':
 66    case 'd':
 67      return n * d
 68    case 'hours':
 69    case 'hour':
 70    case 'hrs':
 71    case 'hr':
 72    case 'h':
 73      return n * h
 74    case 'minutes':
 75    case 'minute':
 76    case 'mins':
 77    case 'min':
 78    case 'm':
 79      return n * m
 80    case 'seconds':
 81    case 'second':
 82    case 'secs':
 83    case 'sec':
 84    case 's':
 85      return n * s
 86    case 'milliseconds':
 87    case 'millisecond':
 88    case 'msecs':
 89    case 'msec':
 90    case 'ms':
 91      return n
 92    default:
 93      return undefined
 94  }
 95}
 96
 97/**
 98 * Short format for `ms`.
 99 *
100 * @param {Number} ms
101 * @return {String}
102 * @api private
103 */
104
105function fmtShort(ms) {
106  if (ms >= d) {
107    return Math.round(ms / d) + 'd'
108  }
109  if (ms >= h) {
110    return Math.round(ms / h) + 'h'
111  }
112  if (ms >= m) {
113    return Math.round(ms / m) + 'm'
114  }
115  if (ms >= s) {
116    return Math.round(ms / s) + 's'
117  }
118  return ms + 'ms'
119}
120
121/**
122 * Long format for `ms`.
123 *
124 * @param {Number} ms
125 * @return {String}
126 * @api private
127 */
128
129function fmtLong(ms) {
130  return plural(ms, d, 'day') ||
131    plural(ms, h, 'hour') ||
132    plural(ms, m, 'minute') ||
133    plural(ms, s, 'second') ||
134    ms + ' ms'
135}
136
137/**
138 * Pluralization helper.
139 */
140
141function plural(ms, n, name) {
142  if (ms < n) {
143    return
144  }
145  if (ms < n * 1.5) {
146    return Math.floor(ms / n) + ' ' + name
147  }
148  return Math.ceil(ms / n) + ' ' + name + 's'
149}