Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

108 строки
3.9 KiB

  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 options = dirtyOptions || {};
  54. var locale = options.locale || _index3.default;
  55. if (!locale.localize) {
  56. throw new RangeError('locale must contain localize property');
  57. }
  58. if (!locale.formatLong) {
  59. throw new RangeError('locale must contain formatLong property');
  60. }
  61. if (!locale.formatRelative) {
  62. throw new RangeError('locale must contain formatRelative property');
  63. }
  64. var diff = (0, _index.default)(date, baseDate);
  65. if (isNaN(diff)) {
  66. throw new RangeError('Invalid time value');
  67. }
  68. var token;
  69. if (diff < -6) {
  70. token = 'other';
  71. } else if (diff < -1) {
  72. token = 'lastWeek';
  73. } else if (diff < 0) {
  74. token = 'yesterday';
  75. } else if (diff < 1) {
  76. token = 'today';
  77. } else if (diff < 2) {
  78. token = 'tomorrow';
  79. } else if (diff < 7) {
  80. token = 'nextWeek';
  81. } else {
  82. token = 'other';
  83. }
  84. var utcDate = (0, _index4.default)(date, (0, _index6.default)(date));
  85. var utcBaseDate = (0, _index4.default)(baseDate, (0, _index6.default)(baseDate));
  86. var formatStr = locale.formatRelative(token, utcDate, utcBaseDate, options);
  87. return (0, _index2.default)(date, formatStr, options);
  88. }
  89. module.exports = exports.default;