formatRelative.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. exports.formatRelative = formatRelative;
  3. var _index = require("./differenceInCalendarDays.js");
  4. var _index2 = require("./format.js");
  5. var _index3 = require("./toDate.js");
  6. var _index4 = require("./_lib/defaultLocale.js");
  7. var _index5 = require("./_lib/defaultOptions.js");
  8. /**
  9. * The {@link formatRelative} function options.
  10. */
  11. /**
  12. * @name formatRelative
  13. * @category Common Helpers
  14. * @summary Represent the date in words relative to the given base date.
  15. *
  16. * @description
  17. * Represent the date in words relative to the given base date.
  18. *
  19. * | Distance to the base date | Result |
  20. * |---------------------------|---------------------------|
  21. * | Previous 6 days | last Sunday at 04:30 AM |
  22. * | Last day | yesterday at 04:30 AM |
  23. * | Same day | today at 04:30 AM |
  24. * | Next day | tomorrow at 04:30 AM |
  25. * | Next 6 days | Sunday at 04:30 AM |
  26. * | Other | 12/31/2017 |
  27. *
  28. * @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).
  29. *
  30. * @param date - The date to format
  31. * @param baseDate - The date to compare with
  32. * @param options - An object with options
  33. *
  34. * @returns The date in words
  35. *
  36. * @throws `date` must not be Invalid Date
  37. * @throws `baseDate` must not be Invalid Date
  38. * @throws `options.locale` must contain `localize` property
  39. * @throws `options.locale` must contain `formatLong` property
  40. * @throws `options.locale` must contain `formatRelative` property
  41. *
  42. * @example
  43. * // Represent the date of 6 days ago in words relative to the given base date. In this example, today is Wednesday
  44. * const result = formatRelative(subDays(new Date(), 6), new Date())
  45. * //=> "last Thursday at 12:45 AM"
  46. */
  47. function formatRelative(date, baseDate, options) {
  48. const _date = (0, _index3.toDate)(date);
  49. const _baseDate = (0, _index3.toDate)(baseDate);
  50. const defaultOptions = (0, _index5.getDefaultOptions)();
  51. const locale =
  52. options?.locale ?? defaultOptions.locale ?? _index4.defaultLocale;
  53. const weekStartsOn =
  54. options?.weekStartsOn ??
  55. options?.locale?.options?.weekStartsOn ??
  56. defaultOptions.weekStartsOn ??
  57. defaultOptions.locale?.options?.weekStartsOn ??
  58. 0;
  59. const diff = (0, _index.differenceInCalendarDays)(_date, _baseDate);
  60. if (isNaN(diff)) {
  61. throw new RangeError("Invalid time value");
  62. }
  63. let token;
  64. if (diff < -6) {
  65. token = "other";
  66. } else if (diff < -1) {
  67. token = "lastWeek";
  68. } else if (diff < 0) {
  69. token = "yesterday";
  70. } else if (diff < 1) {
  71. token = "today";
  72. } else if (diff < 2) {
  73. token = "tomorrow";
  74. } else if (diff < 7) {
  75. token = "nextWeek";
  76. } else {
  77. token = "other";
  78. }
  79. const formatStr = locale.formatRelative(token, _date, _baseDate, {
  80. locale,
  81. weekStartsOn,
  82. });
  83. return (0, _index2.format)(_date, formatStr, { locale, weekStartsOn });
  84. }