/** * Turns opening hours as an editor types them into schema.org `openingHours` * strings, so the panel field can stay human text in the site's language: * * formatOpeningHours('Pn-Pt: 08:00 - 17:00, Sob: 8-14, Nd: nieczynne') * // → ['Mo-Fr 08:00-17:00', 'Sa 08:00-14:00'] * formatOpeningHours('Mon, Wed, Fri 9:30–17') // → ['Mo,We,Fr 09:30-17:00'] * formatOpeningHours('Mo–Fr 8–16 Uhr; Sa 9–12') // → ['Mo-Fr 08:00-16:00', 'Sa 09:00-12:00'] * formatOpeningHours('całodobowo') // → ['Mo-Su 00:00-23:59'] * * Understands Polish, English and German day names and abbreviations (with or * without diacritics and dots), ranges (`-`, `–`, `do`, `to`, `bis`), lists, * `8`, `8:00`, `8.00`, closed days (skipped) and "24/7". Strings already in * schema.org form pass through. Accepts one string or an array of lines. Anything * it can't read is dropped rather than emitted invalid — check the result. */ type Day = 'Fr' | 'Mo' | 'Sa' | 'Su' | 'Th' | 'Tu' | 'We' // Normalized (lowercase, no diacritics, no dots) day words → schema.org day. // "so" is German Sonntag; Polish Saturday is "sob"/"sb"/"sobota". const DAYS: Record = { // pl cz: 'Th', czw: 'Th', czwartek: 'Th', nd: 'Su', ndz: 'Su', niedz: 'Su', niedziela: 'Su', piat: 'Fr', piatek: 'Fr', pn: 'Mo', pon: 'Mo', poniedzialek: 'Mo', pt: 'Fr', sb: 'Sa', sob: 'Sa', sobota: 'Sa', sr: 'We', sro: 'We', sroda: 'We', wt: 'Tu', wto: 'Tu', wtorek: 'Tu', // en fr: 'Fr', fri: 'Fr', friday: 'Fr', mo: 'Mo', mon: 'Mo', monday: 'Mo', sa: 'Sa', sat: 'Sa', saturday: 'Sa', su: 'Su', sun: 'Su', sunday: 'Su', th: 'Th', thu: 'Th', thur: 'Th', thurs: 'Th', thursday: 'Th', tu: 'Tu', tue: 'Tu', tues: 'Tu', tuesday: 'Tu', we: 'We', wed: 'We', wednesday: 'We', // de di: 'Tu', dienstag: 'Tu', do: 'Th', donnerstag: 'Th', freitag: 'Fr', mi: 'We', mittwoch: 'We', montag: 'Mo', samstag: 'Sa', so: 'Su', sonntag: 'Su', } const CLOSED = /\b(?:nieczynne|nieczynny|zamkniete|zamkniety|closed|geschlossen|ruhetag)\b/g const ALWAYS = /\b(?:calodobowo|cala doba|cala dobe|24\s*\/\s*7|24h|non-?stop|rund um die uhr|open 24 hours)\b/ const TIME = /(?\d{1,2})(?:[:.](?\d{2}))?\s*(?:-|do|to|bis)\s*(?\d{1,2})(?:[:.](?\d{2}))?/g const SCHEMA = /^(?:Mo|Tu|We|Th|Fr|Sa|Su)(?:[-,](?:Mo|Tu|We|Th|Fr|Sa|Su))*(?:\s+\d{2}:\d{2}-\d{2}:\d{2})?$/ function normalize(text: string): string { return text .toLowerCase() .normalize('NFD') .replace(/\p{M}/gu, '') // drop combining accents (ą → a) .replace(/ł/g, 'l') .replace(/[–—−]/g, '-') .replace(/\./g, (m, i: number, s: string) => (/\d/.test(s[i - 1] ?? '') && /\d/.test(s[i + 1] ?? '') ? m : ' ')) .replace(/\buhr\b/g, ' ') } function hhmm(h: string, m?: string): null | string { const hour = Number(h) const minute = m ? Number(m) : 0 if (hour > 24 || minute > 59) {return null} return `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}` } /** Day words in a fragment → "Mo-Fr" (range) or "Mo,We,Fr" (list). */ function parseDays(fragment: string): null | string { const days: Day[] = [] let range = false for (const w of fragment.match(/[a-z]+|-/g) ?? []) { // "do" is both Polish "to" and German Donnerstag: a range word only right after the first day. const rangeWord = w === '-' || w === 'to' || w === 'bis' || (w === 'do' && days.length === 1) if (rangeWord) { if (days.length === 1) {range = true} continue } const day = DAYS[w] if (day) {days.push(day)} } if (days.length === 0) {return null} if (range && days.length === 2) {return `${days[0]}-${days[1]}`} return [...new Set(days)].join(',') } export function formatOpeningHours(input: null | readonly string[] | string | undefined): string[] { if (!input) {return []} const lines = (Array.isArray(input) ? input : [input as string]).map((l) => l.trim()).filter(Boolean) if (lines.every((l) => SCHEMA.test(l))) {return [...lines]} const out: string[] = [] for (const raw of lines) { if (SCHEMA.test(raw)) { out.push(raw) continue } const text = normalize(raw) if (ALWAYS.test(text)) { out.push('Mo-Su 00:00-23:59') continue } let last = 0 for (const m of text.matchAll(TIME)) { // The day spec is what precedes this time range — after the previous // range, the last line break / semicolon, and any "closed" day. let spec = text.slice(last, m.index) last = (m.index ?? 0) + m[0].length spec = spec.split(/[;\n]/).pop() ?? '' const closedAt = [...spec.matchAll(CLOSED)].pop() if (closedAt) {spec = spec.slice((closedAt.index ?? 0) + closedAt[0].length)} const days = parseDays(spec) const g = m.groups ?? {} const from = hhmm(g.fromH, g.fromM) const to = hhmm(g.toH, g.toM) if (!days || !from || !to) {continue} out.push(`${days} ${from}-${to === '24:00' ? '23:59' : to}`) } } return out }