import { DateTime } from "luxon"; const MONTHS = { January: 0, February: 1, March: 2, April: 3, May: 4, June: 5, July: 6, August: 7, September: 8, October: 9, November: 10, December: 11 }; /* sorts the months by their date heading, for example: { "February 2022": [ *array of dates for this month* ], "January 2022": [ *array of dates for this month* ], } */ export function sortSplittedPostsByMonth(postsByMonth: any) { const sortedPostsByMonth: { [key: string]: Array<{}> } = {}; Object.keys(postsByMonth) .sort((a, b) => { const [aMonth, aYear] = a.split(" "); const [bMonth, bYear] = b.split(" "); if (aYear < bYear) { return 1; } else if (aYear > bYear) { return -1; } else if ( MONTHS[aMonth as keyof typeof MONTHS] < MONTHS[bMonth as keyof typeof MONTHS] ) { return 1; } else if ( MONTHS[aMonth as keyof typeof MONTHS] > MONTHS[bMonth as keyof typeof MONTHS] ) { return -1; } return 0; }) .forEach((key) => { sortedPostsByMonth[key] = [ ...postsByMonth[key].sort((a: any, b: any) => { const aDate = new Date(a.date); const bDate = new Date(b.date); if (aDate < bDate) { return 1; } else if (aDate > bDate) { return -1; } return 0; }) ]; }); return sortedPostsByMonth; } export function convertForDaylightSavings(utcIsoString: string) { const utcDate = DateTime.fromISO(utcIsoString, { zone: "utc" }); return utcDate.toLocal(); }