From 786c185464a8e42f919599b239cb48518a7c5346 Mon Sep 17 00:00:00 2001
From: SatsAllDay <128755788+SatsAllDay@users.noreply.github.com>
Date: Mon, 18 Sep 2023 18:49:13 -0400
Subject: [PATCH] Introduce format option on `numWithUnits` fn, consumed by
bounty listing (#496)
---
components/item-full.js | 2 +-
lib/format.js | 6 ++++--
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/components/item-full.js b/components/item-full.js
index 39765216..dbcfead7 100644
--- a/components/item-full.js
+++ b/components/item-full.js
@@ -150,7 +150,7 @@ function TopLevelItem ({ item, noReply, ...props }) {
)
: (
- {numWithUnits(item.bounty, { abbreviate: false })} bounty
+ {numWithUnits(item.bounty, { abbreviate: false, format: true })} bounty
)}
}
diff --git a/lib/format.js b/lib/format.js
index 2bcd0763..ebcc0519 100644
--- a/lib/format.js
+++ b/lib/format.js
@@ -15,16 +15,18 @@ export const abbrNum = n => {
* @param opts.abbreviate Whether to abbreviate the number
* @param opts.unitSingular The singular unit label
* @param opts.unitPlural The plural unit label
+ * @param opts.format Format the number with `Intl.NumberFormat`. Can only be used if `abbreviate` is false
*/
export const numWithUnits = (n, {
abbreviate = true,
unitSingular = 'sat',
- unitPlural = 'sats'
+ unitPlural = 'sats',
+ format = false
} = {}) => {
if (isNaN(n)) {
return `${n} ${unitPlural}`
}
- return `${abbreviate ? abbrNum(n) : n} ${n === 1 ? unitSingular : unitPlural}`
+ return `${abbreviate ? abbrNum(n) : format ? new Intl.NumberFormat().format(n) : n} ${n === 1 ? unitSingular : unitPlural}`
}
export const fixedDecimal = (n, f) => {