import { STAR_SCALE } from "./config"; // HTML-escape a value for safe interpolation into template strings. export const esc = (s: unknown = ""): string => String(s).replace(/&/g, "&").replace(//g, ">"); export const fmtDate = (d: Date): string => d.toLocaleDateString("en-GB", { year: "numeric", month: "short", day: "numeric" }); export const cleanTitle = (s = ""): string => s.replace(/\s*-\s*-\s*/g, " – ").replace(/\s{2,}/g, " ").trim(); export const cap = (s = ""): string => s.charAt(0).toUpperCase() + s.slice(1); export const truncate = (s = "", n = 80): string => s.length > n ? s.slice(0, n).trimEnd() + "…" : s; export const dayKey = (d: Date): string => `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`; export const sameDay = (a: Date, b: Date): boolean => dayKey(a) === dayKey(b); export const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const MONTHS_FULL = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; const WEEKDAYS_FULL = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; // 1 → "1st", 2 → "2nd", 25 → "25th", … const ordinal = (n: number): string => { const suffixes = ["th", "st", "nd", "rd"]; const v = n % 100; return `${n}${suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]}`; }; // Compact day-separator label (month/year come from the separators above it), // e.g. "25 Jun" — and its long form revealed on hover, "Saturday, June 25th 2026". export const fmtDay = (d: Date): string => `${d.getDate()} ${MONTHS[d.getMonth()]}`; export const fmtDayFull = (d: Date): string => `${WEEKDAYS_FULL[d.getDay()]}, ${MONTHS_FULL[d.getMonth()]} ${ordinal(d.getDate())} ${d.getFullYear()}`; // Narrowing helper for `.filter(notNull)` (keeps arrays strongly typed). export const notNull = (x: T | null | undefined): x is T => x != null; // Accent- and case-insensitive normaliser, so a search for "cafe" finds "café". export const norm = (s = ""): string => s.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase(); // Render a rating (given on its own `scaleMax`) as fractional stars. // A CSS overlay clips a gold ★★★★★ over a grey one, so halves are exact. export function starsHtml(rating: number | null | undefined, scaleMax = STAR_SCALE): string { if (rating == null) return ""; const outOfFive = Math.max(0, Math.min(STAR_SCALE, (rating / scaleMax) * STAR_SCALE)); const fillPct = (outOfFive / STAR_SCALE) * 100; const label = `${Number(outOfFive.toFixed(1))}/${STAR_SCALE}`; return ``; }