index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * @return {String|Number}
  19. * @api public
  20. */
  21. module.exports = function(val, options){
  22. options = options || {};
  23. if ('string' == typeof val) return parse(val);
  24. return options.long
  25. ? long(val)
  26. : short(val);
  27. };
  28. /**
  29. * Parse the given `str` and return milliseconds.
  30. *
  31. * @param {String} str
  32. * @return {Number}
  33. * @api private
  34. */
  35. function parse(str) {
  36. str = '' + str;
  37. if (str.length > 10000) return;
  38. var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str);
  39. if (!match) return;
  40. var n = parseFloat(match[1]);
  41. var type = (match[2] || 'ms').toLowerCase();
  42. switch (type) {
  43. case 'years':
  44. case 'year':
  45. case 'yrs':
  46. case 'yr':
  47. case 'y':
  48. return n * y;
  49. case 'days':
  50. case 'day':
  51. case 'd':
  52. return n * d;
  53. case 'hours':
  54. case 'hour':
  55. case 'hrs':
  56. case 'hr':
  57. case 'h':
  58. return n * h;
  59. case 'minutes':
  60. case 'minute':
  61. case 'mins':
  62. case 'min':
  63. case 'm':
  64. return n * m;
  65. case 'seconds':
  66. case 'second':
  67. case 'secs':
  68. case 'sec':
  69. case 's':
  70. return n * s;
  71. case 'milliseconds':
  72. case 'millisecond':
  73. case 'msecs':
  74. case 'msec':
  75. case 'ms':
  76. return n;
  77. }
  78. }
  79. /**
  80. * Short format for `ms`.
  81. *
  82. * @param {Number} ms
  83. * @return {String}
  84. * @api private
  85. */
  86. function short(ms) {
  87. if (ms >= d) return Math.round(ms / d) + 'd';
  88. if (ms >= h) return Math.round(ms / h) + 'h';
  89. if (ms >= m) return Math.round(ms / m) + 'm';
  90. if (ms >= s) return Math.round(ms / s) + 's';
  91. return ms + 'ms';
  92. }
  93. /**
  94. * Long format for `ms`.
  95. *
  96. * @param {Number} ms
  97. * @return {String}
  98. * @api private
  99. */
  100. function long(ms) {
  101. return plural(ms, d, 'day')
  102. || plural(ms, h, 'hour')
  103. || plural(ms, m, 'minute')
  104. || plural(ms, s, 'second')
  105. || ms + ' ms';
  106. }
  107. /**
  108. * Pluralization helper.
  109. */
  110. function plural(ms, n, name) {
  111. if (ms < n) return;
  112. if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
  113. return Math.ceil(ms / n) + ' ' + name + 's';
  114. }