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 {
@ -414,14 +422,14 @@ export class OH {
} }
/* /*
This function converts a number of ranges (generated by OpeningHours.js) into all the hours of day that a change occurs. This function converts a number of ranges (generated by OpeningHours.js) into all the hours of day that a change occurs.
E.g. E.g.
Monday, some business is opended from 9:00 till 17:00 Monday, some business is opended from 9:00 till 17:00
Tuesday from 9:30 till 18:00 Tuesday from 9:30 till 18:00
Wednesday from 9:30 till 12:30 Wednesday from 9:30 till 12:30
This function will extract all those moments of change and will return 9:00, 9:30, 12:30, 17:00 and 18:00 This function will extract all those moments of change and will return 9:00, 9:30, 12:30, 17:00 and 18:00
This list will be sorted This list will be sorted
*/ */
public static allChangeMoments( public static allChangeMoments(
ranges: { ranges: {
isOpen: boolean isOpen: boolean
@ -507,9 +515,9 @@ export class OH {
} }
/* /*
Calculates when the business is opened (or on holiday) between two dates. Calculates when the business is opened (or on holiday) between two dates.
Returns a matrix of ranges, where [0] is a list of ranges when it is opened on monday, [1] is a list of ranges for tuesday, ... Returns a matrix of ranges, where [0] is a list of ranges when it is opened on monday, [1] is a list of ranges for tuesday, ...
*/ */
public static GetRanges( public static GetRanges(
oh: any, oh: any,
from: Date, from: Date,
@ -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,24 +656,53 @@ 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) {
ohs.push({ if (!overMidnight) {
weekday: weekday, ohs.push({
startHour: timerange.startHour, weekday: weekday,
startMinutes: timerange.startMinutes, startHour: timerange.startHour,
endHour: timerange.endHour, startMinutes: timerange.startMinutes,
endMinutes: timerange.endMinutes, endHour: timerange.endHour,
}) 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
} }