index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = intlFormatDistance;
  7. var _index = require("../constants/index.js");
  8. var _index2 = _interopRequireDefault(require("../differenceInCalendarDays/index.js"));
  9. var _index3 = _interopRequireDefault(require("../differenceInCalendarMonths/index.js"));
  10. var _index4 = _interopRequireDefault(require("../differenceInCalendarQuarters/index.js"));
  11. var _index5 = _interopRequireDefault(require("../differenceInCalendarWeeks/index.js"));
  12. var _index6 = _interopRequireDefault(require("../differenceInCalendarYears/index.js"));
  13. var _index7 = _interopRequireDefault(require("../differenceInHours/index.js"));
  14. var _index8 = _interopRequireDefault(require("../differenceInMinutes/index.js"));
  15. var _index9 = _interopRequireDefault(require("../differenceInSeconds/index.js"));
  16. var _index10 = _interopRequireDefault(require("../toDate/index.js"));
  17. var _index11 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  18. /**
  19. * @name intlFormatDistance
  20. * @category Common Helpers
  21. * @summary Formats distance between two dates in a human-readable format
  22. * @description
  23. * The function calculates the difference between two dates and formats it as a human-readable string.
  24. *
  25. * The function will pick the most appropriate unit depending on the distance between dates. For example, if the distance is a few hours, it might return `x hours`. If the distance is a few months, it might return `x months`.
  26. *
  27. * You can also specify a unit to force using it regardless of the distance to get a result like `123456 hours`.
  28. *
  29. * See the table below for the unit picking logic:
  30. *
  31. * | Distance between dates | Result (past) | Result (future) |
  32. * | ---------------------- | -------------- | --------------- |
  33. * | 0 seconds | now | now |
  34. * | 1-59 seconds | X seconds ago | in X seconds |
  35. * | 1-59 minutes | X minutes ago | in X minutes |
  36. * | 1-23 hours | X hours ago | in X hours |
  37. * | 1 day | yesterday | tomorrow |
  38. * | 2-6 days | X days ago | in X days |
  39. * | 7 days | last week | next week |
  40. * | 8 days-1 month | X weeks ago | in X weeks |
  41. * | 1 month | last month | next month |
  42. * | 2-3 months | X months ago | in X months |
  43. * | 1 quarter | last quarter | next quarter |
  44. * | 2-3 quarters | X quarters ago | in X quarters |
  45. * | 1 year | last year | next year |
  46. * | 2+ years | X years ago | in X years |
  47. *
  48. * @param {Date|Number} date - the date
  49. * @param {Date|Number} baseDate - the date to compare with.
  50. * @param {Object} [options] - an object with options.
  51. * @param {String} [options.unit] - formats the distance with the given unit ('year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second').
  52. * @param {String|String[]} [options.locale] - the locale to use.
  53. * @param {String} [options.localeMatcher='best fit'] - the locale matching algorithm to use. Other value: 'lookup'.
  54. * See MDN for details [Locale identification and negotiation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation)
  55. * @param {String} [options.numeric='auto'] - the output message format. The values are 'auto' (e.g. `yesterday`), 'always'(e.g. `1 day ago`).
  56. * @param {String} [options.style='long'] - the length of the result. The values are: 'long' (e.g. `1 month`), 'short' (e.g. 'in 1 mo.'), 'narrow' (e.g. 'in 1 mo.').
  57. * The narrow one could be similar to the short one for some locales.
  58. * @returns {String} the distance in words according to language-sensitive relative time formatting.
  59. * @throws {TypeError} 2 arguments required
  60. * @throws {RangeError} `date` must not be Invalid Date
  61. * @throws {RangeError} `baseDate` must not be Invalid Date
  62. * @throws {RangeError} `options.unit` must not be invalid Unit
  63. * @throws {RangeError} `options.locale` must not be invalid locale
  64. * @throws {RangeError} `options.localeMatcher` must not be invalid localeMatcher
  65. * @throws {RangeError} `options.numeric` must not be invalid numeric
  66. * @throws {RangeError} `options.style` must not be invalid style
  67. *
  68. * @example
  69. * // What is the distance between the dates when the fist date is after the second?
  70. * intlFormatDistance(
  71. * new Date(1986, 3, 4, 11, 30, 0),
  72. * new Date(1986, 3, 4, 10, 30, 0)
  73. * )
  74. * //=> 'in 1 hour'
  75. *
  76. * // What is the distance between the dates when the fist date is before the second?
  77. * intlFormatDistance(
  78. * new Date(1986, 3, 4, 10, 30, 0),
  79. * new Date(1986, 3, 4, 11, 30, 0)
  80. * )
  81. * //=> '1 hour ago'
  82. *
  83. * @example
  84. * // Use the unit option to force the function to output the result in quarters. Without setting it, the example would return "next year"
  85. * intlFormatDistance(
  86. * new Date(1987, 6, 4, 10, 30, 0),
  87. * new Date(1986, 3, 4, 10, 30, 0),
  88. * { unit: 'quarter' }
  89. * )
  90. * //=> 'in 5 quarters'
  91. *
  92. * @example
  93. * // Use the locale option to get the result in Spanish. Without setting it, the example would return "in 1 hour".
  94. * intlFormatDistance(
  95. * new Date(1986, 3, 4, 11, 30, 0),
  96. * new Date(1986, 3, 4, 10, 30, 0),
  97. * { locale: 'es' }
  98. * )
  99. * //=> 'dentro de 1 hora'
  100. *
  101. * @example
  102. * // Use the numeric option to force the function to use numeric values. Without setting it, the example would return "tomorrow".
  103. * intlFormatDistance(
  104. * new Date(1986, 3, 5, 11, 30, 0),
  105. * new Date(1986, 3, 4, 11, 30, 0),
  106. * { numeric: 'always' }
  107. * )
  108. * //=> 'in 1 day'
  109. *
  110. * @example
  111. * // Use the style option to force the function to use short values. Without setting it, the example would return "in 2 years".
  112. * intlFormatDistance(
  113. * new Date(1988, 3, 4, 11, 30, 0),
  114. * new Date(1986, 3, 4, 11, 30, 0),
  115. * { style: 'short' }
  116. * )
  117. * //=> 'in 2 yr'
  118. */
  119. function intlFormatDistance(date, baseDate, options) {
  120. (0, _index11.default)(2, arguments);
  121. var value = 0;
  122. var unit;
  123. var dateLeft = (0, _index10.default)(date);
  124. var dateRight = (0, _index10.default)(baseDate);
  125. if (!(options !== null && options !== void 0 && options.unit)) {
  126. // Get the unit based on diffInSeconds calculations if no unit is specified
  127. var diffInSeconds = (0, _index9.default)(dateLeft, dateRight); // The smallest unit
  128. if (Math.abs(diffInSeconds) < _index.secondsInMinute) {
  129. value = (0, _index9.default)(dateLeft, dateRight);
  130. unit = 'second';
  131. } else if (Math.abs(diffInSeconds) < _index.secondsInHour) {
  132. value = (0, _index8.default)(dateLeft, dateRight);
  133. unit = 'minute';
  134. } else if (Math.abs(diffInSeconds) < _index.secondsInDay && Math.abs((0, _index2.default)(dateLeft, dateRight)) < 1) {
  135. value = (0, _index7.default)(dateLeft, dateRight);
  136. unit = 'hour';
  137. } else if (Math.abs(diffInSeconds) < _index.secondsInWeek && (value = (0, _index2.default)(dateLeft, dateRight)) && Math.abs(value) < 7) {
  138. unit = 'day';
  139. } else if (Math.abs(diffInSeconds) < _index.secondsInMonth) {
  140. value = (0, _index5.default)(dateLeft, dateRight);
  141. unit = 'week';
  142. } else if (Math.abs(diffInSeconds) < _index.secondsInQuarter) {
  143. value = (0, _index3.default)(dateLeft, dateRight);
  144. unit = 'month';
  145. } else if (Math.abs(diffInSeconds) < _index.secondsInYear) {
  146. if ((0, _index4.default)(dateLeft, dateRight) < 4) {
  147. // To filter out cases that are less than a year but match 4 quarters
  148. value = (0, _index4.default)(dateLeft, dateRight);
  149. unit = 'quarter';
  150. } else {
  151. value = (0, _index6.default)(dateLeft, dateRight);
  152. unit = 'year';
  153. }
  154. } else {
  155. value = (0, _index6.default)(dateLeft, dateRight);
  156. unit = 'year';
  157. }
  158. } else {
  159. // Get the value if unit is specified
  160. unit = options === null || options === void 0 ? void 0 : options.unit;
  161. if (unit === 'second') {
  162. value = (0, _index9.default)(dateLeft, dateRight);
  163. } else if (unit === 'minute') {
  164. value = (0, _index8.default)(dateLeft, dateRight);
  165. } else if (unit === 'hour') {
  166. value = (0, _index7.default)(dateLeft, dateRight);
  167. } else if (unit === 'day') {
  168. value = (0, _index2.default)(dateLeft, dateRight);
  169. } else if (unit === 'week') {
  170. value = (0, _index5.default)(dateLeft, dateRight);
  171. } else if (unit === 'month') {
  172. value = (0, _index3.default)(dateLeft, dateRight);
  173. } else if (unit === 'quarter') {
  174. value = (0, _index4.default)(dateLeft, dateRight);
  175. } else if (unit === 'year') {
  176. value = (0, _index6.default)(dateLeft, dateRight);
  177. }
  178. }
  179. var rtf = new Intl.RelativeTimeFormat(options === null || options === void 0 ? void 0 : options.locale, {
  180. localeMatcher: options === null || options === void 0 ? void 0 : options.localeMatcher,
  181. numeric: (options === null || options === void 0 ? void 0 : options.numeric) || 'auto',
  182. style: options === null || options === void 0 ? void 0 : options.style
  183. });
  184. return rtf.format(value, unit);
  185. }
  186. module.exports = exports.default;