diff --git a/packages/util/src/format/formatBalance.spec.ts b/packages/util/src/format/formatBalance.spec.ts index 6fda514cb..8790647f0 100644 --- a/packages/util/src/format/formatBalance.spec.ts +++ b/packages/util/src/format/formatBalance.spec.ts @@ -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'); }); }); diff --git a/packages/util/src/format/formatBalance.ts b/packages/util/src/format/formatBalance.ts index 105619f21..1cb1ef0c7 100644 --- a/packages/util/src/format/formatBalance.ts +++ b/packages/util/src/format/formatBalance.ts @@ -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 { (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 . notation -function _formatBalance (input?: number | string | BN | ExtToBn, options: Options = true, decimals: number = defaultDecimals): string { +function _formatBalance (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 (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 (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}` : ''}` ) : ''; diff --git a/yarn.lock b/yarn.lock index 3bbc5e0e9..402e08f04 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5317,13 +5317,6 @@ etag@~1.8.1: resolved "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc= -ethereumjs-testing@1.0.4: - version "1.0.4" - resolved "https://registry.npmjs.org/ethereumjs-testing/-/ethereumjs-testing-1.0.4.tgz#8739f4104e9838ba92f3060eeb165c581041251d" - integrity sha1-hzn0EE6YOLqS8wYO6xZcWBBBJR0= - dependencies: - node-dir "^0.1.16" - eventemitter3@^3.1.0: version "3.1.2" resolved "https://registry.npmjs.org/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7" @@ -8045,11 +8038,6 @@ lru-cache@^5.1.1: dependencies: yallist "^3.0.2" -lru_map@^0.3.3: - version "0.3.3" - resolved "https://registry.npmjs.org/lru_map/-/lru_map-0.3.3.tgz#b5c8351b9464cbd750335a79650a0ec0e56118dd" - integrity sha1-tcg1G5Rky9dQM1p5ZQoOwOVhGN0= - lunr@^2.3.8: version "2.3.8" resolved "https://registry.npmjs.org/lunr/-/lunr-2.3.8.tgz#a8b89c31f30b5a044b97d2d28e2da191b6ba2072" @@ -8676,13 +8664,6 @@ no-case@^2.2.0: dependencies: lower-case "^1.1.1" -node-dir@^0.1.16: - version "0.1.17" - resolved "https://registry.npmjs.org/node-dir/-/node-dir-0.1.17.tgz#5f5665d93351335caabef8f1c554516cf5f1e4e5" - integrity sha1-X1Zl2TNRM1yqvvjxxVRRbPXx5OU= - dependencies: - minimatch "^3.0.2" - node-fetch-npm@^2.0.2: version "2.0.2" resolved "https://registry.npmjs.org/node-fetch-npm/-/node-fetch-npm-2.0.2.tgz#7258c9046182dca345b4208eda918daf33697ff7"