forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

weekly-plan.ts33 lines, 1.4 KiBHistoryRawDownload
1import type { Locale } from "@/i18n/messages";
2import type { WeeklySlot } from "@/api/client";
3
4/** 540 → "09:00". */
5export function minutesToHHmm(minutes: number): string {
6 const pad = (n: number) => String(n).padStart(2, "0");
7 return `${pad(Math.floor(minutes / 60))}:${pad(minutes % 60)}`;
8}
9
10/** "09:00" → 540, or null when the text is not a valid HH:mm. */
11export function hhmmToMinutes(text: string): number | null {
12 const match = text.trim().match(/^(\d{1,2}):(\d{2})$/);
13 if (!match) return null;
14 const hours = Number(match[1]);
15 const minutes = Number(match[2]);
16 if (hours > 23 || minutes > 59) return null;
17 return hours * 60 + minutes;
18}
19
20/** The backend's weekdays: 1 = Monday .. 7 = Sunday. */
21export const WEEKDAYS = [1, 2, 3, 4, 5, 6, 7] as const;
22
23/** Localized weekday name for a 1 (Monday) .. 7 (Sunday) weekday. */
24export function weekdayLabel(weekday: number, locale: Locale): string {
25 // 2024-01-01 was a Monday, so day `weekday` of that week names it.
26 const date = new Date(Date.UTC(2024, 0, weekday));
27 return new Intl.DateTimeFormat(locale === "tr" ? "tr-TR" : "en-GB", { weekday: "long", timeZone: "UTC" }).format(date);
28}
29
30/** Weekday first, then start time — the order the backend lists a plan in. */
31export function sortSlots(slots: WeeklySlot[]): WeeklySlot[] {
32 return slots.slice().sort((a, b) => a.weekday - b.weekday || a.starts_at - b.starts_at);
33}