index.js 5.4 KB

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