index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isWithinInterval;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name isWithinInterval
  11. * @category Interval Helpers
  12. * @summary Is the given date within the interval?
  13. *
  14. * @description
  15. * Is the given date within the interval? (Including start and end.)
  16. *
  17. * ### v2.0.0 breaking changes:
  18. *
  19. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  20. *
  21. * - The function was renamed from `isWithinRange` to `isWithinInterval`.
  22. * This change was made to mirror the use of the word "interval" in standard ISO 8601:2004 terminology:
  23. *
  24. * ```
  25. * 2.1.3
  26. * time interval
  27. * part of the time axis limited by two instants
  28. * ```
  29. *
  30. * Also, this function now accepts an object with `start` and `end` properties
  31. * instead of two arguments as an interval.
  32. * This function now throws `RangeError` if the start of the interval is after its end
  33. * or if any date in the interval is `Invalid Date`.
  34. *
  35. * ```javascript
  36. * // Before v2.0.0
  37. *
  38. * isWithinRange(
  39. * new Date(2014, 0, 3),
  40. * new Date(2014, 0, 1), new Date(2014, 0, 7)
  41. * )
  42. *
  43. * // v2.0.0 onward
  44. *
  45. * isWithinInterval(
  46. * new Date(2014, 0, 3),
  47. * { start: new Date(2014, 0, 1), end: new Date(2014, 0, 7) }
  48. * )
  49. * ```
  50. *
  51. * @param {Date|Number} date - the date to check
  52. * @param {Interval} interval - the interval to check
  53. * @returns {Boolean} the date is within the interval
  54. * @throws {TypeError} 2 arguments required
  55. * @throws {RangeError} The start of an interval cannot be after its end
  56. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  57. *
  58. * @example
  59. * // For the date within the interval:
  60. * isWithinInterval(new Date(2014, 0, 3), {
  61. * start: new Date(2014, 0, 1),
  62. * end: new Date(2014, 0, 7)
  63. * })
  64. * //=> true
  65. *
  66. * @example
  67. * // For the date outside of the interval:
  68. * isWithinInterval(new Date(2014, 0, 10), {
  69. * start: new Date(2014, 0, 1),
  70. * end: new Date(2014, 0, 7)
  71. * })
  72. * //=> false
  73. *
  74. * @example
  75. * // For date equal to interval start:
  76. * isWithinInterval(date, { start, end: date }) // => true
  77. *
  78. * @example
  79. * // For date equal to interval end:
  80. * isWithinInterval(date, { start: date, end }) // => true
  81. */
  82. function isWithinInterval(dirtyDate, interval) {
  83. (0, _index2.default)(2, arguments);
  84. var time = (0, _index.default)(dirtyDate).getTime();
  85. var startTime = (0, _index.default)(interval.start).getTime();
  86. var endTime = (0, _index.default)(interval.end).getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  87. if (!(startTime <= endTime)) {
  88. throw new RangeError('Invalid interval');
  89. }
  90. return time >= startTime && time <= endTime;
  91. }
  92. module.exports = exports.default;