-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Implement a new library for opening hours #330
Changes from 3 commits
85558da
8cb53a7
e0c51cf
dee4808
32c10fe
1f8913a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import OpeningHours from 'opening_hours'; | ||
import { isInRange } from './utils'; | ||
import { SimpleOpeningHoursTable } from './types'; | ||
|
||
type Weekday = keyof SimpleOpeningHoursTable; | ||
const weekdays: Weekday[] = ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa', 'ph']; | ||
const weekdayMappings: Record<string, Weekday> = { | ||
Sun: 'su', | ||
Mon: 'mo', | ||
Tue: 'tu', | ||
Wed: 'we', | ||
Thu: 'th', | ||
Fri: 'fr', | ||
Sat: 'sa', | ||
}; | ||
|
||
const fmtDate = (d: Date) => | ||
d.toLocaleTimeString(undefined, { hour: 'numeric', minute: 'numeric' }); | ||
|
||
export const parseComplexOpeningHours = (value: string) => { | ||
const oh = new OpeningHours(value); | ||
|
||
const today = new Date(); | ||
today.setHours(0, 0, 0, 0); | ||
|
||
const oneWeekLater = new Date(today); | ||
oneWeekLater.setDate(oneWeekLater.getDate() + 7); | ||
|
||
const intervals = oh.getOpenIntervals(today, oneWeekLater); | ||
const grouped = weekdays.map((w) => { | ||
const daysIntervals = intervals.filter( | ||
([from]) => | ||
w === weekdayMappings[from.toLocaleString('en', { weekday: 'short' })], | ||
); | ||
|
||
return [w, daysIntervals] as const; | ||
}); | ||
|
||
const daysTable = Object.fromEntries( | ||
grouped.map((entry) => { | ||
const strings = entry[1].map( | ||
([from, due]) => `${fmtDate(from)}-${fmtDate(due)}`, | ||
); | ||
|
||
return [entry[0], strings] as const; | ||
}), | ||
) as unknown as SimpleOpeningHoursTable; | ||
|
||
return { | ||
daysTable, | ||
isOpen: intervals.some(([from, due]) => isInRange([from, due], new Date())), | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { parseComplexOpeningHours } from './complex'; | ||
import { parseSimpleOpeningHours } from './simple'; | ||
|
||
export const parseOpeningHours = (value: string) => { | ||
try { | ||
return parseComplexOpeningHours(value); | ||
} catch { | ||
return parseSimpleOpeningHours(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you expect this exception more often? I would say we can discard the If we do this it can also simplify the |
||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { SimpleOpeningHours } from 'simple-opening-hours'; | ||
import { SimpleOpeningHoursTable } from './types'; | ||
|
||
export const parseSimpleOpeningHours = (value: string) => { | ||
const sanitized = value.match(/^[0-9:]+-[0-9:]+$/) ? `Mo-Su ${value}` : value; | ||
const opening = new SimpleOpeningHours(sanitized); | ||
const daysTable = opening.getTable() as SimpleOpeningHoursTable; | ||
const isOpen = opening.isOpenNow(); | ||
return { daysTable, isOpen }; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export interface SimpleOpeningHoursTable { | ||
su: string[]; | ||
mo: string[]; | ||
tu: string[]; | ||
we: string[]; | ||
th: string[]; | ||
fr: string[]; | ||
sa: string[]; | ||
ph: string[]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const isInRange = ([startDate, endDate]: [Date, Date], date: Date) => | ||
date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add the
feature.countryCode
here? would it help to find the holidays?You can get it by
useFeatureContext()
in<OpeningHoursRenderer>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I though it wouldn't work without a state but I just checked and it works quite good with only the country and coordinates (which are used for sunsets)