SPB Git
1.1 KB · 13 lines typescript
1import { clsx, type ClassValue } from "clsx";2import { twMerge } from "tailwind-merge";3export const cn = (...i: ClassValue[]) => twMerge(clsx(i));4export const fmtDate = (d: Date | string | null | undefined, opts: Intl.DateTimeFormatOptions = { dateStyle: "medium" }) => (d ? new Intl.DateTimeFormat("en-CA", opts).format(new Date(d)) : "");5export function timeAgo(d: Date | string) {6  const s = (Date.now() - new Date(d).getTime()) / 1000;7  if (s < 60) return "just now"; if (s < 3600) return `${Math.floor(s / 60)} min ago`; if (s < 86400) return `${Math.floor(s / 3600)} h ago`;8  if (s < 86400 * 7) return `${Math.floor(s / 86400)} d ago`; return fmtDate(d);9}10export const money = (n: number | null | undefined, digits = 4) => (n == null ? "–" : `$${n.toFixed(n < 0.01 ? digits : 3)}`);11export const slugify = (s: string) => s.toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g, "").replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "");12export function debounce<T extends (...a: any[]) => void>(fn: T, ms: number) { let t: any; return (...a: Parameters<T>) => { clearTimeout(t); t = setTimeout(() => fn(...a), ms); }; }13
Files · main
Menu

Theme