UNPKG

@empellio/currency-utils

Version:

Lightweight TypeScript utilities for currency formatting and conversions using live exchange rates from public APIs (ECB, CNB, exchangerate.host), with caching and offline fallback.

36 lines (31 loc) 2.83 kB
type CurrencyCode = 'AED' | 'AFN' | 'ALL' | 'AMD' | 'ANG' | 'AOA' | 'ARS' | 'AUD' | 'AWG' | 'AZN' | 'BAM' | 'BBD' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BMD' | 'BND' | 'BOB' | 'BRL' | 'BSD' | 'BTN' | 'BWP' | 'BYN' | 'BZD' | 'CAD' | 'CDF' | 'CHF' | 'CLP' | 'CNY' | 'COP' | 'CRC' | 'CUC' | 'CUP' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'EUR' | 'FJD' | 'FKP' | 'GBP' | 'GEL' | 'GHS' | 'GIP' | 'GMD' | 'GNF' | 'GTQ' | 'GYD' | 'HKD' | 'HNL' | 'HRK' | 'HTG' | 'HUF' | 'IDR' | 'ILS' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KGS' | 'KHR' | 'KMF' | 'KPW' | 'KRW' | 'KWD' | 'KYD' | 'KZT' | 'LAK' | 'LBP' | 'LKR' | 'LRD' | 'LSL' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MNT' | 'MOP' | 'MRU' | 'MUR' | 'MVR' | 'MWK' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PGK' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SBD' | 'SCR' | 'SDG' | 'SEK' | 'SGD' | 'SHP' | 'SLE' | 'SLL' | 'SOS' | 'SRD' | 'SSP' | 'STN' | 'SVC' | 'SYP' | 'SZL' | 'THB' | 'TJS' | 'TMT' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'USD' | 'UYU' | 'UZS' | 'VES' | 'VND' | 'VUV' | 'WST' | 'XAF' | 'XCD' | 'XDR' | 'XOF' | 'XPF' | 'YER' | 'ZAR' | 'ZMW'; type RoundingMode = 'half-up' | 'half-down' | 'floor' | 'ceil'; interface FormatCurrencyOptions { currency: CurrencyCode; locale?: string; minimumFractionDigits?: number; maximumFractionDigits?: number; style?: 'currency' | 'code' | 'symbol'; roundingMode?: RoundingMode; } interface ConvertCurrencyOptions { date?: string | Date; precision?: number; roundingMode?: RoundingMode; } interface RateProviderOptions { date?: string | Date; } type RateProviderName = 'ecb' | 'cnb' | 'exchangeratehost'; type RateProviderFn = (from: CurrencyCode, to: CurrencyCode, options?: RateProviderOptions) => Promise<number>; interface RateCache { getRate: (key: string) => Promise<number | undefined> | number | undefined; setRate: (key: string, value: number, ttlMs?: number) => Promise<void> | void; } declare function formatCurrency(value: number, options: FormatCurrencyOptions): string; declare function convertCurrency(amount: number, from: CurrencyCode, to: CurrencyCode, options?: ConvertCurrencyOptions): Promise<number>; declare function setRateProvider(nameOrFn: RateProviderName | RateProviderFn): void; declare function setCache(customCache: RateCache): void; declare function getCache(): RateCache; declare function setDefaultConvertPrecision(precision: number): void; export { type ConvertCurrencyOptions, type CurrencyCode, type FormatCurrencyOptions, type RateCache, type RateProviderFn, convertCurrency, formatCurrency, getCache, setCache, setDefaultConvertPrecision, setRateProvider };