From e07682520ab03f52beb78c2e85d90c92fdd00b94 Mon Sep 17 00:00:00 2001 From: Nathan Seva Date: Thu, 16 May 2024 16:36:43 -0500 Subject: [PATCH] add helper formatStandard for wail frontend --- src/lib/util/parseAmount.test.ts | 73 ++++++++++++++++++++++++++++++++ src/lib/util/parseAmount.ts | 11 +++++ 2 files changed, 84 insertions(+) diff --git a/src/lib/util/parseAmount.test.ts b/src/lib/util/parseAmount.test.ts index 9cb06a13..66e5dd4a 100644 --- a/src/lib/util/parseAmount.test.ts +++ b/src/lib/util/parseAmount.test.ts @@ -1,5 +1,6 @@ import { formatAmount, + formatStandard, roundDecimalPartToOneSignificantDigit, } from './parseAmount'; @@ -86,3 +87,75 @@ describe('roundDecimalPartToOneSignificantDigit', () => { expect(roundDecimalPartToOneSignificantDigit('0099')).toEqual('01'); }); }); + +describe('formatStandard', () => { + test('formats an empty string', () => { + const result = formatStandard('', 18); + expect(result).toEqual('0'); + }); + + test('formats an amount with default parameters', () => { + const result = formatStandard('123456789012345678901', 18); + expect(result).toEqual('123.456789012345678901'); + }); + + test('formats an amount with less than the specified decimals', () => { + const result = formatStandard('12345', 8); + expect(result).toEqual('0.00012345'); + }); + + test('adds padding zeroes when necessary', () => { + const result = formatStandard('1', 18); + expect(result).toEqual('0.000000000000000001'); + }); + + test('handles amount with exact decimals length', () => { + const result = formatStandard('1000000000000000000', 18); + expect(result).toEqual('1'); + }); + + test('formats an amount with less than the specified decimals and round up', () => { + const result = formatStandard('69000', 9); + expect(result).toEqual('0.000069'); + }); + + it('formatStandard with min string value', () => { + const value = '0000000000'; + + const result = formatStandard(value.toString()); + + expect(result).toBe('0'); + }); + + it('formatStandard with min bigint value', () => { + const value = 0n; + + const result = formatStandard(value.toString()); + + expect(result).toBe('0'); + }); + + it('formatStandard with mid range string value', () => { + const value = '10000000000000'; + + const result = formatStandard(value.toString()); + + expect(result).toBe('10,000'); + }); + + it('formatStandard with mid range bigint value', () => { + const value = 10000000000000n; + + const result = formatStandard(value.toString()); + + expect(result).toBe('10,000'); + }); + + it('formatStandard with max string value', () => { + const value = '922337203600000000000'; + + const result = formatStandard(value.toString()); + + expect(result).toBe('922,337,203,600'); + }); +}); diff --git a/src/lib/util/parseAmount.ts b/src/lib/util/parseAmount.ts index b6313f33..80dc61b6 100644 --- a/src/lib/util/parseAmount.ts +++ b/src/lib/util/parseAmount.ts @@ -7,6 +7,17 @@ export interface FormattedAmount { amountFormattedFull: string; } +function removeTrailingZeros(numStr: string): string { + return numStr.replace(/\.?0+$/, ''); +} + +// Like format amount but remove the trailing zeros +export function formatStandard(amount: string, decimals = 9): string { + return removeTrailingZeros( + formatAmount(amount, decimals).amountFormattedFull, + ); +} + /** * reverse format FT amount */