formatRelative.cjs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. exports.formatRelative = formatRelative;
  3. var _index = require("./_lib/defaultLocale.cjs");
  4. var _index2 = require("./_lib/defaultOptions.cjs");
  5. var _index3 = require("./_lib/normalizeDates.cjs");
  6. var _index4 = require("./differenceInCalendarDays.cjs");
  7. var _index5 = require("./format.cjs");
  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. * @param date - The date to format
  29. * @param baseDate - The date to compare with
  30. * @param options - An object with options
  31. *
  32. * @returns The date in words
  33. *
  34. * @throws `date` must not be Invalid Date
  35. * @throws `baseDate` must not be Invalid Date
  36. * @throws `options.locale` must contain `localize` property
  37. * @throws `options.locale` must contain `formatLong` property
  38. * @throws `options.locale` must contain `formatRelative` property
  39. *
  40. * @example
  41. * // Represent the date of 6 days ago in words relative to the given base date. In this example, today is Wednesday
  42. * const result = formatRelative(subDays(new Date(), 6), new Date())
  43. * //=> "last Thursday at 12:45 AM"
  44. */
  45. function formatRelative(date, baseDate, options) {
  46. const [date_, baseDate_] = (0, _index3.normalizeDates)(
  47. options?.in,
  48. date,
  49. baseDate,
  50. );
  51. const defaultOptions = (0, _index2.getDefaultOptions)();
  52. const locale =
  53. options?.locale ?? defaultOptions.locale ?? _index.defaultLocale;
  54. const weekStartsOn =
  55. options?.weekStartsOn ??
  56. options?.locale?.options?.weekStartsOn ??
  57. defaultOptions.weekStartsOn ??
  58. defaultOptions.locale?.options?.weekStartsOn ??
  59. 0;
  60. const diff = (0, _index4.differenceInCalendarDays)(date_, baseDate_);
  61. if (isNaN(diff)) {
  62. throw new RangeError("Invalid time value");
  63. }
  64. let token;
  65. if (diff < -6) {
  66. token = "other";
  67. } else if (diff < -1) {
  68. token = "lastWeek";
  69. } else if (diff < 0) {
  70. token = "yesterday";
  71. } else if (diff < 1) {
  72. token = "today";
  73. } else if (diff < 2) {
  74. token = "tomorrow";
  75. } else if (diff < 7) {
  76. token = "nextWeek";
  77. } else {
  78. token = "other";
  79. }
  80. const formatStr = locale.formatRelative(token, date_, baseDate_, {
  81. locale,
  82. weekStartsOn,
  83. });
  84. return (0, _index5.format)(date_, formatStr, { locale, weekStartsOn });
  85. }