index.js 3.7 KB

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