diff options
| author | Adam Soltys <[email protected]> | 2022-09-13 23:18:40 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-13 23:18:40 -0700 |
| commit | dfe32780e0862515eb4b2ddebae9fcfb2198704a (patch) | |
| tree | f8b463ccc51291393c71cc3c95ca3725c74d341b | |
| parent | cfdc6ed4a98e1eb60aba653d4403138a8fcaa4c2 (diff) | |
| download | coinos-ui-dfe32780e0862515eb4b2ddebae9fcfb2198704a.tar.gz coinos-ui-dfe32780e0862515eb4b2ddebae9fcfb2198704a.zip | |
currency formatting helpers
| -rw-r--r-- | src/lib/utils.js | 10 | ||||
| -rw-r--r-- | src/routes/[username]/dashboard/+page.svelte | 16 | ||||
| -rw-r--r-- | src/routes/[username]/receive/+page.svelte | 15 | ||||
| -rw-r--r-- | src/routes/[username]/transactions/+page.svelte | 10 | ||||
| -rw-r--r-- | 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 @@ <script> - import { post, failure } from '$lib/utils'; + import { f, s, post, failure } from '$lib/utils'; import { tick } from 'svelte'; import { AppHeader, Icon } from '$comp'; import { selectedRate } from '$lib/store'; @@ -27,21 +27,13 @@ } }; - $: btcPrice = new Intl.NumberFormat('en-US', { - style: 'currency', - currency: user.currency - }).format($selectedRate); + $: btcPrice = f($selectedRate, user.currency); - $: accountBalanceFiat = new Intl.NumberFormat('en-US', { - style: 'currency', - currency: user.currency - }).format(user.account.balance * ($selectedRate / 100000000)); + $: accountBalanceFiat = f(user.account.balance * ($selectedRate / 100000000), user.currency); $: accountBalanceBtc = user.account.balance / 100000000; - $: accountBalanceSats = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format( - user.account.balance - ); + $: accountBalanceSats = s(user.account.balance); </script> <AppHeader avatarPosition="left-[calc(50vw-48px)] lg:left-[calc(15vw-48px)]" /> 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 @@ <div class="grid grid-cols-3 border-b pb-5"> <div class="font-bold"> <span class="block mb-1" - >{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)} </span> <span class="text-secondary" - >{tx.amount > 0 ? '+' : ''}{new Intl.NumberFormat('en-US', { - maximumFractionDigits: 0 - }).format(tx.amount)} + >{tx.amount > 0 ? '+' : ''}{s(tx.amount)} SAT </span> </div> 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 @@ <script> import { LottiePlayer } from '@lottiefiles/svelte-lottie-player'; - import { get, post, reverseFormat } from '$lib/utils'; + import { copy, f, get, post, reverseFormat, s } from '$lib/utils'; import { browser } from '$app/environment'; import { invoices, user } from '$lib/store'; import { Icon, Image, Qr } from '$comp'; import { goto, invalidate } from '$app/navigation'; import { onMount, tick } from 'svelte'; - import { copy } from '$lib/utils'; import { t } from '$lib/translations'; export let data; @@ -49,14 +48,11 @@ $: tipAmount = ((tip * rate) / 100000000).toFixed(2); - $: tipAmountFormatted = new Intl.NumberFormat('en-US', { - style: 'currency', - currency - }).format(tipAmount); + $: tipAmountFormatted = f(tipAmount, currency); - $: tipAmountSats = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format(tip); + $: tipAmountSats = s(tip); - $: totalAmountSats = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format( + $: totalAmountSats = s( amount + parseFloat(tipAmountSats.replace(/,/g, '')) ); @@ -79,19 +75,11 @@ goto(`/invoice/${r}`); }; - $: totalAmountFormatted = new Intl.NumberFormat('en-US', { - style: 'currency', - currency - }).format(amountFiat + parseFloat(tipAmount)); + $: totalAmountFormatted = f(amountFiat + parseFloat(tipAmount), currency); - const amountFormatted = new Intl.NumberFormat('en-US', { - maximumFractionDigits: 0 - }).format(amount); + const amountFormatted = f(amount, currency); - $: invoiceAmountFiatFormatted = new Intl.NumberFormat('en-US', { - style: 'currency', - currency - }).format(amountFiat); + $: invoiceAmountFiatFormatted = f(amountFiat, currency); let customTipAmount; |
