diff options
Diffstat (limited to 'frontend/lib')
| -rw-r--r-- | frontend/lib/api.ts | 7 | ||||
| -rw-r--r-- | frontend/lib/format.ts | 31 |
2 files changed, 30 insertions, 8 deletions
diff --git a/frontend/lib/api.ts b/frontend/lib/api.ts index 2635af5..a584241 100644 --- a/frontend/lib/api.ts +++ b/frontend/lib/api.ts @@ -1,4 +1,4 @@ -import type { FilingsResponse, FinancialsResponse, HistoryPoint, InsidersResponse, MarketIndex, NewsResponse, RatiosResponse, SearchResult, TickerOverview, ValuationResponse, WatchlistResponse } from "@/types/api"; +import type { FilingsResponse, FinancialsResponse, HistoryPoint, InsidersResponse, MarketIndex, NewsResponse, RatiosResponse, SearchResult, TickerOverview, ValuationResponse, WaccResponse, WatchlistResponse } from "@/types/api"; const API_BASE = (process.env.NEXT_PUBLIC_API_BASE_URL || "").trim().replace(/\/$/, ""); @@ -59,6 +59,11 @@ export const api = { `/api/tickers/${encodeURIComponent(symbol)}/valuation` ); }, + wacc(symbol: string) { + return request<WaccResponse>( + `/api/tickers/${encodeURIComponent(symbol)}/wacc` + ); + }, ratios(symbol: string) { return request<RatiosResponse>( `/api/tickers/${encodeURIComponent(symbol)}/ratios` diff --git a/frontend/lib/format.ts b/frontend/lib/format.ts index 6a1fcc2..e4bae13 100644 --- a/frontend/lib/format.ts +++ b/frontend/lib/format.ts @@ -1,6 +1,22 @@ -export function fmtCurrency(value?: number | null, decimals = 2): string { +const CURRENCY_SYMBOLS: Record<string, string> = { + USD: "$", + CAD: "C$", + GBP: "£", + EUR: "€", + JPY: "¥", + AUD: "A$", + CHF: "CHF ", + CNY: "CN¥", + HKD: "HK$", +}; + +function currencySymbol(currency = "USD"): string { + return CURRENCY_SYMBOLS[currency.toUpperCase()] || `${currency.toUpperCase()} `; +} + +export function fmtCurrency(value?: number | null, decimals = 2, currency = "USD"): string { if (value === null || value === undefined || Number.isNaN(value)) return "-"; - return `$${value.toLocaleString(undefined, { maximumFractionDigits: decimals, minimumFractionDigits: decimals })}`; + return `${currencySymbol(currency)}${value.toLocaleString(undefined, { maximumFractionDigits: decimals, minimumFractionDigits: decimals })}`; } export function fmtNumber(value?: number | null, decimals = 2): string { @@ -8,14 +24,15 @@ export function fmtNumber(value?: number | null, decimals = 2): string { return value.toLocaleString(undefined, { maximumFractionDigits: decimals, minimumFractionDigits: decimals }); } -export function fmtLarge(value?: number | null): string { +export function fmtLarge(value?: number | null, currency = "USD"): string { if (value === null || value === undefined || Number.isNaN(value)) return "-"; const abs = Math.abs(value); const sign = value < 0 ? "-" : ""; - if (abs >= 1e12) return `${sign}$${(abs / 1e12).toFixed(2)}T`; - if (abs >= 1e9) return `${sign}$${(abs / 1e9).toFixed(2)}B`; - if (abs >= 1e6) return `${sign}$${(abs / 1e6).toFixed(1)}M`; - return `${sign}$${abs.toLocaleString(undefined, { maximumFractionDigits: 0 })}`; + const symbol = currencySymbol(currency); + if (abs >= 1e12) return `${sign}${symbol}${(abs / 1e12).toFixed(2)}T`; + if (abs >= 1e9) return `${sign}${symbol}${(abs / 1e9).toFixed(2)}B`; + if (abs >= 1e6) return `${sign}${symbol}${(abs / 1e6).toFixed(1)}M`; + return `${sign}${symbol}${abs.toLocaleString(undefined, { maximumFractionDigits: 0 })}`; } export function fmtPct(value?: number | null, decimals = 2, signed = false): string { |
