feat: show return, cash and structure on account cards (#114)
This commit is contained in:
@@ -3,9 +3,17 @@ const CENTS_PRECISION = 2;
|
||||
|
||||
type FormatMoneyOptions = {
|
||||
withRoundedCents?: boolean;
|
||||
withSign?: boolean;
|
||||
withZeroCents?: boolean;
|
||||
};
|
||||
|
||||
const portfolioCategoryPresentation = {
|
||||
BOND: { color: "grape.6", label: "portfolio.bonds" },
|
||||
CASH: { color: "green.6", label: "portfolio.structure.cash" },
|
||||
OTHER: { color: "orange.6", label: "portfolio.structure.other" },
|
||||
STOCK: { color: "blue.6", label: "portfolio.stocks" },
|
||||
} as const;
|
||||
|
||||
type Money = { currency: string; value: string };
|
||||
|
||||
const dateFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
@@ -40,7 +48,11 @@ function formatMoney(
|
||||
money: Money
|
||||
| null
|
||||
| undefined,
|
||||
{ withRoundedCents = false, withZeroCents = false }: FormatMoneyOptions = {},
|
||||
{
|
||||
withRoundedCents = false,
|
||||
withSign = false,
|
||||
withZeroCents = false,
|
||||
}: FormatMoneyOptions = {},
|
||||
): string {
|
||||
if (!money) return "—";
|
||||
|
||||
@@ -62,8 +74,25 @@ function formatMoney(
|
||||
: integerPart;
|
||||
|
||||
const currency = money.currency === "RUB" ? "₽" : money.currency;
|
||||
const formatted = `${formatDecimal(decimal)} ${currency}`;
|
||||
|
||||
return `${formatDecimal(decimal)} ${currency}`;
|
||||
if (!withSign) return formatted;
|
||||
if (isZeroDecimal(value))
|
||||
return value.startsWith("-") ? formatted.slice(1) : formatted;
|
||||
|
||||
return value.startsWith("-") ? `−${formatted.slice(1)}` : `+${formatted}`;
|
||||
}
|
||||
|
||||
function isZeroDecimal(value: string) {
|
||||
const unsigned = value.startsWith("-") ? value.slice(1) : value;
|
||||
|
||||
const [integer = "", fraction = ""] = unsigned.split(
|
||||
".",
|
||||
DECIMAL_PART_COUNT,
|
||||
);
|
||||
|
||||
return integer.replaceAll("0", "") === ""
|
||||
&& fraction.replaceAll("0", "") === "";
|
||||
}
|
||||
|
||||
function roundDecimalToCents(value: string): string {
|
||||
@@ -116,4 +145,5 @@ export {
|
||||
formatMoney,
|
||||
formatSnapshotTime,
|
||||
instrumentLogoUrl,
|
||||
portfolioCategoryPresentation,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user