index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = formatDuration;
  6. var _index = _interopRequireDefault(require("../locale/en-US/index.js"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var defaultFormat = ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
  9. /**
  10. * @name formatDuration
  11. * @category Common Helpers
  12. * @summary Formats a duration in human-readable format
  13. *
  14. * @description
  15. * Return human-readable duration string i.e. "9 months 2 days"
  16. *
  17. * @param {Duration} duration - the duration to format
  18. * @param {Object} [options] - an object with options.
  19. * @param {string[]} [options.format=['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds']] - the array of units to format
  20. * @param {boolean} [options.zero=false] - should be zeros be included in the output?
  21. * @param {string} [options.delimiter=' '] - delimiter string
  22. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  23. * @returns {string} the formatted date string
  24. * @throws {TypeError} 1 argument required
  25. *
  26. * @example
  27. * // Format full duration
  28. * formatDuration({
  29. * years: 2,
  30. * months: 9,
  31. * weeks: 1,
  32. * days: 7,
  33. * hours: 5,
  34. * minutes: 9,
  35. * seconds: 30
  36. * })
  37. * //=> '2 years 9 months 1 week 7 days 5 hours 9 minutes 30 seconds
  38. *
  39. * @example
  40. * // Format partial duration
  41. * formatDuration({ months: 9, days: 2 })
  42. * //=> '9 months 2 days'
  43. *
  44. * @example
  45. * // Customize the format
  46. * formatDuration(
  47. * {
  48. * years: 2,
  49. * months: 9,
  50. * weeks: 1,
  51. * days: 7,
  52. * hours: 5,
  53. * minutes: 9,
  54. * seconds: 30
  55. * },
  56. * { format: ['months', 'weeks'] }
  57. * ) === '9 months 1 week'
  58. *
  59. * @example
  60. * // Customize the zeros presence
  61. * formatDuration({ years: 0, months: 9 })
  62. * //=> '9 months'
  63. * formatDuration({ years: 0, months: 9 }, { zero: true })
  64. * //=> '0 years 9 months'
  65. *
  66. * @example
  67. * // Customize the delimiter
  68. * formatDuration({ years: 2, months: 9, weeks: 3 }, { delimiter: ', ' })
  69. * //=> '2 years, 9 months, 3 weeks'
  70. */
  71. function formatDuration(duration, options) {
  72. if (arguments.length < 1) {
  73. throw new TypeError("1 argument required, but only ".concat(arguments.length, " present"));
  74. }
  75. var format = (options === null || options === void 0 ? void 0 : options.format) || defaultFormat;
  76. var locale = (options === null || options === void 0 ? void 0 : options.locale) || _index.default;
  77. var zero = (options === null || options === void 0 ? void 0 : options.zero) || false;
  78. var delimiter = (options === null || options === void 0 ? void 0 : options.delimiter) || ' ';
  79. var result = format.reduce(function (acc, unit) {
  80. var token = "x".concat(unit.replace(/(^.)/, function (m) {
  81. return m.toUpperCase();
  82. }));
  83. var addChunk = typeof duration[unit] === 'number' && (zero || duration[unit]);
  84. return addChunk ? acc.concat(locale.formatDistance(token, duration[unit])) : acc;
  85. }, []).join(delimiter);
  86. return result;
  87. }
  88. module.exports = exports.default;