index.js 3.1 KB

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