From 34bd38610decf99e3f09f704b681364f7f0e8147 Mon Sep 17 00:00:00 2001 From: Martin Kleiven Date: Tue, 21 Jan 2025 20:45:07 +0100 Subject: [PATCH] problem: every month is januaru solution: fix whitespace bug --- src/pages/index.astro | 94 +++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 30 deletions(-) diff --git a/src/pages/index.astro b/src/pages/index.astro index 5d47e0b..02d8453 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -20,40 +20,74 @@ const monthNames = [ 'Desember' ] as const; -// Helper function to parse Norwegian date format -function parseNorwegianDate(dateStr: string) { +type MonthMap = Record; + +/** + * Be hereby warned, a lot of data cleaning is done here that should not be done here + * We're simply reading the input data, filtering, then grouping by *cleaned* dates + * then we write to loops of info, the dates, and for the inner loop we write events. + * **/ +const NORWEGIAN_MONTHS: MonthMap = { + 'jan': 0, 'januar': 0, + 'feb': 1, 'februar': 1, + 'mar': 2, 'mars': 2, + 'apr': 3, 'april': 3, + 'mai': 4, + 'jun': 5, 'juni': 5, + 'jul': 6, 'juli': 6, + 'aug': 7, 'august': 7, + 'sep': 8, 'september': 8, + 'okt': 9, 'oktober': 9, + 'nov': 10, 'november': 10, + 'des': 11, 'desember': 11 +} as const; + +const cleanDateString = (dateStr: string): string => { + return dateStr.toLowerCase().trim(); +}; + +const isMonthOnly = (dateStr: string): boolean => { + return NORWEGIAN_MONTHS[dateStr] !== undefined; +}; + +const createDateFromMonthOnly = (month: string): Date => { + return new Date(2024, NORWEGIAN_MONTHS[month], 1); +}; + +const parseDateParts = (dateStr: string): { day: number; month: string } | null => { + const parts = dateStr.split('.'); + const day = parseInt(parts[0]); + const month = parts[1]?.trim(); + + if (!month || !(month in NORWEGIAN_MONTHS)) { + return null; + } + + return { day, month }; +}; + +const parseNorwegianDate = (dateStr: string): Date => { if (!dateStr) return new Date(0); - - const monthMap: Record = { - 'jan': 0, 'januar': 0, - 'feb': 1, 'februar': 1, - 'mar': 2, 'mars': 2, - 'apr': 3, 'april': 3, - 'mai': 4, - 'jun': 5, 'juni': 5, - 'jul': 6, 'juli': 6, - 'aug': 7, 'august': 7, - 'sep': 8, 'september': 8, - 'okt': 9, 'oktober': 9, - 'nov': 10, 'november': 10, - 'des': 11, 'desember': 11 - }; - - // Handle full month names like "Januar" - const monthOnly = dateStr.toLowerCase().trim(); - if (monthMap[monthOnly] !== undefined) { - return new Date(2024, monthMap[monthOnly], 1); + + const cleanStr = cleanDateString(dateStr); + + // Handle full month names like "Februar" + if (isMonthOnly(cleanStr)) { + return createDateFromMonthOnly(cleanStr); } - // Handle dates like "27.jan." - const [day, month] = dateStr.toLowerCase().split(/\s*\.\s*/); - if (!month) return new Date(0); - - return new Date(2024, monthMap[month] || 0, parseInt(day) || 1); -} + // Handle dates like "14.februar" or "14.februar " + const dateParts = parseDateParts(cleanStr); + if (!dateParts) { + return new Date(0); + } -function formatNorwegianDate(dateStr: string) { + return new Date(2025, NORWEGIAN_MONTHS[dateParts.month], dateParts.day); +}; + +const formatNorwegianDate = (dateStr: string): string => { if (dateStr === 'Udatert') return dateStr; + const date = parseNorwegianDate(dateStr); const day = date.getDate(); const month = monthNames[date.getMonth()]; @@ -64,7 +98,7 @@ function formatNorwegianDate(dateStr: string) { } return `${day}. ${month}`; -} +}; type Event = typeof events[number]; const publishedEvents = events.filter(event => event["Skal ut"] !== "FALSE");