formatDuration.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. exports.formatDuration = formatDuration;
  3. var _index = require("./_lib/defaultLocale.js");
  4. var _index2 = require("./_lib/defaultOptions.js");
  5. /**
  6. * The {@link formatDuration} function options.
  7. */
  8. const defaultFormat = [
  9. "years",
  10. "months",
  11. "weeks",
  12. "days",
  13. "hours",
  14. "minutes",
  15. "seconds",
  16. ];
  17. /**
  18. * @name formatDuration
  19. * @category Common Helpers
  20. * @summary Formats a duration in human-readable format
  21. *
  22. * @description
  23. * Return human-readable duration string i.e. "9 months 2 days"
  24. *
  25. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  26. *
  27. * @param duration - The duration to format
  28. * @param options - An object with options.
  29. *
  30. * @returns The formatted date string
  31. *
  32. * @example
  33. * // Format full duration
  34. * formatDuration({
  35. * years: 2,
  36. * months: 9,
  37. * weeks: 1,
  38. * days: 7,
  39. * hours: 5,
  40. * minutes: 9,
  41. * seconds: 30
  42. * })
  43. * //=> '2 years 9 months 1 week 7 days 5 hours 9 minutes 30 seconds'
  44. *
  45. * @example
  46. * // Format partial duration
  47. * formatDuration({ months: 9, days: 2 })
  48. * //=> '9 months 2 days'
  49. *
  50. * @example
  51. * // Customize the format
  52. * formatDuration(
  53. * {
  54. * years: 2,
  55. * months: 9,
  56. * weeks: 1,
  57. * days: 7,
  58. * hours: 5,
  59. * minutes: 9,
  60. * seconds: 30
  61. * },
  62. * { format: ['months', 'weeks'] }
  63. * ) === '9 months 1 week'
  64. *
  65. * @example
  66. * // Customize the zeros presence
  67. * formatDuration({ years: 0, months: 9 })
  68. * //=> '9 months'
  69. * formatDuration({ years: 0, months: 9 }, { zero: true })
  70. * //=> '0 years 9 months'
  71. *
  72. * @example
  73. * // Customize the delimiter
  74. * formatDuration({ years: 2, months: 9, weeks: 3 }, { delimiter: ', ' })
  75. * //=> '2 years, 9 months, 3 weeks'
  76. */
  77. function formatDuration(duration, options) {
  78. const defaultOptions = (0, _index2.getDefaultOptions)();
  79. const locale =
  80. options?.locale ?? defaultOptions.locale ?? _index.defaultLocale;
  81. const format = options?.format ?? defaultFormat;
  82. const zero = options?.zero ?? false;
  83. const delimiter = options?.delimiter ?? " ";
  84. if (!locale.formatDistance) {
  85. return "";
  86. }
  87. const result = format
  88. .reduce((acc, unit) => {
  89. const token = `x${unit.replace(/(^.)/, (m) => m.toUpperCase())}`;
  90. const value = duration[unit];
  91. if (value !== undefined && (zero || duration[unit])) {
  92. return acc.concat(locale.formatDistance(token, value));
  93. }
  94. return acc;
  95. }, [])
  96. .join(delimiter);
  97. return result;
  98. }