Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

112 строки
4.1 KiB

  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 docs/types/Interval}
  46. * @param {Interval} intervalRight - the second interval to compare. See [Interval]{@link docs/types/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. requiredArgs(2, arguments);
  95. var intervalLeft = dirtyIntervalLeft || {};
  96. var intervalRight = dirtyIntervalRight || {};
  97. var leftStartTime = toDate(intervalLeft.start).getTime();
  98. var leftEndTime = toDate(intervalLeft.end).getTime();
  99. var rightStartTime = toDate(intervalRight.start).getTime();
  100. var rightEndTime = toDate(intervalRight.end).getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  101. if (!(leftStartTime <= leftEndTime && rightStartTime <= rightEndTime)) {
  102. throw new RangeError('Invalid interval');
  103. }
  104. if (options.inclusive) {
  105. return leftStartTime <= rightEndTime && rightStartTime <= leftEndTime;
  106. }
  107. return leftStartTime < rightEndTime && rightStartTime < leftEndTime;
  108. }