Skip to content

Commit

Permalink
add helper formatStandard for wail frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof committed May 16, 2024
1 parent 69192bc commit e076825
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/lib/util/parseAmount.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
formatAmount,
formatStandard,
roundDecimalPartToOneSignificantDigit,
} from './parseAmount';

Expand Down Expand Up @@ -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');
});
});
11 changes: 11 additions & 0 deletions src/lib/util/parseAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit e076825

Please sign in to comment.