SPB Git forge

spb/polyllm

Public
15commits 1branches 0releases
2.2 MBsize
maindefault branch
13 days agolast push
TypeScript 97.4% SQL 1% JavaScript 0.9% CSS 0.6%
2.7 KB · 72 lines typescript
Raw Blame History
1import { clsx, type ClassValue } from "clsx";2import { twMerge } from "tailwind-merge";34export function cn(...inputs: ClassValue[]) {5  return twMerge(clsx(inputs));6}78export function formatNumber(n: number | null | undefined, opts: Intl.NumberFormatOptions = {}): string {9  if (n === null || n === undefined || Number.isNaN(n)) return "—";10  return new Intl.NumberFormat("en-US", { maximumFractionDigits: 0, ...opts }).format(n);11}1213export function formatCompact(n: number | null | undefined): string {14  if (n === null || n === undefined) return "—";15  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n % 1_000_000 === 0 ? 0 : 1)}M`;16  if (n >= 1_000) return `${(n / 1_000).toFixed(n % 1_000 === 0 ? 0 : 0)}K`;17  return String(n);18}1920export function formatTokens(n: number | null | undefined): string {21  if (n === null || n === undefined) return "—";22  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2).replace(/\.?0+$/, "")}M`;23  if (n >= 10_000) return `${(n / 1_000).toFixed(1).replace(/\.?0+$/, "")}K`;24  return new Intl.NumberFormat("en-US").format(n);25}2627export function formatUsd(n: number | null | undefined, opts: { precise?: boolean } = {}): string {28  if (n === null || n === undefined || Number.isNaN(n)) return "—";29  if (n === 0) return "$0.00";30  if (n < 0.01 || opts.precise) return `$${n.toFixed(n < 0.001 ? 5 : 4)}`;31  return `$${n.toFixed(2)}`;32}3334export function formatMs(ms: number | null | undefined): string {35  if (ms === null || ms === undefined) return "—";36  if (ms < 1000) return `${Math.round(ms)} ms`;37  return `${(ms / 1000).toFixed(ms < 10_000 ? 2 : 1)} s`;38}3940export function formatRelative(date: Date | string | null | undefined): string {41  if (!date) return "—";42  const d = typeof date === "string" ? new Date(date) : date;43  const diff = Date.now() - d.getTime();44  const s = Math.round(diff / 1000);45  if (s < 45) return "just now";46  const m = Math.round(s / 60);47  if (m < 60) return `${m} min ago`;48  const h = Math.round(m / 60);49  if (h < 24) return `${h} h ago`;50  const days = Math.round(h / 24);51  if (days < 7) return `${days} d ago`;52  return d.toLocaleDateString("en-US", { month: "short", day: "numeric", year: d.getFullYear() === new Date().getFullYear() ? undefined : "numeric" });53}5455export function truncate(s: string, n: number): string {56  return s.length > n ? `${s.slice(0, n - 1)}…` : s;57}5859export function sleep(ms: number, signal?: AbortSignal): Promise<void> {60  return new Promise((resolve, reject) => {61    const t = setTimeout(resolve, ms);62    signal?.addEventListener(63      "abort",64      () => {65        clearTimeout(t);66        reject(new DOMException("Aborted", "AbortError"));67      },68      { once: true },69    );70  });71}72