web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit fb2d30b8dd66458ca5c0d05529d390a9dc8b8404
parent a3222f503975ee153d0ecddea9fead7474935c3f
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Thu, 24 Oct 2024 12:13:53 +0000

utils: add utils

Diffstat:
Mutils/src/currency.ts | 27++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/utils/src/currency.ts b/utils/src/currency.ts @@ -16,6 +16,8 @@ export type CurrencyPrice = { val_f: number; }; +export type CurrencyPriceFmt = [string, FiatCurrency, number, number] + export function parse_currency(val?: string): FiatCurrency { const _val = val?.trim().toLowerCase() switch (_val) { @@ -52,6 +54,29 @@ export const parse_currency_price = (locale: string, _currency: string, amount: currency, val_i: Number(symbol_val_i.replaceAll(`,`, ``).slice(1)), val_f: Number(val_f), - } + }; }; +export const parse_currency_price_fmt = (locale: string, _currency: string, amount: number): CurrencyPriceFmt => { + const currency = parse_currency(_currency); + const fmt = new Intl.NumberFormat(locale, { + style: 'currency', + currency: currency.toUpperCase(), + minimumFractionDigits: 2, + }); + const fmt_amt = fmt.format(amount); + const [symbol_val_i, val_f] = fmt_amt.split('.'); + return [symbol_val_i.charAt(0), currency, Number(symbol_val_i.replaceAll(`,`, ``).slice(1)), Number(val_f)]; +}; + +export const price_fmt = (locale: string, _currency: string): Intl.NumberFormat => { + const currency = parse_currency(_currency); + const fmt = new Intl.NumberFormat(locale, { + style: 'currency', + currency: currency.toUpperCase(), + minimumFractionDigits: 2, + }); + return fmt; +}; + +