Spaces:
Running
Running
File size: 2,966 Bytes
c7bece7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
/** shadcn/ui's class merger: conditional classes with Tailwind conflict resolution. */
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
const MONEY = new Intl.NumberFormat("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
const COMPACT = new Intl.NumberFormat("en-US", {
notation: "compact",
maximumFractionDigits: 1,
});
export function formatMoney(value: number | null | undefined, currency?: string | null): string {
if (value === null || value === undefined) return "—";
return currency ? `${currency} ${MONEY.format(value)}` : MONEY.format(value);
}
export function formatCompact(value: number): string {
return COMPACT.format(value);
}
/** Sub-dollar figures need more precision than a currency formatter gives. */
export function formatCost(value: number): string {
if (value === 0) return "$0.00";
if (value < 0.01) return `$${value.toFixed(4)}`;
return `$${value.toFixed(2)}`;
}
export function formatLatency(ms: number | null | undefined): string {
if (ms === null || ms === undefined) return "—";
if (ms < 1000) return `${Math.round(ms)} ms`;
return `${(ms / 1000).toFixed(ms < 10_000 ? 2 : 1)} s`;
}
export function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
}
export function formatDate(value: string | null | undefined): string {
if (!value) return "—";
const parsed = new Date(value);
if (Number.isNaN(parsed.getTime())) return value;
return parsed.toLocaleDateString("en-GB", {
day: "2-digit",
month: "short",
year: "numeric",
});
}
export function formatTime(value: string | null | undefined): string {
if (!value) return "—";
const parsed = new Date(value);
if (Number.isNaN(parsed.getTime())) return value;
return parsed.toLocaleTimeString("en-GB", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
}
export function relativeTime(value: string): string {
const then = new Date(value).getTime();
if (Number.isNaN(then)) return value;
const seconds = Math.round((Date.now() - then) / 1000);
if (seconds < 60) return "just now";
if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
if (seconds < 86_400) return `${Math.floor(seconds / 3600)}h ago`;
return `${Math.floor(seconds / 86_400)}d ago`;
}
/** Turn `pipeline.extraction_succeeded` into `Extraction succeeded`. */
export function humaniseEvent(event: string): string {
const tail = event.includes(".") ? event.slice(event.indexOf(".") + 1) : event;
const words = tail.replace(/_/g, " ");
return words.charAt(0).toUpperCase() + words.slice(1);
}
export function titleCase(value: string): string {
return value.charAt(0).toUpperCase() + value.slice(1).toLowerCase();
}
|