This commit is contained in:
Pieter Vander Vennet 2023-10-18 02:08:49 +02:00
parent aa5422b34e
commit 65250d77be
2 changed files with 64 additions and 19 deletions

View file

@ -289,6 +289,14 @@ export class OH {
* rules[0].startHour // => 11 * rules[0].startHour // => 11
* rules[3].endHour // => 19 * rules[3].endHour // => 19
* *
* const rules = OH.ParseRule("Mo 20:00-02:00");
* rules.length // => 2
* rules[0].weekday // => 0
* rules[0].startHour // => 20
* rules[0].endHour // => 0
* rules[1].weekday // => 1
* rules[1].startHour // => 0
* rules[1].endHour // => 2
*/ */
public static ParseRule(rule: string): OpeningHour[] { public static ParseRule(rule: string): OpeningHour[] {
try { try {
@ -560,6 +568,9 @@ export class OH {
return values return values
} }
/**
* OH.parseHHMM("12:30") // => {hours: 12, minutes: 30}
*/
private static parseHHMM(hhmm: string): { hours: number; minutes: number } { private static parseHHMM(hhmm: string): { hours: number; minutes: number } {
if (hhmm === undefined || hhmm == null) { if (hhmm === undefined || hhmm == null) {
return null return null
@ -575,6 +586,10 @@ export class OH {
return hm return hm
} }
/**
* OH.ParseHhmmRanges("20:00-22:15") // => [{startHour: 20, startMinutes: 0, endHour: 22, endMinutes: 15}]
* OH.ParseHhmmRanges("20:00-02:15") // => [{startHour: 20, startMinutes: 0, endHour: 2, endMinutes: 15}]
*/
private static ParseHhmmRanges(hhmms: string): { private static ParseHhmmRanges(hhmms: string): {
startHour: number startHour: number
startMinutes: number startMinutes: number
@ -641,13 +656,25 @@ export class OH {
endHour: number endHour: number
endMinutes: number endMinutes: number
}[] }[]
) { ): {
weekday: number
startHour: number
startMinutes: number
endHour: number
endMinutes: number
}[] {
if ((weekdays ?? null) == null || (timeranges ?? null) == null) { if ((weekdays ?? null) == null || (timeranges ?? null) == null) {
return null return null
} }
const ohs: OpeningHour[] = [] const ohs: OpeningHour[] = []
for (const timerange of timeranges) { for (const timerange of timeranges) {
const overMidnight =
!(timerange.endHour === 0 && timerange.endMinutes === 0) &&
(timerange.endHour < timerange.startHour ||
(timerange.endHour == timerange.startHour &&
timerange.endMinutes < timerange.startMinutes))
for (const weekday of weekdays) { for (const weekday of weekdays) {
if (!overMidnight) {
ohs.push({ ohs.push({
weekday: weekday, weekday: weekday,
startHour: timerange.startHour, startHour: timerange.startHour,
@ -655,10 +682,27 @@ export class OH {
endHour: timerange.endHour, endHour: timerange.endHour,
endMinutes: timerange.endMinutes, endMinutes: timerange.endMinutes,
}) })
} else {
ohs.push({
weekday: weekday,
startHour: timerange.startHour,
startMinutes: timerange.startMinutes,
endHour: 0,
endMinutes: 0,
})
ohs.push({
weekday: (weekday + 1) % 7,
startHour: 0,
startMinutes: 0,
endHour: timerange.endHour,
endMinutes: timerange.endMinutes,
})
}
} }
} }
return ohs return ohs
} }
public static getMondayBefore(d) { public static getMondayBefore(d) {
d = new Date(d) d = new Date(d)
const day = d.getDay() const day = d.getDay()

View file

@ -82,6 +82,7 @@ export default class OpeningHoursInput extends InputElement<string> {
const rules = valueWithoutPrefix.data?.split(";") ?? [] const rules = valueWithoutPrefix.data?.split(";") ?? []
for (const rule of rules) { for (const rule of rules) {
if (OH.ParsePHRule(rule) !== null) { if (OH.ParsePHRule(rule) !== null) {
// We found the rule containing the public holiday information
ph = rule ph = rule
break break
} }