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
+1 -2
View File
@@ -22,8 +22,7 @@ type MessageFn = () => string;
* assert(false, () => 'message'); // Error with 'message'
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function assert (condition: any, message: string | MessageFn): asserts condition {
export default function assert (condition: boolean | object | string | number | undefined | null, message: string | MessageFn): asserts condition {
if (!condition) {
throw new Error(
isFunction(message)
@@ -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}`;
+1 -2
View File
@@ -19,7 +19,6 @@ import BN from 'bn.js';
* console.log('isBn', isBn(new BN(1))); // => true
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function isBn (value: any): value is BN {
export default function isBn (value: object | string | number | undefined | null): value is BN {
return BN.isBN(value);
}