formatBalance adjustments (#536)

* formatBalance extensions

* Fly-by any removals

* isBn updates

* Remove unneeded ()'s
This commit is contained in:
Jaco Greeff
2020-02-17 12:16:02 +01:00
committed by GitHub
parent dca36e41fd
commit 171015fbba
8 changed files with 38 additions and 22 deletions
@@ -50,6 +50,18 @@ describe('formatBalance', (): void => {
).toEqual('123.456 Unit');
});
it('formats 123,456,789,000 (decimals=9, no unit)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true, withUnit: false }, 9)
).toEqual('123.456');
});
it('formats 123,456,789,000 (decimals=9, unit given)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true, withUnit: 'FOO' }, 9)
).toEqual('123.456 FOO');
});
it('formats 123,456,789,000 (decimals=12, no SI)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: false }, 12)
@@ -80,6 +92,12 @@ describe('formatBalance', (): void => {
).toEqual('123.456k Unit');
});
it('formats 123,456,789,000 (decimals=6, unit specified)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true, withUnit: 'BAR' }, 6)
).toEqual('123.456k BAR');
});
it('formats 123,456,789,000 * 10 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL.muln(10), { withSi: true }, 12)
+6 -6
View File
@@ -21,7 +21,7 @@ interface Options {
forceUnit?: string;
withSi?: boolean;
withSiFull?: boolean;
withUnit?: boolean;
withUnit?: boolean | string;
}
interface BalanceFormatter {
@@ -69,11 +69,11 @@ function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | E
const padding = mid < 0 ? 0 - mid : 0;
const postfix = `${`${new Array(padding + 1).join('0')}${text}`.substr(mid < 0 ? 0 : mid)}000`.substr(0, 3);
const units = withSi || withSiFull
? (
si.value === '-'
? ` ${si.text}`
: `${withSiFull ? ` ${si.text}` : si.value}${withUnit ? ` ${SI[SI_MID].text}` : ''}`
)
? si.value === '-'
? withUnit
? ` ${isBoolean(withUnit) ? si.text : withUnit}`
: ''
: `${withSiFull ? ` ${si.text}` : si.value}${withUnit ? ` ${isBoolean(withUnit) ? SI[SI_MID].text : withUnit}` : ''}`
: '';
return `${isNegative ? '-' : ''}${formatDecimal(prefix || '0')}.${postfix}${units}`;