50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
|
|
import { type ClassValue, clsx } from "clsx";
|
||
|
|
import { twMerge } from "tailwind-merge";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Utility for merging tailwind classes safely.
|
||
|
|
*/
|
||
|
|
export function cn(...inputs: ClassValue[]) {
|
||
|
|
return twMerge(clsx(inputs));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Formats a number as a currency string.
|
||
|
|
*/
|
||
|
|
export function formatCurrency(value: number, decimals: number = 2) {
|
||
|
|
return new Intl.NumberFormat("en-US", {
|
||
|
|
style: "currency",
|
||
|
|
currency: "USD",
|
||
|
|
minimumFractionDigits: decimals,
|
||
|
|
maximumFractionDigits: decimals,
|
||
|
|
}).format(value);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Formats a percentage from midpoint.
|
||
|
|
*/
|
||
|
|
export function formatPercent(value: number) {
|
||
|
|
return (value * 100).toFixed(1) + "%";
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Converts Fahrenheit to Celsius.
|
||
|
|
*/
|
||
|
|
export function fToC(f: number) {
|
||
|
|
return ((f - 32) * 5) / 9;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Formats a date/time string to HH:MM:SS.
|
||
|
|
*/
|
||
|
|
export function formatTime(date: Date | string) {
|
||
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
||
|
|
if (isNaN(d.getTime())) return String(date);
|
||
|
|
return d.toLocaleTimeString("zh-CN", {
|
||
|
|
hour12: false,
|
||
|
|
hour: "2-digit",
|
||
|
|
minute: "2-digit",
|
||
|
|
second: "2-digit",
|
||
|
|
});
|
||
|
|
}
|