index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = areIntervalsOverlapping;
  7. var _index = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. /**
  10. * @name areIntervalsOverlapping
  11. * @category Interval Helpers
  12. * @summary Is the given time interval overlapping with another time interval?
  13. *
  14. * @description
  15. * Is the given time interval overlapping with another time interval? Adjacent intervals do not count as overlapping.
  16. *
  17. * @param {Interval} intervalLeft - the first interval to compare. See [Interval]{@link https://date-fns.org/docs/Interval}
  18. * @param {Interval} intervalRight - the second interval to compare. See [Interval]{@link https://date-fns.org/docs/Interval}
  19. * @param {Object} [options] - the object with options
  20. * @param {Boolean} [options.inclusive=false] - whether the comparison is inclusive or not
  21. * @returns {Boolean} whether the time intervals are overlapping
  22. * @throws {TypeError} 2 arguments required
  23. * @throws {RangeError} The start of an interval cannot be after its end
  24. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  25. *
  26. * @example
  27. * // For overlapping time intervals:
  28. * areIntervalsOverlapping(
  29. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  30. * { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
  31. * )
  32. * //=> true
  33. *
  34. * @example
  35. * // For non-overlapping time intervals:
  36. * areIntervalsOverlapping(
  37. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  38. * { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
  39. * )
  40. * //=> false
  41. *
  42. * @example
  43. * // For adjacent time intervals:
  44. * areIntervalsOverlapping(
  45. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  46. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 30) }
  47. * )
  48. * //=> false
  49. *
  50. * @example
  51. * // Using the inclusive option:
  52. * areIntervalsOverlapping(
  53. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  54. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) }
  55. * )
  56. * //=> false
  57. * areIntervalsOverlapping(
  58. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  59. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) },
  60. * { inclusive: true }
  61. * )
  62. * //=> true
  63. */
  64. function areIntervalsOverlapping(intervalLeft, intervalRight, options) {
  65. (0, _index2.default)(2, arguments);
  66. var leftStartTime = (0, _index.default)(intervalLeft === null || intervalLeft === void 0 ? void 0 : intervalLeft.start).getTime();
  67. var leftEndTime = (0, _index.default)(intervalLeft === null || intervalLeft === void 0 ? void 0 : intervalLeft.end).getTime();
  68. var rightStartTime = (0, _index.default)(intervalRight === null || intervalRight === void 0 ? void 0 : intervalRight.start).getTime();
  69. var rightEndTime = (0, _index.default)(intervalRight === null || intervalRight === void 0 ? void 0 : intervalRight.end).getTime();
  70. // Throw an exception if start date is after end date or if any date is `Invalid Date`
  71. if (!(leftStartTime <= leftEndTime && rightStartTime <= rightEndTime)) {
  72. throw new RangeError('Invalid interval');
  73. }
  74. if (options !== null && options !== void 0 && options.inclusive) {
  75. return leftStartTime <= rightEndTime && rightStartTime <= leftEndTime;
  76. }
  77. return leftStartTime < rightEndTime && rightStartTime < leftEndTime;
  78. }
  79. module.exports = exports.default;