feat: improve bank card expenses summary
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
type Money = { currency: string; value: string };
|
||||
type FormatMoneyOptions = {
|
||||
withZeroCents?: boolean;
|
||||
};
|
||||
|
||||
function formatDecimal(value: string): string {
|
||||
const [integer, fraction] = value.split(".");
|
||||
@@ -10,9 +13,21 @@ function formatDecimal(value: string): string {
|
||||
return `${sign}${grouped}${fraction ? `,${fraction}` : ""}`;
|
||||
}
|
||||
|
||||
function formatMoney(money: Money | null): string {
|
||||
function formatMoney(
|
||||
money: Money | null,
|
||||
{ withZeroCents = false }: FormatMoneyOptions = {},
|
||||
): string {
|
||||
if (!money) return "—";
|
||||
return `${formatDecimal(money.value)} ${money.currency === "RUB" ? "₽" : money.currency}`;
|
||||
const [integerPart, fractionPart = ""] = money.value.split(".");
|
||||
const normalizedFraction =
|
||||
withZeroCents && !fractionPart
|
||||
? "00"
|
||||
: withZeroCents && fractionPart.length === 1
|
||||
? `${fractionPart}0`
|
||||
: fractionPart;
|
||||
return `${formatDecimal(
|
||||
`${integerPart}${normalizedFraction ? `.${normalizedFraction}` : ""}`,
|
||||
)} ${money.currency === "RUB" ? "₽" : money.currency}`;
|
||||
}
|
||||
|
||||
function formatDate(value: string): string {
|
||||
|
||||
Reference in New Issue
Block a user