Introduce format option on `numWithUnits` fn, consumed by bounty listing (#496)

This commit is contained in:
SatsAllDay 2023-09-18 18:49:13 -04:00 committed by GitHub
parent b0737e53d1
commit 786c185464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -150,7 +150,7 @@ function TopLevelItem ({ item, noReply, ...props }) {
</div>) </div>)
: ( : (
<div className='px-3 py-1 d-inline-block bg-grey-darkmode rounded text-light'> <div className='px-3 py-1 d-inline-block bg-grey-darkmode rounded text-light'>
{numWithUnits(item.bounty, { abbreviate: false })} bounty {numWithUnits(item.bounty, { abbreviate: false, format: true })} bounty
</div>)} </div>)}
</div>} </div>}
</div> </div>

View File

@ -15,16 +15,18 @@ export const abbrNum = n => {
* @param opts.abbreviate Whether to abbreviate the number * @param opts.abbreviate Whether to abbreviate the number
* @param opts.unitSingular The singular unit label * @param opts.unitSingular The singular unit label
* @param opts.unitPlural The plural 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, { export const numWithUnits = (n, {
abbreviate = true, abbreviate = true,
unitSingular = 'sat', unitSingular = 'sat',
unitPlural = 'sats' unitPlural = 'sats',
format = false
} = {}) => { } = {}) => {
if (isNaN(n)) { if (isNaN(n)) {
return `${n} ${unitPlural}` 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) => { export const fixedDecimal = (n, f) => {