* A utility function which checks if two date ranges overlap.
*
* @param {Object} givenPeriodLeft - the first period to compare.
* @param {Object} givenPeriodRight - the second period to compare.
* @returns {Object} { daysOverlap: number of days the overlap is present, hoursOverlap: number of hours the overlap is present, overlapStartDate: the start date of the overlap in time format, overlapEndDate: the end date of the overlap in time format }
* @throws {Error} Uncaught Error: Invalid period
*
* @example
* getOverlapDateInPeriods(
* { start: new Date(2021, 0, 11), end: new Date(2021, 0, 13) },
* { start: new Date(2021, 0, 11), end: new Date(2021, 0, 14) }
it('returns an overlap object that contains the amount of days overlapping, the amount of hours overlapping, start date of overlap and end date of overlap',()=>{
expect(
datetimeUtility.getOverlapDateInPeriods(
{start,end},
{start:givenPeriodLeft,end:givenPeriodRight},
),
).toEqual({
daysOverlap:2,
hoursOverlap:48,
overlapStartDate:givenPeriodLeft.getTime(),
overlapEndDate:end.getTime(),
});
});
});
describe('when date periods do not overlap',()=>{
constgivenPeriodLeft=newDate(2021,0,9);
constgivenPeriodRight=newDate(2021,0,10);
it('returns an overlap object that contains a 0 value for days overlapping',()=>{
expect(
datetimeUtility.getOverlapDateInPeriods(
{start,end},
{start:givenPeriodLeft,end:givenPeriodRight},
),
).toEqual({daysOverlap:0});
});
});
describe('when date periods contain an invalid Date',()=>{
conststartInvalid=newDate(NaN);
constendInvalid=newDate(NaN);
consterror=__('Invalid period');
it('throws an exception when the left period contains an invalid date',()=>{