export function cn(...xs: Array): string { return xs.filter(Boolean).join(" "); } export function domainOf(url: string): string { try { return new URL(url).hostname.replace(/^www\./, ""); } catch { return url; } } export function faviconOf(url: string): string { return `https://icons.duckduckgo.com/ip3/${domainOf(url)}.ico`; } export function sleep(ms: number): Promise { return new Promise((r) => setTimeout(r, ms)); } export function clamp(n: number, min: number, max: number): number { return Math.min(max, Math.max(min, n)); } const ENTITIES: Record = { "&": "&", "<": "<", ">": ">", """: '"', "'": "'", "'": "'", "/": "/", "'": "'", " ": " ", }; export function decodeHtmlEntities(input: string): string { if (!input) return input; return input.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (m, entity) => { if (entity[0] === "#") { const code = entity[1] === "x" || entity[1] === "X" ? parseInt(entity.slice(2), 16) : parseInt(entity.slice(1), 10); if (Number.isFinite(code)) { try { return String.fromCodePoint(code); } catch { return m; } } return m; } return ENTITIES[`&${entity};`] ?? m; }); } export function timeAgo(input: Date | string | number): string { const date = input instanceof Date ? input : new Date(input); const seconds = Math.max(0, Math.floor((Date.now() - date.getTime()) / 1000)); if (seconds < 45) return "just now"; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `${minutes} min ago`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours} hr ago`; const days = Math.floor(hours / 24); if (days < 7) return `${days} day${days === 1 ? "" : "s"} ago`; const weeks = Math.floor(days / 7); if (weeks < 5) return `${weeks} wk ago`; const months = Math.floor(days / 30); if (months < 12) return `${months} mo ago`; const years = Math.floor(days / 365); return `${years} yr ago`; } /** * Deterministic date label. Pinned to UTC so the server render and the browser * hydration always produce the same text, whatever the visitor's time zone. */ export function formatDate(input: Date | string | number): string { const date = input instanceof Date ? input : new Date(input); if (Number.isNaN(date.getTime())) return ""; return date.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric", timeZone: "UTC", }); } export function formatCompact(n: number): string { if (n >= 1_000_000_000) return `${(n / 1_000_000_000).toFixed(1)}B`; if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`; if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`; return String(n); } export function isHttpUrl(value: unknown): value is string { if (typeof value !== "string" || !value) return false; try { const u = new URL(value); return u.protocol === "http:" || u.protocol === "https:"; } catch { return false; } } const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; /** Guards route params / body ids so malformed values never reach a uuid column. */ export function isUuid(value: unknown): value is string { return typeof value === "string" && UUID_RE.test(value); } /** * Only allow same-site relative paths as post-login redirect targets * (blocks `//evil.com`, `/\evil.com`, absolute URLs and auth-page loops). */ export function safeInternalPath(value: string | null | undefined, fallback = "/dashboard"): string { if (!value || value.length > 2000) return fallback; if (!value.startsWith("/") || value.startsWith("//") || value.startsWith("/\\")) return fallback; if (/[\u0000-\u001f\\]/.test(value)) return fallback; if (/^\/(login|signup)(\/|\?|$)/.test(value)) return fallback; return value; } export const COLLECTION_COLORS = [ "#4285f4", "#ea4335", "#fbbc05", "#34a853", "#a142f4", "#ff6d00", "#00acc1", "#ec407a", ]; export function truncate(s: string, max: number): string { if (s.length <= max) return s; return s.slice(0, max - 1).trimEnd() + "…"; }