index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
  2. import differenceInCalendarDays from "../differenceInCalendarDays/index.js";
  3. import format from "../format/index.js";
  4. import defaultLocale from "../_lib/defaultLocale/index.js";
  5. import subMilliseconds from "../subMilliseconds/index.js";
  6. import toDate from "../toDate/index.js";
  7. import getTimezoneOffsetInMilliseconds from "../_lib/getTimezoneOffsetInMilliseconds/index.js";
  8. import requiredArgs from "../_lib/requiredArgs/index.js";
  9. import toInteger from "../_lib/toInteger/index.js";
  10. /**
  11. * @name formatRelative
  12. * @category Common Helpers
  13. * @summary Represent the date in words relative to the given base date.
  14. *
  15. * @description
  16. * Represent the date in words relative to the given base date.
  17. *
  18. * | Distance to the base date | Result |
  19. * |---------------------------|---------------------------|
  20. * | Previous 6 days | last Sunday at 04:30 AM |
  21. * | Last day | yesterday at 04:30 AM |
  22. * | Same day | today at 04:30 AM |
  23. * | Next day | tomorrow at 04:30 AM |
  24. * | Next 6 days | Sunday at 04:30 AM |
  25. * | Other | 12/31/2017 |
  26. *
  27. * @param {Date|Number} date - the date to format
  28. * @param {Date|Number} baseDate - the date to compare with
  29. * @param {Object} [options] - an object with options.
  30. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  31. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  32. * @returns {String} the date in words
  33. * @throws {TypeError} 2 arguments required
  34. * @throws {RangeError} `date` must not be Invalid Date
  35. * @throws {RangeError} `baseDate` must not be Invalid Date
  36. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  37. * @throws {RangeError} `options.locale` must contain `localize` property
  38. * @throws {RangeError} `options.locale` must contain `formatLong` property
  39. * @throws {RangeError} `options.locale` must contain `formatRelative` property
  40. *
  41. * @example
  42. * // Represent the date of 6 days ago in words relative to the given base date. In this example, today is Wednesday
  43. * const result = formatRelative(addDays(new Date(), -6), new Date())
  44. * //=> "last Thursday at 12:45 AM"
  45. */
  46. export default function formatRelative(dirtyDate, dirtyBaseDate, options) {
  47. var _ref, _options$locale, _ref2, _ref3, _ref4, _options$weekStartsOn, _options$locale2, _options$locale2$opti, _defaultOptions$local, _defaultOptions$local2;
  48. requiredArgs(2, arguments);
  49. var date = toDate(dirtyDate);
  50. var baseDate = toDate(dirtyBaseDate);
  51. var defaultOptions = getDefaultOptions();
  52. 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;
  53. var weekStartsOn = toInteger((_ref2 = (_ref3 = (_ref4 = (_options$weekStartsOn = options === null || options === void 0 ? void 0 : options.weekStartsOn) !== null && _options$weekStartsOn !== void 0 ? _options$weekStartsOn : options === null || options === void 0 ? void 0 : (_options$locale2 = options.locale) === null || _options$locale2 === void 0 ? void 0 : (_options$locale2$opti = _options$locale2.options) === null || _options$locale2$opti === void 0 ? void 0 : _options$locale2$opti.weekStartsOn) !== null && _ref4 !== void 0 ? _ref4 : defaultOptions.weekStartsOn) !== null && _ref3 !== void 0 ? _ref3 : (_defaultOptions$local = defaultOptions.locale) === null || _defaultOptions$local === void 0 ? void 0 : (_defaultOptions$local2 = _defaultOptions$local.options) === null || _defaultOptions$local2 === void 0 ? void 0 : _defaultOptions$local2.weekStartsOn) !== null && _ref2 !== void 0 ? _ref2 : 0);
  54. if (!locale.localize) {
  55. throw new RangeError('locale must contain localize property');
  56. }
  57. if (!locale.formatLong) {
  58. throw new RangeError('locale must contain formatLong property');
  59. }
  60. if (!locale.formatRelative) {
  61. throw new RangeError('locale must contain formatRelative property');
  62. }
  63. var diff = differenceInCalendarDays(date, baseDate);
  64. if (isNaN(diff)) {
  65. throw new RangeError('Invalid time value');
  66. }
  67. var token;
  68. if (diff < -6) {
  69. token = 'other';
  70. } else if (diff < -1) {
  71. token = 'lastWeek';
  72. } else if (diff < 0) {
  73. token = 'yesterday';
  74. } else if (diff < 1) {
  75. token = 'today';
  76. } else if (diff < 2) {
  77. token = 'tomorrow';
  78. } else if (diff < 7) {
  79. token = 'nextWeek';
  80. } else {
  81. token = 'other';
  82. }
  83. var utcDate = subMilliseconds(date, getTimezoneOffsetInMilliseconds(date));
  84. var utcBaseDate = subMilliseconds(baseDate, getTimezoneOffsetInMilliseconds(baseDate));
  85. var formatStr = locale.formatRelative(token, utcDate, utcBaseDate, {
  86. locale: locale,
  87. weekStartsOn: weekStartsOn
  88. });
  89. return format(date, formatStr, {
  90. locale: locale,
  91. weekStartsOn: weekStartsOn
  92. });
  93. }