33 lines
991 B
TypeScript
33 lines
991 B
TypeScript
import type { Portfolio } from "../api/usePortfolio";
|
|
|
|
type Money = Portfolio["totalsByCurrency"][number];
|
|
|
|
export function formatDecimal(value: string): string {
|
|
const [integer, fraction] = value.split(".");
|
|
const sign = integer.startsWith("-") ? "-" : "";
|
|
const grouped = (sign ? integer.slice(1) : integer).replace(
|
|
/\B(?=(\d{3})+(?!\d))/g,
|
|
" ",
|
|
);
|
|
return `${sign}${grouped}${fraction ? `,${fraction}` : ""}`;
|
|
}
|
|
|
|
export function formatMoney(money: Money | null): string {
|
|
if (!money) return "—";
|
|
return `${formatDecimal(money.value)} ${money.currency === "RUB" ? "₽" : money.currency}`;
|
|
}
|
|
|
|
export function formatSnapshotTime(value: string): string {
|
|
return new Intl.DateTimeFormat("ru-RU", {
|
|
dateStyle: "medium",
|
|
timeStyle: "short",
|
|
timeZone: "UTC",
|
|
}).format(new Date(value));
|
|
}
|
|
|
|
export function portfolioErrorCode(error: unknown): string | undefined {
|
|
return typeof error === "object" && error !== null && "code" in error
|
|
? String(error.code)
|
|
: undefined;
|
|
}
|