index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * ### v2.0.0 breaking changes:
  12. *
  13. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  14. *
  15. * - The function was renamed from `areRangesOverlapping` to `areIntervalsOverlapping`.
  16. * This change was made to mirror the use of the word "interval" in standard ISO 8601:2004 terminology:
  17. *
  18. * ```
  19. * 2.1.3
  20. * time interval
  21. * part of the time axis limited by two instants
  22. * ```
  23. *
  24. * Also, this function now accepts an object with `start` and `end` properties
  25. * instead of two arguments as an interval.
  26. * This function now throws `RangeError` if the start of the interval is after its end
  27. * or if any date in the interval is `Invalid Date`.
  28. *
  29. * ```javascript
  30. * // Before v2.0.0
  31. *
  32. * areRangesOverlapping(
  33. * new Date(2014, 0, 10), new Date(2014, 0, 20),
  34. * new Date(2014, 0, 17), new Date(2014, 0, 21)
  35. * )
  36. *
  37. * // v2.0.0 onward
  38. *
  39. * areIntervalsOverlapping(
  40. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  41. * { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
  42. * )
  43. * ```
  44. *
  45. * @param {Interval} intervalLeft - the first interval to compare. See [Interval]{@link https://date-fns.org/docs/Interval}
  46. * @param {Interval} intervalRight - the second interval to compare. See [Interval]{@link https://date-fns.org/docs/Interval}
  47. * @param {Object} [options] - the object with options
  48. * @param {Boolean} [options.inclusive=false] - whether the comparison is inclusive or not
  49. * @returns {Boolean} whether the time intervals are overlapping
  50. * @throws {TypeError} 2 arguments required
  51. * @throws {RangeError} The start of an interval cannot be after its end
  52. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  53. *
  54. * @example
  55. * // For overlapping time intervals:
  56. * areIntervalsOverlapping(
  57. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  58. * { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
  59. * )
  60. * //=> true
  61. *
  62. * @example
  63. * // For non-overlapping time intervals:
  64. * areIntervalsOverlapping(
  65. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  66. * { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
  67. * )
  68. * //=> false
  69. *
  70. * @example
  71. * // For adjacent time intervals:
  72. * areIntervalsOverlapping(
  73. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  74. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 30) }
  75. * )
  76. * //=> false
  77. *
  78. * @example
  79. * // Using the inclusive option:
  80. * areIntervalsOverlapping(
  81. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  82. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) }
  83. * )
  84. * //=> false
  85. * areIntervalsOverlapping(
  86. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  87. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) },
  88. * { inclusive: true }
  89. * )
  90. * //=> true
  91. */
  92. export default function areIntervalsOverlapping(dirtyIntervalLeft, dirtyIntervalRight) {
  93. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
  94. inclusive: false
  95. };
  96. requiredArgs(2, arguments);
  97. var intervalLeft = dirtyIntervalLeft || {};
  98. var intervalRight = dirtyIntervalRight || {};
  99. var leftStartTime = toDate(intervalLeft.start).getTime();
  100. var leftEndTime = toDate(intervalLeft.end).getTime();
  101. var rightStartTime = toDate(intervalRight.start).getTime();
  102. var rightEndTime = toDate(intervalRight.end).getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  103. if (!(leftStartTime <= leftEndTime && rightStartTime <= rightEndTime)) {
  104. throw new RangeError('Invalid interval');
  105. }
  106. if (options.inclusive) {
  107. return leftStartTime <= rightEndTime && rightStartTime <= leftEndTime;
  108. }
  109. return leftStartTime < rightEndTime && rightStartTime < leftEndTime;
  110. }