Allow with expanded SI unit (m -> milli) (#505)

This commit is contained in:
Jaco Greeff
2020-01-23 16:07:01 +01:00
committed by GitHub
parent 41f024d0fc
commit e09d0ca5ca
3 changed files with 44 additions and 40 deletions
+34 -16
View File
@@ -17,13 +17,13 @@ describe('formatBalance', (): void => {
it('formats 123,456,789,000 (decimals=15)', (): void => {
expect(
formatBalance(TESTVAL, true, 15)
formatBalance(TESTVAL, { withSi: true }, 15)
).toEqual('123.456µ Unit');
});
it('formats 123,456,789,000 (decimals=36)', (): void => {
expect(
formatBalance(TESTVAL, true, 36)
formatBalance(TESTVAL, { withSi: true }, 36)
).toEqual('0.123y Unit');
});
@@ -34,55 +34,73 @@ describe('formatBalance', (): void => {
something: 'else'
};
expect(
formatBalance(compact, true, 15)
formatBalance(compact, { withSi: true }, 15)
).toEqual('123.456µ Unit');
});
it('formats 123,456,789,000 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL, true, 12)
formatBalance(TESTVAL, { withSi: true }, 12)
).toEqual('123.456m Unit');
});
it('formats 123,456,789,000 (decimals=12, no SI)', (): void => {
expect(
formatBalance(TESTVAL, false, 12)
).toEqual('123.456');
});
it('formats 123,456,789,000 (decimals=9)', (): void => {
expect(
formatBalance(TESTVAL, true, 9)
formatBalance(TESTVAL, { withSi: true }, 9)
).toEqual('123.456 Unit');
});
it('formats 123,456,789,000 (decimals=12, no SI)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: false }, 12)
).toEqual('123.456');
});
it('formats 123,456,789,000 (decimals=12, full SI)', (): void => {
expect(
formatBalance(TESTVAL, { withSiFull: true }, 12)
).toEqual('123.456 milli Unit');
});
it('formats 123,456,789,000 (decimals=12, full SI, no unit)', (): void => {
expect(
formatBalance(TESTVAL, { withSiFull: true, withUnit: false }, 12)
).toEqual('123.456 milli');
});
it('formats 123,456,789,000 (decimals=9, full SI)', (): void => {
expect(
formatBalance(TESTVAL, { withSiFull: true }, 9)
).toEqual('123.456 Unit');
});
it('formats 123,456,789,000 (decimals=6)', (): void => {
expect(
formatBalance(TESTVAL, true, 6)
formatBalance(TESTVAL, { withSi: true }, 6)
).toEqual('123.456k Unit');
});
it('formats 123,456,789,000 * 10 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL.muln(10), true, 12)
formatBalance(TESTVAL.muln(10), { withSi: true }, 12)
).toEqual('1.234 Unit');
});
it('formats 123,456,789,000 * 100 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL.muln(100), true, 12)
formatBalance(TESTVAL.muln(100), { withSi: true }, 12)
).toEqual('12.345 Unit');
});
it('formats 123,456,789,000 * 1000 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL.muln(1000), true, 12)
formatBalance(TESTVAL.muln(1000), { withSi: true }, 12)
).toEqual('123.456 Unit');
});
it('formats -123,456,789,000 (decimals=15)', (): void => {
expect(
formatBalance(new BN('-123456789000'), true, 15)
formatBalance(new BN('-123456789000'), { withSi: true }, 15)
).toEqual('-123.456µ Unit');
});
});
+10 -5
View File
@@ -17,7 +17,12 @@ interface Defaults {
unit: string;
}
type Options = boolean | { forceUnit?: string; withSi?: boolean };
interface Options {
forceUnit?: string;
withSi?: boolean;
withSiFull?: boolean;
withUnit?: boolean;
}
interface BalanceFormatter {
<ExtToBn extends ToBn> (input?: number | string | BN | ExtToBn, options?: Options, decimals?: number): string;
@@ -35,7 +40,7 @@ let defaultDecimals = DEFAULT_DECIMALS;
let defaultUnit = DEFAULT_UNIT;
// Formats a string/number with <prefix>.<postfix><type> notation
function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | ExtToBn, options: Options = true, decimals: number = defaultDecimals): string {
function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | ExtToBn, options: Options | boolean = true, decimals: number = defaultDecimals): string {
let text = bnToBn(input).toString();
if (text.length === 0 || text === '0') {
@@ -51,7 +56,7 @@ function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | E
}
// extract options - the boolean case is for backwards-compat
const { forceUnit = undefined, withSi = true } = isBoolean(options)
const { forceUnit = undefined, withSi = true, withSiFull = false, withUnit = true } = isBoolean(options)
? { withSi: options }
: options;
@@ -63,11 +68,11 @@ function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | E
const prefix = text.substr(0, mid);
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
const units = withSi || withSiFull
? (
si.value === '-'
? ` ${si.text}`
: `${si.value} ${SI[SI_MID].text}`
: `${withSiFull ? ` ${si.text}` : si.value}${withUnit ? ` ${SI[SI_MID].text}` : ''}`
)
: '';