Spaces:
Running on Zero

now / apps /web /src /lib /format.ts
souliya_pps
fix: merge env+DB AI keys so a stale env key can't block valid keys
2eef5d6
Raw History Blame Contribute Delete
2.12 kB
export function formatMoney(value: number | undefined | null, currency = "₭"): string {
const n = Number(value || 0);
const formatted = Math.round(n).toLocaleString("en-US");
return `${currency}${formatted}`;
}
export function formatCompactNumber(value: number | undefined | null): string {
const n = Number(value || 0);
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;
if (n >= 1_000) return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}K`;
return String(n);
}
export function formatDate(value: string | undefined | null): string {
if (!value) return "-";
const d = new Date(value);
if (Number.isNaN(d.getTime())) return "-";
return d.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
}
export function formatDateTime(value: string | undefined | null): string {
if (!value) return "-";
const d = new Date(value);
if (Number.isNaN(d.getTime())) return "-";
return d.toLocaleString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
});
}
export function initials(name: string | undefined | null): string {
if (!name) return "?";
return name
.split(/\s+/)
.filter(Boolean)
.slice(0, 2)
.map((p) => p[0]!.toUpperCase())
.join("");
}
export function timeAgo(value: string | undefined | null): string {
if (!value) return "";
const d = new Date(value);
if (Number.isNaN(d.getTime())) return "";
const secs = Math.max(0, Math.floor((Date.now() - d.getTime()) / 1000));
if (secs < 60) return "just now";
const mins = Math.floor(secs / 60);
if (mins < 60) return `${mins}m ago`;
const hrs = Math.floor(mins / 60);
if (hrs < 24) return `${hrs}h ago`;
const days = Math.floor(hrs / 24);
if (days < 30) return `${days}d ago`;
return d.toLocaleDateString("en-US", { month: "short", day: "numeric" });
}
export function cacheBust(url: string | undefined | null): string {
if (!url) return "";
const separator = url.includes("?") ? "&" : "?";
return `${url}${separator}t=${Date.now()}`;
}