index.js 2.7 KB

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