From 7a692fc8a517e28974b9607d81f56613afd3b62e Mon Sep 17 00:00:00 2001 From: Pierre Bertet Date: Fri, 26 Apr 2019 15:32:13 +0200 Subject: [PATCH] Prevent RangeError when .toLocaleString() is used Prevents the app to crash on the current Firefox Nightly. --- apps/finance/app/src/lib/utils.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/apps/finance/app/src/lib/utils.js b/apps/finance/app/src/lib/utils.js index 2346947779..921cff17d6 100644 --- a/apps/finance/app/src/lib/utils.js +++ b/apps/finance/app/src/lib/utils.js @@ -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) ) +}