Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a periods overlap helper #548

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/lib/general.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ function generateUUID () {
return randomUUID({ disableEntropyCache: true })
}

/**
* Checks if any of the periods in checkPeriods overlap with any period in referencePeriods
* @param {*} referencePeriods Array of objects representing periods with startDate and endDate properties
* @param {*} checkPeriods Array of objects representing periods to check with startDate and endDate properties
* @returns Returns true if there is an overlap, otherwise false
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
*/
function periodsOverlap (referencePeriods, checkPeriods) {
for (const referencePeriod of referencePeriods) {
const overLappingPeriods = checkPeriods.filter((checkPeriod) => {
if (checkPeriod.startDate > referencePeriod.endDate || referencePeriod.startDate > checkPeriod.endDate) {
return false
}

return true
})

if (overLappingPeriods.length) {
return true
}
}

return false
}

/**
* Returns the current date and time as an ISO string
*
Expand All @@ -44,5 +68,6 @@ function timestampForPostgres () {

module.exports = {
generateUUID,
periodsOverlap,
timestampForPostgres
}
45 changes: 45 additions & 0 deletions test/lib/general.lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,49 @@ describe('RequestLib', () => {
expect(result).to.equal('2015-10-21T20:31:57.000Z')
})
})

describe('#periodsOverlap', () => {
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
let referencePeriod
let checkPeriod

describe('when given dates that do not overlap', () => {
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
beforeEach(() => {
referencePeriod = [{
startDate: new Date('2023-02-01'),
endDate: new Date('2023-02-02')
}]

checkPeriod = [{
startDate: new Date('2023-01-01'),
endDate: new Date('2023-01-31')
}]
})

it('returns false', () => {
const result = GeneralLib.periodsOverlap(referencePeriod, checkPeriod)

expect(result).to.equal(false)
})
})

describe('when given dates that do overlap', () => {
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
beforeEach(() => {
referencePeriod = [{
startDate: new Date('2023-01-01'),
endDate: new Date('2023-01-02')
}]

checkPeriod = [{
startDate: new Date('2023-01-01'),
endDate: new Date('2023-01-31')
}]
})

it('returns true', () => {
const result = GeneralLib.periodsOverlap(referencePeriod, checkPeriod)

expect(result).to.equal(true)
})
})
})
})
Loading