diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-07-14 02:10:06 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-07-14 02:11:49 +0800 |
commit | c5fcdd06579b67b5b129749474acd9a4d1d4db65 (patch) | |
tree | e8741f39fc11b3bd816c2e6c9e5a4e8bae2b8543 | |
parent | b4747c3f85acb7d2909c5b7673d98055e8daaebe (diff) | |
download | dexon-sol-tools-c5fcdd06579b67b5b129749474acd9a4d1d4db65.tar dexon-sol-tools-c5fcdd06579b67b5b129749474acd9a4d1d4db65.tar.gz dexon-sol-tools-c5fcdd06579b67b5b129749474acd9a4d1d4db65.tar.bz2 dexon-sol-tools-c5fcdd06579b67b5b129749474acd9a4d1d4db65.tar.lz dexon-sol-tools-c5fcdd06579b67b5b129749474acd9a4d1d4db65.tar.xz dexon-sol-tools-c5fcdd06579b67b5b129749474acd9a4d1d4db65.tar.zst dexon-sol-tools-c5fcdd06579b67b5b129749474acd9a4d1d4db65.zip |
Move format to helper function
-rw-r--r-- | packages/website/ts/utils/utils.ts | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/packages/website/ts/utils/utils.ts b/packages/website/ts/utils/utils.ts index 77b0846a9..f2baeb849 100644 --- a/packages/website/ts/utils/utils.ts +++ b/packages/website/ts/utils/utils.ts @@ -386,6 +386,14 @@ export const utils = { getFormattedAmountFromToken(token: Token, tokenState: TokenState): string { return utils.getFormattedAmount(tokenState.balance, token.decimals); }, + format(value: BigNumber, format: string): string { + const formattedAmount = numeral(value).format(format); + if (_.isNaN(formattedAmount)) { + // https://github.com/adamwdraper/Numeral-js/issues/596 + return numeral(new BigNumber(0)).format(format); + } + return formattedAmount; + }, getFormattedAmount(amount: BigNumber, decimals: number): string { const unitAmount = Web3Wrapper.toUnitAmount(amount, decimals); // if the unit amount is less than 1, show the natural number of decimal places with a max of 4 @@ -394,22 +402,12 @@ export const utils = { const greaterThanOnePrecision = 2; const precision = unitAmount.lt(1) ? lessThanOnePrecision : greaterThanOnePrecision; const format = `0,0.${_.repeat('0', precision)}`; - const formattedAmount = numeral(unitAmount).format(format); - if (_.isNaN(formattedAmount)) { - // https://github.com/adamwdraper/Numeral-js/issues/596 - return '0'; - } - return formattedAmount; + return utils.format(unitAmount, format); }, getUsdValueFormattedAmount(amount: BigNumber, decimals: number, price: BigNumber): string { const unitAmount = Web3Wrapper.toUnitAmount(amount, decimals); const value = unitAmount.mul(price); - const formattedAmount = numeral(value).format(constants.NUMERAL_USD_FORMAT); - if (_.isNaN(formattedAmount)) { - // https://github.com/adamwdraper/Numeral-js/issues/596 - return numeral(new BigNumber(0)).format(constants.NUMERAL_USD_FORMAT); - } - return formattedAmount; + return utils.format(value, constants.NUMERAL_USD_FORMAT); }, openUrl(url: string): void { window.open(url, '_blank'); |