initial commit
This commit is contained in:
41
src/lib/format.ts
Normal file
41
src/lib/format.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { format as dfFormat } from "date-fns";
|
||||
import { de } from "date-fns/locale";
|
||||
|
||||
export const eur = new Intl.NumberFormat("de-DE", {
|
||||
style: "currency",
|
||||
currency: "EUR",
|
||||
});
|
||||
|
||||
export const pct = new Intl.NumberFormat("de-DE", {
|
||||
style: "percent",
|
||||
minimumFractionDigits: 1,
|
||||
maximumFractionDigits: 1,
|
||||
});
|
||||
|
||||
export function formatDate(date: Date | string | undefined): string {
|
||||
if (!date) return "–";
|
||||
const d = typeof date === "string" ? new Date(date) : date;
|
||||
return dfFormat(d, "dd.MM.yyyy", { locale: de });
|
||||
}
|
||||
|
||||
export function formatMonth(monthKey: string | undefined): string {
|
||||
if (!monthKey) return "–";
|
||||
const [year, month] = monthKey.split("-");
|
||||
return `${month}.${year}`;
|
||||
}
|
||||
|
||||
export function formatAmount(amount: number): string {
|
||||
if (amount === 0) return eur.format(0);
|
||||
return eur.format(amount);
|
||||
}
|
||||
|
||||
export function amountClass(amount: number): string {
|
||||
if (amount < 0) return "text-red-600 dark:text-red-400";
|
||||
if (amount > 0) return "text-emerald-600 dark:text-emerald-400";
|
||||
return "";
|
||||
}
|
||||
|
||||
export function dashIfZero(value: number, formatter: (n: number) => string = (n) => String(n)): string {
|
||||
if (value === 0) return "–";
|
||||
return formatter(value);
|
||||
}
|
||||
Reference in New Issue
Block a user