index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getOverlappingDaysInIntervals;
  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. var MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000;
  10. /**
  11. * @name getOverlappingDaysInIntervals
  12. * @category Interval Helpers
  13. * @summary Get the number of days that overlap in two time intervals
  14. *
  15. * @description
  16. * Get the number of days that overlap in two time intervals
  17. *
  18. * ### v2.0.0 breaking changes:
  19. *
  20. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  21. *
  22. * - The function was renamed from `getOverlappingDaysInRanges` to `getOverlappingDaysInIntervals`.
  23. * This change was made to mirror the use of the word "interval" in standard ISO 8601:2004 terminology:
  24. *
  25. * ```
  26. * 2.1.3
  27. * time interval
  28. * part of the time axis limited by two instants
  29. * ```
  30. *
  31. * Also, this function now accepts an object with `start` and `end` properties
  32. * instead of two arguments as an interval.
  33. * This function now throws `RangeError` if the start of the interval is after its end
  34. * or if any date in the interval is `Invalid Date`.
  35. *
  36. * ```javascript
  37. * // Before v2.0.0
  38. *
  39. * getOverlappingDaysInRanges(
  40. * new Date(2014, 0, 10), new Date(2014, 0, 20),
  41. * new Date(2014, 0, 17), new Date(2014, 0, 21)
  42. * )
  43. *
  44. * // v2.0.0 onward
  45. *
  46. * getOverlappingDaysInIntervals(
  47. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  48. * { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
  49. * )
  50. * ```
  51. *
  52. * @param {Interval} intervalLeft - the first interval to compare. See [Interval]{@link docs/Interval}
  53. * @param {Interval} intervalRight - the second interval to compare. See [Interval]{@link docs/Interval}
  54. * @returns {Number} the number of days that overlap in two time intervals
  55. * @throws {TypeError} 2 arguments required
  56. * @throws {RangeError} The start of an interval cannot be after its end
  57. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  58. *
  59. * @example
  60. * // For overlapping time intervals adds 1 for each started overlapping day:
  61. * getOverlappingDaysInIntervals(
  62. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  63. * { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
  64. * )
  65. * //=> 3
  66. *
  67. * @example
  68. * // For non-overlapping time intervals returns 0:
  69. * getOverlappingDaysInIntervals(
  70. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  71. * { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
  72. * )
  73. * //=> 0
  74. */
  75. function getOverlappingDaysInIntervals(dirtyIntervalLeft, dirtyIntervalRight) {
  76. (0, _index2.default)(2, arguments);
  77. var intervalLeft = dirtyIntervalLeft || {};
  78. var intervalRight = dirtyIntervalRight || {};
  79. var leftStartTime = (0, _index.default)(intervalLeft.start).getTime();
  80. var leftEndTime = (0, _index.default)(intervalLeft.end).getTime();
  81. var rightStartTime = (0, _index.default)(intervalRight.start).getTime();
  82. 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`
  83. if (!(leftStartTime <= leftEndTime && rightStartTime <= rightEndTime)) {
  84. throw new RangeError('Invalid interval');
  85. }
  86. var isOverlapping = leftStartTime < rightEndTime && rightStartTime < leftEndTime;
  87. if (!isOverlapping) {
  88. return 0;
  89. }
  90. var overlapStartDate = rightStartTime < leftStartTime ? leftStartTime : rightStartTime;
  91. var overlapEndDate = rightEndTime > leftEndTime ? leftEndTime : rightEndTime;
  92. var differenceInMs = overlapEndDate - overlapStartDate;
  93. return Math.ceil(differenceInMs / MILLISECONDS_IN_DAY);
  94. }
  95. module.exports = exports.default;