index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = formatDistanceStrict;
  6. var _index = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
  7. var _index2 = _interopRequireDefault(require("../compareAsc/index.js"));
  8. var _index3 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/cloneObject/index.js"));
  10. var _index5 = _interopRequireDefault(require("../locale/en-US/index.js"));
  11. var _index6 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. var MILLISECONDS_IN_MINUTE = 1000 * 60;
  14. var MINUTES_IN_DAY = 60 * 24;
  15. var MINUTES_IN_MONTH = MINUTES_IN_DAY * 30;
  16. var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365;
  17. /**
  18. * @name formatDistanceStrict
  19. * @category Common Helpers
  20. * @summary Return the distance between the given dates in words.
  21. *
  22. * @description
  23. * Return the distance between the given dates in words, using strict units.
  24. * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
  25. * 'less than' and the like.
  26. *
  27. * | Distance between dates | Result |
  28. * |------------------------|---------------------|
  29. * | 0 ... 59 secs | [0..59] seconds |
  30. * | 1 ... 59 mins | [1..59] minutes |
  31. * | 1 ... 23 hrs | [1..23] hours |
  32. * | 1 ... 29 days | [1..29] days |
  33. * | 1 ... 11 months | [1..11] months |
  34. * | 1 ... N years | [1..N] years |
  35. *
  36. * ### v2.0.0 breaking changes:
  37. *
  38. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  39. *
  40. * - The function was renamed from `distanceInWordsStrict` to `formatDistanceStrict`
  41. * to make its name consistent with `format` and `formatRelative`.
  42. *
  43. * - The order of arguments is swapped to make the function
  44. * consistent with `differenceIn...` functions.
  45. *
  46. * ```javascript
  47. * // Before v2.0.0
  48. *
  49. * distanceInWordsStrict(
  50. * new Date(2015, 0, 2),
  51. * new Date(2014, 6, 2)
  52. * ) //=> '6 months'
  53. *
  54. * // v2.0.0 onward
  55. *
  56. * formatDistanceStrict(
  57. * new Date(2014, 6, 2),
  58. * new Date(2015, 0, 2)
  59. * ) //=> '6 months'
  60. * ```
  61. *
  62. * - `partialMethod` option is renamed to `roundingMethod`.
  63. *
  64. * ```javascript
  65. * // Before v2.0.0
  66. *
  67. * distanceInWordsStrict(
  68. * new Date(1986, 3, 4, 10, 32, 0),
  69. * new Date(1986, 3, 4, 10, 33, 1),
  70. * { partialMethod: 'ceil' }
  71. * ) //=> '2 minutes'
  72. *
  73. * // v2.0.0 onward
  74. *
  75. * formatDistanceStrict(
  76. * new Date(1986, 3, 4, 10, 33, 1),
  77. * new Date(1986, 3, 4, 10, 32, 0),
  78. * { roundingMethod: 'ceil' }
  79. * ) //=> '2 minutes'
  80. * ```
  81. *
  82. * - If `roundingMethod` is not specified, it now defaults to `round` instead of `floor`.
  83. *
  84. * - `unit` option now accepts one of the strings:
  85. * 'second', 'minute', 'hour', 'day', 'month' or 'year' instead of 's', 'm', 'h', 'd', 'M' or 'Y'
  86. *
  87. * ```javascript
  88. * // Before v2.0.0
  89. *
  90. * distanceInWordsStrict(
  91. * new Date(1986, 3, 4, 10, 32, 0),
  92. * new Date(1986, 3, 4, 10, 33, 1),
  93. * { unit: 'm' }
  94. * )
  95. *
  96. * // v2.0.0 onward
  97. *
  98. * formatDistanceStrict(
  99. * new Date(1986, 3, 4, 10, 33, 1),
  100. * new Date(1986, 3, 4, 10, 32, 0),
  101. * { unit: 'minute' }
  102. * )
  103. * ```
  104. *
  105. * @param {Date|Number} date - the date
  106. * @param {Date|Number} baseDate - the date to compare with
  107. * @param {Object} [options] - an object with options.
  108. * @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
  109. * @param {'second'|'minute'|'hour'|'day'|'month'|'year'} [options.unit] - if specified, will force a unit
  110. * @param {'floor'|'ceil'|'round'} [options.roundingMethod='round'] - which way to round partial units
  111. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  112. * @returns {String} the distance in words
  113. * @throws {TypeError} 2 arguments required
  114. * @throws {RangeError} `date` must not be Invalid Date
  115. * @throws {RangeError} `baseDate` must not be Invalid Date
  116. * @throws {RangeError} `options.roundingMethod` must be 'floor', 'ceil' or 'round'
  117. * @throws {RangeError} `options.unit` must be 'second', 'minute', 'hour', 'day', 'month' or 'year'
  118. * @throws {RangeError} `options.locale` must contain `formatDistance` property
  119. *
  120. * @example
  121. * // What is the distance between 2 July 2014 and 1 January 2015?
  122. * const result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2))
  123. * //=> '6 months'
  124. *
  125. * @example
  126. * // What is the distance between 1 January 2015 00:00:15
  127. * // and 1 January 2015 00:00:00?
  128. * const result = formatDistanceStrict(
  129. * new Date(2015, 0, 1, 0, 0, 15),
  130. * new Date(2015, 0, 1, 0, 0, 0)
  131. * )
  132. * //=> '15 seconds'
  133. *
  134. * @example
  135. * // What is the distance from 1 January 2016
  136. * // to 1 January 2015, with a suffix?
  137. * const result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), {
  138. * addSuffix: true
  139. * })
  140. * //=> '1 year ago'
  141. *
  142. * @example
  143. * // What is the distance from 1 January 2016
  144. * // to 1 January 2015, in minutes?
  145. * const result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), {
  146. * unit: 'minute'
  147. * })
  148. * //=> '525600 minutes'
  149. *
  150. * @example
  151. * // What is the distance from 1 January 2015
  152. * // to 28 January 2015, in months, rounded up?
  153. * const result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), {
  154. * unit: 'month',
  155. * roundingMethod: 'ceil'
  156. * })
  157. * //=> '1 month'
  158. *
  159. * @example
  160. * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
  161. * import { eoLocale } from 'date-fns/locale/eo'
  162. * const result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), {
  163. * locale: eoLocale
  164. * })
  165. * //=> '1 jaro'
  166. */
  167. function formatDistanceStrict(dirtyDate, dirtyBaseDate) {
  168. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  169. (0, _index6.default)(2, arguments);
  170. var locale = options.locale || _index5.default;
  171. if (!locale.formatDistance) {
  172. throw new RangeError('locale must contain localize.formatDistance property');
  173. }
  174. var comparison = (0, _index2.default)(dirtyDate, dirtyBaseDate);
  175. if (isNaN(comparison)) {
  176. throw new RangeError('Invalid time value');
  177. }
  178. var localizeOptions = (0, _index4.default)(options);
  179. localizeOptions.addSuffix = Boolean(options.addSuffix);
  180. localizeOptions.comparison = comparison;
  181. var dateLeft;
  182. var dateRight;
  183. if (comparison > 0) {
  184. dateLeft = (0, _index3.default)(dirtyBaseDate);
  185. dateRight = (0, _index3.default)(dirtyDate);
  186. } else {
  187. dateLeft = (0, _index3.default)(dirtyDate);
  188. dateRight = (0, _index3.default)(dirtyBaseDate);
  189. }
  190. var roundingMethod = options.roundingMethod == null ? 'round' : String(options.roundingMethod);
  191. var roundingMethodFn;
  192. if (roundingMethod === 'floor') {
  193. roundingMethodFn = Math.floor;
  194. } else if (roundingMethod === 'ceil') {
  195. roundingMethodFn = Math.ceil;
  196. } else if (roundingMethod === 'round') {
  197. roundingMethodFn = Math.round;
  198. } else {
  199. throw new RangeError("roundingMethod must be 'floor', 'ceil' or 'round'");
  200. }
  201. var milliseconds = dateRight.getTime() - dateLeft.getTime();
  202. var minutes = milliseconds / MILLISECONDS_IN_MINUTE;
  203. var timezoneOffset = (0, _index.default)(dateRight) - (0, _index.default)(dateLeft); // Use DST-normalized difference in minutes for years, months and days;
  204. // use regular difference in minutes for hours, minutes and seconds.
  205. var dstNormalizedMinutes = (milliseconds - timezoneOffset) / MILLISECONDS_IN_MINUTE;
  206. var unit;
  207. if (options.unit == null) {
  208. if (minutes < 1) {
  209. unit = 'second';
  210. } else if (minutes < 60) {
  211. unit = 'minute';
  212. } else if (minutes < MINUTES_IN_DAY) {
  213. unit = 'hour';
  214. } else if (dstNormalizedMinutes < MINUTES_IN_MONTH) {
  215. unit = 'day';
  216. } else if (dstNormalizedMinutes < MINUTES_IN_YEAR) {
  217. unit = 'month';
  218. } else {
  219. unit = 'year';
  220. }
  221. } else {
  222. unit = String(options.unit);
  223. } // 0 up to 60 seconds
  224. if (unit === 'second') {
  225. var seconds = roundingMethodFn(milliseconds / 1000);
  226. return locale.formatDistance('xSeconds', seconds, localizeOptions); // 1 up to 60 mins
  227. } else if (unit === 'minute') {
  228. var roundedMinutes = roundingMethodFn(minutes);
  229. return locale.formatDistance('xMinutes', roundedMinutes, localizeOptions); // 1 up to 24 hours
  230. } else if (unit === 'hour') {
  231. var hours = roundingMethodFn(minutes / 60);
  232. return locale.formatDistance('xHours', hours, localizeOptions); // 1 up to 30 days
  233. } else if (unit === 'day') {
  234. var days = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_DAY);
  235. return locale.formatDistance('xDays', days, localizeOptions); // 1 up to 12 months
  236. } else if (unit === 'month') {
  237. var months = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_MONTH);
  238. return months === 12 && options.unit !== 'month' ? locale.formatDistance('xYears', 1, localizeOptions) : locale.formatDistance('xMonths', months, localizeOptions); // 1 year up to max Date
  239. } else if (unit === 'year') {
  240. var years = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_YEAR);
  241. return locale.formatDistance('xYears', years, localizeOptions);
  242. }
  243. throw new RangeError("unit must be 'second', 'minute', 'hour', 'day', 'month' or 'year'");
  244. }
  245. module.exports = exports.default;