index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = formatRelative;
  6. var _index = _interopRequireDefault(require("../differenceInCalendarDays/index.js"));
  7. var _index2 = _interopRequireDefault(require("../format/index.js"));
  8. var _index3 = _interopRequireDefault(require("../locale/en-US/index.js"));
  9. var _index4 = _interopRequireDefault(require("../subMilliseconds/index.js"));
  10. var _index5 = _interopRequireDefault(require("../toDate/index.js"));
  11. var _index6 = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
  12. var _index7 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. /**
  15. * @name formatRelative
  16. * @category Common Helpers
  17. * @summary Represent the date in words relative to the given base date.
  18. *
  19. * @description
  20. * Represent the date in words relative to the given base date.
  21. *
  22. * | Distance to the base date | Result |
  23. * |---------------------------|---------------------------|
  24. * | Previous 6 days | last Sunday at 04:30 AM |
  25. * | Last day | yesterday at 04:30 AM |
  26. * | Same day | today at 04:30 AM |
  27. * | Next day | tomorrow at 04:30 AM |
  28. * | Next 6 days | Sunday at 04:30 AM |
  29. * | Other | 12/31/2017 |
  30. *
  31. * ### v2.0.0 breaking changes:
  32. *
  33. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  34. *
  35. * @param {Date|Number} date - the date to format
  36. * @param {Date|Number} baseDate - the date to compare with
  37. * @param {Object} [options] - an object with options.
  38. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  39. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  40. * @returns {String} the date in words
  41. * @throws {TypeError} 2 arguments required
  42. * @throws {RangeError} `date` must not be Invalid Date
  43. * @throws {RangeError} `baseDate` must not be Invalid Date
  44. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  45. * @throws {RangeError} `options.locale` must contain `localize` property
  46. * @throws {RangeError} `options.locale` must contain `formatLong` property
  47. * @throws {RangeError} `options.locale` must contain `formatRelative` property
  48. */
  49. function formatRelative(dirtyDate, dirtyBaseDate, dirtyOptions) {
  50. (0, _index7.default)(2, arguments);
  51. var date = (0, _index5.default)(dirtyDate);
  52. var baseDate = (0, _index5.default)(dirtyBaseDate);
  53. var _ref = dirtyOptions || {},
  54. _ref$locale = _ref.locale,
  55. locale = _ref$locale === void 0 ? _index3.default : _ref$locale,
  56. _ref$weekStartsOn = _ref.weekStartsOn,
  57. weekStartsOn = _ref$weekStartsOn === void 0 ? 0 : _ref$weekStartsOn;
  58. if (!locale.localize) {
  59. throw new RangeError('locale must contain localize property');
  60. }
  61. if (!locale.formatLong) {
  62. throw new RangeError('locale must contain formatLong property');
  63. }
  64. if (!locale.formatRelative) {
  65. throw new RangeError('locale must contain formatRelative property');
  66. }
  67. var diff = (0, _index.default)(date, baseDate);
  68. if (isNaN(diff)) {
  69. throw new RangeError('Invalid time value');
  70. }
  71. var token;
  72. if (diff < -6) {
  73. token = 'other';
  74. } else if (diff < -1) {
  75. token = 'lastWeek';
  76. } else if (diff < 0) {
  77. token = 'yesterday';
  78. } else if (diff < 1) {
  79. token = 'today';
  80. } else if (diff < 2) {
  81. token = 'tomorrow';
  82. } else if (diff < 7) {
  83. token = 'nextWeek';
  84. } else {
  85. token = 'other';
  86. }
  87. var utcDate = (0, _index4.default)(date, (0, _index6.default)(date));
  88. var utcBaseDate = (0, _index4.default)(baseDate, (0, _index6.default)(baseDate));
  89. var formatStr = locale.formatRelative(token, utcDate, utcBaseDate, {
  90. locale: locale,
  91. weekStartsOn: weekStartsOn
  92. });
  93. return (0, _index2.default)(date, formatStr, {
  94. locale: locale,
  95. weekStartsOn: weekStartsOn
  96. });
  97. }
  98. module.exports = exports.default;