index.js 2.8 KB

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