RJBULK-APP/lib/utils.ts

46 lines
1.4 KiB
TypeScript
Raw Normal View History

import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function formatCurrency(value: number | null | undefined): string {
if (value == null) return "R0.00";
return new Intl.NumberFormat("en-ZA", {
style: "currency",
currency: "ZAR",
minimumFractionDigits: 2,
}).format(value);
}
export function formatNumber(value: number | null | undefined, decimals = 2): string {
if (value == null) return "0";
return new Intl.NumberFormat("en-ZA", {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals,
}).format(value);
}
export function formatDate(value: string | Date | null | undefined): string {
if (!value) return "";
const d = typeof value === "string" ? new Date(value) : value;
if (isNaN(d.getTime())) return "";
return new Intl.DateTimeFormat("en-ZA", {
year: "numeric",
month: "short",
day: "2-digit",
}).format(d);
}
export function formatDateInput(value: string | Date | null | undefined): string {
if (!value) return "";
const d = typeof value === "string" ? new Date(value) : value;
if (isNaN(d.getTime())) return "";
// YYYY-MM-DD for <input type="date">
const yyyy = d.getFullYear();
const mm = String(d.getMonth() + 1).padStart(2, "0");
const dd = String(d.getDate()).padStart(2, "0");
return `${yyyy}-${mm}-${dd}`;
}