import toDate from '../toDate/index' ; import requiredArgs from '../_lib/requiredArgs/index' ; import type { Interval } from '../types' ; const MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000 ; name category summary description param { Interval } intervalLeft - the first interval to compare. See [Interval] docs param { Interval } intervalRight - the second interval to compare. See [Interval] docs returns { Number } throws throws throws example example export default function getOverlappingDaysInIntervals ( dirtyIntervalLeft , dirtyIntervalRight ) { requiredArgs ( 2 , arguments ) ; const intervalLeft = dirtyIntervalLeft || { } ; const intervalRight = dirtyIntervalRight || { } ; const leftStartTime = toDate ( intervalLeft . start ) . getTime ( ) ; const leftEndTime = toDate ( intervalLeft . end ) . getTime ( ) ; const rightStartTime = toDate ( intervalRight . start ) . getTime ( ) ; const rightEndTime = toDate ( intervalRight . end ) . getTime ( ) ; if ( ! ( leftStartTime <= leftEndTime && rightStartTime <= rightEndTime ) ) { throw new RangeError ( 'Invalid interval' ) ; } const isOverlapping = leftStartTime < rightEndTime && rightStartTime < leftEndTime ; if ( ! isOverlapping ) { return 0 ; } const overlapStartDate = rightStartTime < leftStartTime ? leftStartTime : rightStartTime ; const overlapEndDate = rightEndTime > leftEndTime ? leftEndTime : rightEndTime ; const differenceInMs = overlapEndDate - overlapStartDate ; return Math . ceil ( differenceInMs / MILLISECONDS_IN_DAY ) ; }