index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = areIntervalsOverlapping;
  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 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. * ### 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 `areRangesOverlapping` to `areIntervalsOverlapping`.
  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. * areRangesOverlapping(
  39. * new Date(2014, 0, 10), new Date(2014, 0, 20),
  40. * new Date(2014, 0, 17), new Date(2014, 0, 21)
  41. * )
  42. *
  43. * // v2.0.0 onward
  44. *
  45. * areIntervalsOverlapping(
  46. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  47. * { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
  48. * )
  49. * ```
  50. *
  51. * @param {Interval} intervalLeft - the first interval to compare. See [Interval]{@link https://date-fns.org/docs/Interval}
  52. * @param {Interval} intervalRight - the second interval to compare. See [Interval]{@link https://date-fns.org/docs/Interval}
  53. * @param {Object} [options] - the object with options
  54. * @param {Boolean} [options.inclusive=false] - whether the comparison is inclusive or not
  55. * @returns {Boolean} whether the time intervals are overlapping
  56. * @throws {TypeError} 2 arguments required
  57. * @throws {RangeError} The start of an interval cannot be after its end
  58. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  59. *
  60. * @example
  61. * // For overlapping time intervals:
  62. * areIntervalsOverlapping(
  63. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  64. * { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
  65. * )
  66. * //=> true
  67. *
  68. * @example
  69. * // For non-overlapping time intervals:
  70. * areIntervalsOverlapping(
  71. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  72. * { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
  73. * )
  74. * //=> false
  75. *
  76. * @example
  77. * // For adjacent time intervals:
  78. * areIntervalsOverlapping(
  79. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  80. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 30) }
  81. * )
  82. * //=> false
  83. *
  84. * @example
  85. * // Using the inclusive option:
  86. * areIntervalsOverlapping(
  87. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  88. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) }
  89. * )
  90. * //=> false
  91. * areIntervalsOverlapping(
  92. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  93. * { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) },
  94. * { inclusive: true }
  95. * )
  96. * //=> true
  97. */
  98. function areIntervalsOverlapping(dirtyIntervalLeft, dirtyIntervalRight) {
  99. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
  100. inclusive: false
  101. };
  102. (0, _index2.default)(2, arguments);
  103. var intervalLeft = dirtyIntervalLeft || {};
  104. var intervalRight = dirtyIntervalRight || {};
  105. var leftStartTime = (0, _index.default)(intervalLeft.start).getTime();
  106. var leftEndTime = (0, _index.default)(intervalLeft.end).getTime();
  107. var rightStartTime = (0, _index.default)(intervalRight.start).getTime();
  108. var rightEndTime = (0, _index.default)(intervalRight.end).getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  109. if (!(leftStartTime <= leftEndTime && rightStartTime <= rightEndTime)) {
  110. throw new RangeError('Invalid interval');
  111. }
  112. if (options.inclusive) {
  113. return leftStartTime <= rightEndTime && rightStartTime <= leftEndTime;
  114. }
  115. return leftStartTime < rightEndTime && rightStartTime < leftEndTime;
  116. }
  117. module.exports = exports.default;