From 10e49eae380249353e690909fc629815b486cf7a Mon Sep 17 00:00:00 2001 From: Eric Matala de Mazza Date: Thu, 27 Apr 2023 04:00:34 +0200 Subject: [PATCH] feat: add trash utils --- src/utils.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index e69de29..ea58de2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -0,0 +1,25 @@ +import { trash } from "./data"; +import { closestTo, format } from "date-fns"; + +const trashMap: Record = { + 0: "Papiertonne", + 1: "Restabfalltonne", + 2: "Biotonne", + 3: "Wertstofftonne", + 4: "Sperrgut, Grünabfall", +}; + +const parseDate = (date: string) => { + const [year, month, day] = date.split("-").map(Number); + return new Date(year, month - 1, day); +}; + +export const getTrash = () => { + const dates = Object.keys(trash) + .map(parseDate) + .filter((d) => d > new Date()); + const closest = closestTo(new Date(), dates); + if (!closest) return []; + const date = format(closest, "yyyy-MM-dd"); + return trash[date].map((t) => trashMap[t]); +};