Skip to content

Commit

Permalink
problem: every month is januaru
Browse files Browse the repository at this point in the history
solution: fix whitespace bug
  • Loading branch information
kluvin committed Jan 21, 2025
1 parent b4232b8 commit 34bd386
Showing 1 changed file with 64 additions and 30 deletions.
94 changes: 64 additions & 30 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,74 @@ const monthNames = [
'Desember'
] as const;
// Helper function to parse Norwegian date format
function parseNorwegianDate(dateStr: string) {
type MonthMap = Record<string, number>;
/**
* 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<string, number> = {
'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()];
Expand All @@ -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");
Expand Down

0 comments on commit 34bd386

Please sign in to comment.