Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/view-models/src/money.vm.ts
T

25 lines
916 B
TypeScript

import { defaultLocale, formatCurrencyAmount, type CurrencyAmount } from "@hivegrid/i18n";
// HiveGrid v1 operational amounts are EUR; punctuation and currency placement
// follow the request/viewer locale supplied by the server-side VM builder.
const EURO_PRICE_OPTIONS = {
currency: "EUR",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
} as const;
function isCurrencyAmount(value: unknown): value is CurrencyAmount {
return (
typeof value === "number" ||
typeof value === "bigint" ||
typeof value === "string" ||
(typeof value === "object" && value !== null && "toString" in value && typeof value.toString === "function")
);
}
export function formatEuroAmount(value: unknown, locale: string = defaultLocale): string | null {
if (value == null) return null;
if (!isCurrencyAmount(value)) return null;
return formatCurrencyAmount(value, { ...EURO_PRICE_OPTIONS, locale });
}