index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = formatDistanceStrict;
  7. var _index = require("../_lib/defaultOptions/index.js");
  8. var _index2 = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
  9. var _index3 = _interopRequireDefault(require("../compareAsc/index.js"));
  10. var _index4 = _interopRequireDefault(require("../toDate/index.js"));
  11. var _index5 = _interopRequireDefault(require("../_lib/cloneObject/index.js"));
  12. var _index6 = _interopRequireDefault(require("../_lib/assign/index.js"));
  13. var _index7 = _interopRequireDefault(require("../_lib/defaultLocale/index.js"));
  14. var _index8 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  15. var MILLISECONDS_IN_MINUTE = 1000 * 60;
  16. var MINUTES_IN_DAY = 60 * 24;
  17. var MINUTES_IN_MONTH = MINUTES_IN_DAY * 30;
  18. var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365;
  19. /**
  20. * @name formatDistanceStrict
  21. * @category Common Helpers
  22. * @summary Return the distance between the given dates in words.
  23. *
  24. * @description
  25. * Return the distance between the given dates in words, using strict units.
  26. * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
  27. * 'less than' and the like.
  28. *
  29. * | Distance between dates | Result |
  30. * |------------------------|---------------------|
  31. * | 0 ... 59 secs | [0..59] seconds |
  32. * | 1 ... 59 mins | [1..59] minutes |
  33. * | 1 ... 23 hrs | [1..23] hours |
  34. * | 1 ... 29 days | [1..29] days |
  35. * | 1 ... 11 months | [1..11] months |
  36. * | 1 ... N years | [1..N] years |
  37. *
  38. * @param {Date|Number} date - the date
  39. * @param {Date|Number} baseDate - the date to compare with
  40. * @param {Object} [options] - an object with options.
  41. * @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
  42. * @param {'second'|'minute'|'hour'|'day'|'month'|'year'} [options.unit] - if specified, will force a unit
  43. * @param {'floor'|'ceil'|'round'} [options.roundingMethod='round'] - which way to round partial units
  44. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  45. * @returns {String} the distance in words
  46. * @throws {TypeError} 2 arguments required
  47. * @throws {RangeError} `date` must not be Invalid Date
  48. * @throws {RangeError} `baseDate` must not be Invalid Date
  49. * @throws {RangeError} `options.roundingMethod` must be 'floor', 'ceil' or 'round'
  50. * @throws {RangeError} `options.unit` must be 'second', 'minute', 'hour', 'day', 'month' or 'year'
  51. * @throws {RangeError} `options.locale` must contain `formatDistance` property
  52. *
  53. * @example
  54. * // What is the distance between 2 July 2014 and 1 January 2015?
  55. * const result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2))
  56. * //=> '6 months'
  57. *
  58. * @example
  59. * // What is the distance between 1 January 2015 00:00:15
  60. * // and 1 January 2015 00:00:00?
  61. * const result = formatDistanceStrict(
  62. * new Date(2015, 0, 1, 0, 0, 15),
  63. * new Date(2015, 0, 1, 0, 0, 0)
  64. * )
  65. * //=> '15 seconds'
  66. *
  67. * @example
  68. * // What is the distance from 1 January 2016
  69. * // to 1 January 2015, with a suffix?
  70. * const result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), {
  71. * addSuffix: true
  72. * })
  73. * //=> '1 year ago'
  74. *
  75. * @example
  76. * // What is the distance from 1 January 2016
  77. * // to 1 January 2015, in minutes?
  78. * const result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), {
  79. * unit: 'minute'
  80. * })
  81. * //=> '525600 minutes'
  82. *
  83. * @example
  84. * // What is the distance from 1 January 2015
  85. * // to 28 January 2015, in months, rounded up?
  86. * const result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), {
  87. * unit: 'month',
  88. * roundingMethod: 'ceil'
  89. * })
  90. * //=> '1 month'
  91. *
  92. * @example
  93. * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
  94. * import { eoLocale } from 'date-fns/locale/eo'
  95. * const result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), {
  96. * locale: eoLocale
  97. * })
  98. * //=> '1 jaro'
  99. */
  100. function formatDistanceStrict(dirtyDate, dirtyBaseDate, options) {
  101. var _ref, _options$locale, _options$roundingMeth;
  102. (0, _index8.default)(2, arguments);
  103. var defaultOptions = (0, _index.getDefaultOptions)();
  104. 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 : _index7.default;
  105. if (!locale.formatDistance) {
  106. throw new RangeError('locale must contain localize.formatDistance property');
  107. }
  108. var comparison = (0, _index3.default)(dirtyDate, dirtyBaseDate);
  109. if (isNaN(comparison)) {
  110. throw new RangeError('Invalid time value');
  111. }
  112. var localizeOptions = (0, _index6.default)((0, _index5.default)(options), {
  113. addSuffix: Boolean(options === null || options === void 0 ? void 0 : options.addSuffix),
  114. comparison: comparison
  115. });
  116. var dateLeft;
  117. var dateRight;
  118. if (comparison > 0) {
  119. dateLeft = (0, _index4.default)(dirtyBaseDate);
  120. dateRight = (0, _index4.default)(dirtyDate);
  121. } else {
  122. dateLeft = (0, _index4.default)(dirtyDate);
  123. dateRight = (0, _index4.default)(dirtyBaseDate);
  124. }
  125. var roundingMethod = String((_options$roundingMeth = options === null || options === void 0 ? void 0 : options.roundingMethod) !== null && _options$roundingMeth !== void 0 ? _options$roundingMeth : 'round');
  126. var roundingMethodFn;
  127. if (roundingMethod === 'floor') {
  128. roundingMethodFn = Math.floor;
  129. } else if (roundingMethod === 'ceil') {
  130. roundingMethodFn = Math.ceil;
  131. } else if (roundingMethod === 'round') {
  132. roundingMethodFn = Math.round;
  133. } else {
  134. throw new RangeError("roundingMethod must be 'floor', 'ceil' or 'round'");
  135. }
  136. var milliseconds = dateRight.getTime() - dateLeft.getTime();
  137. var minutes = milliseconds / MILLISECONDS_IN_MINUTE;
  138. var timezoneOffset = (0, _index2.default)(dateRight) - (0, _index2.default)(dateLeft);
  139. // Use DST-normalized difference in minutes for years, months and days;
  140. // use regular difference in minutes for hours, minutes and seconds.
  141. var dstNormalizedMinutes = (milliseconds - timezoneOffset) / MILLISECONDS_IN_MINUTE;
  142. var defaultUnit = options === null || options === void 0 ? void 0 : options.unit;
  143. var unit;
  144. if (!defaultUnit) {
  145. if (minutes < 1) {
  146. unit = 'second';
  147. } else if (minutes < 60) {
  148. unit = 'minute';
  149. } else if (minutes < MINUTES_IN_DAY) {
  150. unit = 'hour';
  151. } else if (dstNormalizedMinutes < MINUTES_IN_MONTH) {
  152. unit = 'day';
  153. } else if (dstNormalizedMinutes < MINUTES_IN_YEAR) {
  154. unit = 'month';
  155. } else {
  156. unit = 'year';
  157. }
  158. } else {
  159. unit = String(defaultUnit);
  160. }
  161. // 0 up to 60 seconds
  162. if (unit === 'second') {
  163. var seconds = roundingMethodFn(milliseconds / 1000);
  164. return locale.formatDistance('xSeconds', seconds, localizeOptions);
  165. // 1 up to 60 mins
  166. } else if (unit === 'minute') {
  167. var roundedMinutes = roundingMethodFn(minutes);
  168. return locale.formatDistance('xMinutes', roundedMinutes, localizeOptions);
  169. // 1 up to 24 hours
  170. } else if (unit === 'hour') {
  171. var hours = roundingMethodFn(minutes / 60);
  172. return locale.formatDistance('xHours', hours, localizeOptions);
  173. // 1 up to 30 days
  174. } else if (unit === 'day') {
  175. var days = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_DAY);
  176. return locale.formatDistance('xDays', days, localizeOptions);
  177. // 1 up to 12 months
  178. } else if (unit === 'month') {
  179. var months = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_MONTH);
  180. return months === 12 && defaultUnit !== 'month' ? locale.formatDistance('xYears', 1, localizeOptions) : locale.formatDistance('xMonths', months, localizeOptions);
  181. // 1 year up to max Date
  182. } else if (unit === 'year') {
  183. var years = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_YEAR);
  184. return locale.formatDistance('xYears', years, localizeOptions);
  185. }
  186. throw new RangeError("unit must be 'second', 'minute', 'hour', 'day', 'month' or 'year'");
  187. }
  188. module.exports = exports.default;