From dfe32780e0862515eb4b2ddebae9fcfb2198704a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 13 Sep 2022 23:18:40 -0700 Subject: currency formatting helpers --- src/lib/utils.js | 10 ++++++++++ src/routes/[username]/dashboard/+page.svelte | 16 ++++----------- src/routes/[username]/receive/+page.svelte | 15 ++++---------- src/routes/[username]/transactions/+page.svelte | 10 +++------- src/routes/invoice/[id]/+page.svelte | 26 +++++++------------------ 5 files changed, 28 insertions(+), 49 deletions(-) diff --git a/src/lib/utils.js b/src/lib/utils.js index 0f665ec4..80419a45 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -127,3 +127,13 @@ export const auth = (req) => { let { token } = cookie.parse(req.headers.get('cookie') || ''); return { authorization: `Bearer ${token}` }; }; + +export const f = (s, currency) => + new Intl.NumberFormat('en-US', { + style: 'currency', + currency + }) + .format(s) + .replace('CA', ''); + +export const s = (s) => new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format(s); diff --git a/src/routes/[username]/dashboard/+page.svelte b/src/routes/[username]/dashboard/+page.svelte index e61360c9..eee13576 100644 --- a/src/routes/[username]/dashboard/+page.svelte +++ b/src/routes/[username]/dashboard/+page.svelte @@ -1,5 +1,5 @@ diff --git a/src/routes/[username]/receive/+page.svelte b/src/routes/[username]/receive/+page.svelte index f83fc953..12960984 100644 --- a/src/routes/[username]/receive/+page.svelte +++ b/src/routes/[username]/receive/+page.svelte @@ -3,7 +3,7 @@ import { AppHeader, Icon } from '$comp'; import { goto } from '$app/navigation'; import { invoiceAmount, invoiceAmountFiat, selectedRate, user } from '$lib/store'; - import { post, warning } from '$lib/utils'; + import { f, s, post, warning } from '$lib/utils'; import { page } from '$app/stores'; import { t } from '$lib/translations'; @@ -19,18 +19,11 @@ amountFiat === 0 ? amountSats * ($selectedRate / 100000000) : amountFiat ); - $: amountSatsFormatted = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format( - amountSats - ); + $: amountSatsFormatted = s(amountSats); - $: amountFiatConverted = new Intl.NumberFormat('en-US', { - style: 'currency', - currency: $user.currency - }).format(amountSats * ($selectedRate / 100000000)); + $: amountFiatConverted = f(amountSats * ($selectedRate / 100000000), $user.currency); - $: amountSatsConverted = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format( - amountFiat / ($selectedRate / 100000000) - ); + $: amountSatsConverted = s(amountFiat / ($selectedRate / 100000000)); const numPad = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '0', '<']; diff --git a/src/routes/[username]/transactions/+page.svelte b/src/routes/[username]/transactions/+page.svelte index 9bc67fda..5a132394 100644 --- a/src/routes/[username]/transactions/+page.svelte +++ b/src/routes/[username]/transactions/+page.svelte @@ -2,6 +2,7 @@ import { AppHeader } from '$comp'; import { user, newPayment } from '$lib/store'; import { t } from '$lib/translations'; + import { f, s } from "$lib/utils"; $newPayment = false; @@ -28,16 +29,11 @@
{tx.amount > 0 ? '+' : ''}{new Intl.NumberFormat('en-US', { - style: 'currency', - currency: tx.currency - }).format(tx.amount * (tx.rate / 100000000))} + >{tx.amount > 0 ? '+' : ''}{f(tx.amount * (tx.rate / 100000000), tx.currency)} {tx.amount > 0 ? '+' : ''}{new Intl.NumberFormat('en-US', { - maximumFractionDigits: 0 - }).format(tx.amount)} + >{tx.amount > 0 ? '+' : ''}{s(tx.amount)} SAT
diff --git a/src/routes/invoice/[id]/+page.svelte b/src/routes/invoice/[id]/+page.svelte index df25c359..978e2b9a 100644 --- a/src/routes/invoice/[id]/+page.svelte +++ b/src/routes/invoice/[id]/+page.svelte @@ -1,12 +1,11 @@