index.js 3.4 KB

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