File size: 2,115 Bytes
2eef5d6 | 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 | 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()}`;
}
|