Skip to content

Commit

Permalink
Finance: prevent RangeError when .toLocaleString() is used (aragon#827)
Browse files Browse the repository at this point in the history
Prevents the app to crash on the current Firefox Nightly.
  • Loading branch information
bpierre authored Apr 26, 2019
1 parent be0929f commit cd76e6d
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions apps/finance/app/src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { round } from './math-utils'

export const formatTokenAmount = (
export function formatDecimals(value, digits) {
try {
return value.toLocaleString('latn', {
style: 'decimal',
maximumFractionDigits: digits,
})
} catch (err) {
if (err.name === 'RangeError') {
// Fallback to Number.prototype.toString()
// if the language tag is not supported.
return value.toString()
}
throw err
}
}

export function formatTokenAmount(
amount,
isIncoming,
decimals = 0,
displaySign = false,
{ rounding = 2 } = {}
) =>
(displaySign ? (isIncoming ? '+' : '-') : '') +
Number(round(amount / Math.pow(10, decimals), rounding)).toLocaleString(
'latn',
{
style: 'decimal',
maximumFractionDigits: 18,
}
) {
return (
(displaySign ? (isIncoming ? '+' : '-') : '') +
formatDecimals(round(amount / Math.pow(10, decimals), rounding), 18)
)
}

0 comments on commit cd76e6d

Please sign in to comment.