diff --git a/web/cashtab/src/utils/__tests__/cashMethods.test.js b/web/cashtab/src/utils/__tests__/cashMethods.test.js index 1936f526c..0c2f6b6c4 100644 --- a/web/cashtab/src/utils/__tests__/cashMethods.test.js +++ b/web/cashtab/src/utils/__tests__/cashMethods.test.js @@ -1,31 +1,40 @@ import { fromSmallestDenomination, formatBalance } from '@utils/cashMethods'; describe('Correctly executes cash utility functions', () => { it(`Correctly converts smallest base unit to smallest decimal for cashDecimals = 2`, () => { expect(fromSmallestDenomination(1, 2)).toBe(0.01); }); it(`Correctly converts largest base unit to smallest decimal for cashDecimals = 2`, () => { expect(fromSmallestDenomination(1000000012345678, 2)).toBe( 10000000123456.78, ); }); it(`Correctly converts smallest base unit to smallest decimal for cashDecimals = 8`, () => { expect(fromSmallestDenomination(1, 8)).toBe(0.00000001); }); it(`Correctly converts largest base unit to smallest decimal for cashDecimals = 8`, () => { expect(fromSmallestDenomination(1000000012345678, 8)).toBe( 10000000.12345678, ); }); it(`Formats a large number to add spaces as thousands separator`, () => { expect(formatBalance(1000000012345678)).toBe('1 000 000 012 345 678'); }); it(`Formats a large number with 2 decimal places to add as thousands separator`, () => { expect(formatBalance(10000000123456.78)).toBe('10 000 000 123 456.78'); }); it(`Formats a large number with 9 decimal places to add as thousands separator without adding them to decimals`, () => { expect(formatBalance('10000000123456.789123456')).toBe( '10 000 000 123 456.789123456', ); }); + it(`formatBalance handles an input of 0`, () => { + expect(formatBalance('0')).toBe('0'); + }); + it(`formatBalance handles an input of undefined`, () => { + expect(formatBalance(undefined)).toBe(undefined); + }); + it(`formatBalance handles an input of null`, () => { + expect(formatBalance(null)).toBe(null); + }); }); diff --git a/web/cashtab/src/utils/cashMethods.js b/web/cashtab/src/utils/cashMethods.js index 70d18d1ca..77b82f545 100644 --- a/web/cashtab/src/utils/cashMethods.js +++ b/web/cashtab/src/utils/cashMethods.js @@ -1,39 +1,48 @@ import { currency } from '@components/Common/Ticker'; import BigNumber from 'bignumber.js'; export const fromSmallestDenomination = ( amount, cashDecimals = currency.cashDecimals, ) => { const amountBig = new BigNumber(amount); const multiplier = new BigNumber(10 ** (-1 * cashDecimals)); const amountInBaseUnits = amountBig.times(multiplier); return amountInBaseUnits.toNumber(); }; export const toSmallestDenomination = ( sendAmount, cashDecimals = currency.cashDecimals, ) => { // Replace the BCH.toSatoshi method with an equivalent function that works for arbitrary decimal places // Example, for an 8 decimal place currency like Bitcoin // Input: a BigNumber of the amount of Bitcoin to be sent // Output: a BigNumber of the amount of satoshis to be sent, or false if input is invalid // Validate // Input should be a BigNumber with no more decimal places than cashDecimals const isValidSendAmount = BigNumber.isBigNumber(sendAmount) && sendAmount.dp() <= cashDecimals; if (!isValidSendAmount) { return false; } const conversionFactor = new BigNumber(10 ** cashDecimals); const sendAmountSmallestDenomination = sendAmount.times(conversionFactor); return sendAmountSmallestDenomination; }; export const formatBalance = x => { - let balanceInParts = x.toString().split('.'); - balanceInParts[0] = balanceInParts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ' '); - return balanceInParts.join('.'); + try { + let balanceInParts = x.toString().split('.'); + balanceInParts[0] = balanceInParts[0].replace( + /\B(?=(\d{3})+(?!\d))/g, + ' ', + ); + return balanceInParts.join('.'); + } catch (err) { + console.log(`Error in formatBalance for ${x}`); + console.log(err); + return x; + } };