-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FeaturePanel: Enhancements to the opening hours (#516)
- Loading branch information
Showing
6 changed files
with
198 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/components/FeaturePanel/renderers/openingHours/__tests__/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { splitDateRangeAtMidnight } from '../utils'; | ||
|
||
describe('splitDateRangeAtMidnight', () => { | ||
it('should not split it when it isn not needed', () => { | ||
expect( | ||
splitDateRangeAtMidnight([ | ||
new Date(2024, 4, 4, 4, 4), | ||
new Date(2024, 4, 4, 6, 6), | ||
]), | ||
).toEqual([[new Date(2024, 4, 4, 4, 4), new Date(2024, 4, 4, 6, 6)]]); | ||
}); | ||
|
||
it('should not split it at midnight into x sub ranges', () => { | ||
expect( | ||
splitDateRangeAtMidnight([ | ||
new Date(2024, 4, 4, 4, 4), | ||
new Date(2024, 4, 5, 6, 6), | ||
]), | ||
).toEqual([ | ||
[new Date(2024, 4, 4, 4, 4), new Date(2024, 4, 5, 0, 0)], | ||
[new Date(2024, 4, 5, 0, 0), new Date(2024, 4, 5, 6, 6)], | ||
]); | ||
|
||
expect( | ||
splitDateRangeAtMidnight([ | ||
new Date(2024, 4, 4, 4, 4), | ||
new Date(2024, 4, 6, 6, 6), | ||
]), | ||
).toEqual([ | ||
[new Date(2024, 4, 4, 4, 4), new Date(2024, 4, 5, 0, 0)], | ||
[new Date(2024, 4, 5, 0, 0), new Date(2024, 4, 6, 0, 0)], | ||
[new Date(2024, 4, 6, 0, 0), new Date(2024, 4, 6, 6, 6)], | ||
]); | ||
}); | ||
|
||
it('should work when the dates start/end at midnight', () => { | ||
// Test case where the start and end time is exactly at midnight | ||
expect( | ||
splitDateRangeAtMidnight([ | ||
new Date(2024, 4, 4, 0, 0), | ||
new Date(2024, 4, 5, 0, 0), | ||
]), | ||
).toEqual([[new Date(2024, 4, 4, 0, 0), new Date(2024, 4, 5, 0, 0)]]); | ||
|
||
// Test case where start time is exactly at midnight and end time is later the same day | ||
expect( | ||
splitDateRangeAtMidnight([ | ||
new Date(2024, 4, 4, 0, 0), | ||
new Date(2024, 4, 4, 23, 59), | ||
]), | ||
).toEqual([[new Date(2024, 4, 4, 0, 0), new Date(2024, 4, 4, 23, 59)]]); | ||
|
||
// Test case where start time is just before midnight and end time is exactly at midnight | ||
expect( | ||
splitDateRangeAtMidnight([ | ||
new Date(2024, 4, 4, 23, 59), | ||
new Date(2024, 4, 5, 0, 0), | ||
]), | ||
).toEqual([[new Date(2024, 4, 4, 23, 59), new Date(2024, 4, 5, 0, 0)]]); | ||
|
||
// Test case where range starts and ends on different midnights | ||
expect( | ||
splitDateRangeAtMidnight([ | ||
new Date(2024, 4, 4, 0, 0), | ||
new Date(2024, 4, 6, 0, 0), | ||
]), | ||
).toEqual([ | ||
[new Date(2024, 4, 4, 0, 0), new Date(2024, 4, 5, 0, 0)], | ||
[new Date(2024, 4, 5, 0, 0), new Date(2024, 4, 6, 0, 0)], | ||
]); | ||
|
||
// Test case where range starts at midnight and ends at a time that is not midnight | ||
expect( | ||
splitDateRangeAtMidnight([ | ||
new Date(2024, 4, 4, 0, 0), | ||
new Date(2024, 4, 5, 12, 0), | ||
]), | ||
).toEqual([ | ||
[new Date(2024, 4, 4, 0, 0), new Date(2024, 4, 5, 0, 0)], | ||
[new Date(2024, 4, 5, 0, 0), new Date(2024, 4, 5, 12, 0)], | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 34 additions & 2 deletions
36
src/components/FeaturePanel/renderers/openingHours/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,34 @@ | ||
export const isInRange = ([startDate, endDate]: [Date, Date], date: Date) => | ||
date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime(); | ||
export type DateRange = [Date, Date]; | ||
|
||
const isSameDay = (date1: Date, date2: Date) => | ||
date1.getFullYear() === date2.getFullYear() && | ||
date1.getMonth() === date2.getMonth() && | ||
date1.getDate() === date2.getDate(); | ||
|
||
export const isMidnight = (date: Date) => | ||
date.getHours() === 0 && | ||
date.getMinutes() === 0 && | ||
date.getSeconds() === 0 && | ||
date.getMilliseconds() === 0; | ||
|
||
export function splitDateRangeAtMidnight( | ||
[startDate, endDate]: DateRange, | ||
shouldSplit = (d1: Date, d2: Date) => !isSameDay(d1, d2), | ||
): DateRange[] { | ||
const midnight = new Date(startDate); | ||
midnight.setHours(0, 0, 0, 0); | ||
midnight.setDate(midnight.getDate() + 1); | ||
|
||
if (startDate.getTime() === endDate.getTime()) { | ||
return []; | ||
} | ||
|
||
if (!shouldSplit(startDate, endDate)) { | ||
return [[startDate, endDate]]; | ||
} | ||
|
||
return [ | ||
[startDate, midnight], | ||
...splitDateRangeAtMidnight([midnight, endDate], shouldSplit), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters