SPB Git

Repository Public MIT

format.ts 1.1 KB 24 lines typescript

1export function bytes(n: number): string {2  if (n < 1024) return `${n} B`;3  if (n < 1024 * 1024) return `${(n / 1024).toFixed(n < 10240 ? 1 : 0)} kB`;4  return `${(n / 1024 / 1024).toFixed(1)} MB`;5}6export function date(d: Date | string | null | undefined): string {7  if (!d) return "—";8  return new Date(d).toLocaleDateString("en-CA", { year: "numeric", month: "short", day: "numeric", timeZone: "UTC" });9}10export function dateTime(d: Date | string | null | undefined): string {11  if (!d) return "—";12  return new Date(d).toISOString().replace("T", " ").slice(0, 16) + " UTC";13}14export function relative(d: Date | string | null | undefined): string {15  if (!d) return "—";16  const s = (Date.now() - new Date(d).getTime()) / 1000;17  if (s < 60) return "just now";18  if (s < 3600) return `${Math.floor(s / 60)} min ago`;19  if (s < 86400) return `${Math.floor(s / 3600)} h ago`;20  if (s < 86400 * 30) return `${Math.floor(s / 86400)} d ago`;21  return date(d);22}23export const plural = (n: number, one: string, many = one + "s") => `${n.toLocaleString("en-CA")} ${n === 1 ? one : many}`;24
Files main
Menu

Theme