From f38975d126931abc1a419bfc3ec80832ae06d457 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 5 Nov 2019 13:53:49 +0100 Subject: [PATCH] Allow forcing of units in formatBalance (#486) --- package.json | 4 +- .../util/src/format/formatBalance.spec.ts | 166 ++++++++++-------- packages/util/src/format/formatBalance.ts | 14 +- packages/util/src/format/si.ts | 15 +- yarn.lock | 16 +- 5 files changed, 128 insertions(+), 87 deletions(-) diff --git a/package.json b/package.json index 2159d3035..050e6483a 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ }, "devDependencies": { "@babel/core": "^7.6.4", - "@polkadot/dev": "^0.32.0-beta.11", - "@polkadot/ts": "^0.1.82", + "@polkadot/dev": "^0.32.0-beta.12", + "@polkadot/ts": "^0.1.83", "gh-pages": "^2.1.1" } } diff --git a/packages/util/src/format/formatBalance.spec.ts b/packages/util/src/format/formatBalance.spec.ts index a8e02be9d..778d8ab57 100644 --- a/packages/util/src/format/formatBalance.spec.ts +++ b/packages/util/src/format/formatBalance.spec.ts @@ -9,80 +9,108 @@ import formatBalance from './formatBalance'; describe('formatBalance', (): void => { const TESTVAL = new BN('123456789000'); - it('formats empty to 0', (): void => { - expect(formatBalance()).toEqual('0'); - expect(formatBalance('0')).toEqual('0'); + describe('SI formatting', (): void => { + it('formats empty to 0', (): void => { + expect(formatBalance()).toEqual('0'); + expect(formatBalance('0')).toEqual('0'); + }); + + it('formats 123,456,789,000 (decimals=15)', (): void => { + expect( + formatBalance(TESTVAL, true, 15) + ).toEqual('123.456µ Unit'); + }); + + it('formats 123,456,789,000 (decimals=36)', (): void => { + expect( + formatBalance(TESTVAL, true, 36) + ).toEqual('0.123y Unit'); + }); + + it('formats 123,456,789,000 (decimals=15, Compact)', (): void => { + const compact = { + toBn: (): BN => TESTVAL, + unwrap: (): BN => TESTVAL, + something: 'else' + }; + expect( + formatBalance(compact, true, 15) + ).toEqual('123.456µ Unit'); + }); + + it('formats 123,456,789,000 (decimals=12)', (): void => { + expect( + formatBalance(TESTVAL, 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) + ).toEqual('123.456 Unit'); + }); + + it('formats 123,456,789,000 (decimals=6)', (): void => { + expect( + formatBalance(TESTVAL, true, 6) + ).toEqual('123.456k Unit'); + }); + + it('formats 123,456,789,000 * 10 (decimals=12)', (): void => { + expect( + formatBalance(TESTVAL.muln(10), true, 12) + ).toEqual('1.234 Unit'); + }); + + it('formats 123,456,789,000 * 100 (decimals=12)', (): void => { + expect( + formatBalance(TESTVAL.muln(100), true, 12) + ).toEqual('12.345 Unit'); + }); + + it('formats 123,456,789,000 * 1000 (decimals=12)', (): void => { + expect( + formatBalance(TESTVAL.muln(1000), true, 12) + ).toEqual('123.456 Unit'); + }); + + it('formats -123,456,789,000 (decimals=15)', (): void => { + expect( + formatBalance(new BN('-123456789000'), true, 15) + ).toEqual('-123.456µ Unit'); + }); }); - it('formats 123,456,789,000 (decimals=15)', (): void => { - expect( - formatBalance(TESTVAL, true, 15) - ).toEqual('123.456µ Unit'); - }); + describe('Forced formatting', (): void => { + it('formats 123,456,789,000 (decimals=12, forceUnit=base)', (): void => { + expect( + formatBalance(TESTVAL, { forceUnit: '-' }, 12) + ).toEqual('0.123 Unit'); + }); - it('formats 123,456,789,000 (decimals=36)', (): void => { - expect( - formatBalance(TESTVAL, true, 36) - ).toEqual('0.123y Unit'); - }); + it('formats 123,456,789,000 (decimals=9, forceUnit=base)', (): void => { + expect( + formatBalance(TESTVAL, { forceUnit: '-' }, 9) + ).toEqual('123.456 Unit'); + }); - it('formats 123,456,789,000 (decimals=15, Compact)', (): void => { - const compact = { - toBn: (): BN => TESTVAL, - unwrap: (): BN => TESTVAL, - something: 'else' - }; - expect( - formatBalance(compact, true, 15) - ).toEqual('123.456µ Unit'); - }); + it('formats 123,456,789,000 (decimals=7, forceUnit=base)', (): void => { + expect( + formatBalance(TESTVAL, { forceUnit: '-' }, 7) + ).toEqual('12,345.678 Unit'); + }); - it('formats 123,456,789,000 (decimals=12)', (): void => { - expect( - formatBalance(TESTVAL, 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) - ).toEqual('123.456 Unit'); - }); - - it('formats 123,456,789,000 (decimals=6)', (): void => { - expect( - formatBalance(TESTVAL, true, 6) - ).toEqual('123.456k Unit'); - }); - - it('formats 123,456,789,000 * 10 (decimals=12)', (): void => { - expect( - formatBalance(TESTVAL.muln(10), true, 12) - ).toEqual('1.234 Unit'); - }); - - it('formats 123,456,789,000 * 100 (decimals=12)', (): void => { - expect( - formatBalance(TESTVAL.muln(100), true, 12) - ).toEqual('12.345 Unit'); - }); - - it('formats 123,456,789,000 * 1000 (decimals=12)', (): void => { - expect( - formatBalance(TESTVAL.muln(1000), true, 12) - ).toEqual('123.456 Unit'); - }); - - it('formats -123,456,789,000 (decimals=15)', (): void => { - expect( - formatBalance(new BN('-123456789000'), true, 15) - ).toEqual('-123.456µ Unit'); + it('formats 123,456,789,000 (decimals=15, forceUnit=µ)', (): void => { + expect( + formatBalance(TESTVAL, { forceUnit: 'µ' }, 15) + ).toEqual('123.456µ Unit'); + }); }); describe('calcSi', (): void => { diff --git a/packages/util/src/format/formatBalance.ts b/packages/util/src/format/formatBalance.ts index 0f97f1293..660358db8 100644 --- a/packages/util/src/format/formatBalance.ts +++ b/packages/util/src/format/formatBalance.ts @@ -7,6 +7,7 @@ import { SiDef, ToBn } from '../types'; import BN from 'bn.js'; import bnToBn from '../bn/toBn'; +import isBoolean from '../is/boolean'; import isUndefined from '../is/undefined'; import formatDecimal from './formatDecimal'; import { SI, SI_MID, calcSi, findSi } from './si'; @@ -16,8 +17,10 @@ interface Defaults { unit: string; } +type Options = boolean | { forceUnit?: string; withSi?: boolean }; + interface BalanceFormatter { - (input?: number | string | BN | ExtToBn, withSi?: boolean, decimals?: number): string; + (input?: number | string | BN | ExtToBn, options?: Options, decimals?: number): string; calcSi (text: string, decimals?: number): SiDef; findSi (type: string): SiDef; getDefaults (): Defaults; @@ -32,7 +35,7 @@ let defaultDecimals = DEFAULT_DECIMALS; let defaultUnit = DEFAULT_UNIT; // Formats a string/number with . notation -function _formatBalance (input?: number | string | BN | ExtToBn, withSi = true, decimals: number = defaultDecimals): string { +function _formatBalance (input?: number | string | BN | ExtToBn, options: Options = true, decimals: number = defaultDecimals): string { let text = bnToBn(input).toString(); if (text.length === 0 || text === '0') { @@ -47,10 +50,15 @@ function _formatBalance (input?: number | string | BN | E text = text.substr(1); } + // extract options - the boolean case is for backwards-compat + const { forceUnit = undefined, withSi = true } = isBoolean(options) + ? { withSi: options } + : options; + // NOTE We start at midpoint (8) minus 1 - this means that values display as // 123.456 instead of 0.123k (so always 6 relevant). Additionally we us ceil // so there are at most 3 decimal before the decimal seperator - const si = calcSi(text, decimals); + const si = calcSi(text, decimals, forceUnit); const mid = text.length - (decimals + si.power); const prefix = text.substr(0, mid); const padding = mid < 0 ? 0 - mid : 0; diff --git a/packages/util/src/format/si.ts b/packages/util/src/format/si.ts index a97b92137..9cbb2ccb0 100644 --- a/packages/util/src/format/si.ts +++ b/packages/util/src/format/si.ts @@ -26,11 +26,6 @@ export const SI: SiDef[] = [ { power: 24, value: 'Y', text: 'Yotta' } ]; -export function calcSi (text: string, decimals: number): SiDef { - const siDefIndex = (SI_MID - 1) + Math.ceil((text.length - decimals) / 3); - return SI[siDefIndex] || SI[siDefIndex < 0 ? 0 : SI.length - 1]; -} - // Given a SI type (e.g. k, m, Y) find the SI definition export function findSi (type: string): SiDef { // use a loop here, better RN support (which doesn't have [].find) @@ -42,3 +37,13 @@ export function findSi (type: string): SiDef { return SI[SI_MID]; } + +export function calcSi (text: string, decimals: number, forceUnit?: string): SiDef { + if (forceUnit) { + return findSi(forceUnit); + } + + const siDefIndex = (SI_MID - 1) + Math.ceil((text.length - decimals) / 3); + + return SI[siDefIndex] || SI[siDefIndex < 0 ? 0 : SI.length - 1]; +} diff --git a/yarn.lock b/yarn.lock index 3eb1682f3..97d43e6dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1790,10 +1790,10 @@ dependencies: "@types/node" "^12.11.1" -"@polkadot/dev@^0.32.0-beta.11": - version "0.32.0-beta.11" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.32.0-beta.11.tgz#930332443807db0c0f692d959ca18c7a84037be1" - integrity sha512-qowTLH6OI/ujmRki5N+QpCCYlyuwpI59IP/XNOw7IhpM/6CbxCdxsdGaoELCtxEIAeRMH2IksqD3eoU5YK8LxA== +"@polkadot/dev@^0.32.0-beta.12": + version "0.32.0-beta.12" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.32.0-beta.12.tgz#0c11d980c445c51ce6262d0a113ea4ca2d105d77" + integrity sha512-jHGH5y1yuZP1ZQMrqdWFtE8jFznrD6zkDPY2m209G6dL+jTlDw/NRWyBP+zHzhXMT3JSFyw7dcBNCdmecXUnMA== dependencies: "@babel/cli" "^7.6.4" "@babel/core" "^7.6.4" @@ -1836,10 +1836,10 @@ typescript "^3.6.4" vuepress "^1.2.0" -"@polkadot/ts@^0.1.82": - version "0.1.82" - resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.82.tgz#e9c9742dcb4096358d740d1476c307595f97c9f9" - integrity sha512-7rC56/fb/ubwZ3k+zn3yf+HTmjXGy+IobDbO8+P/0REXj+MkhQp4ns3tMuIfanWZEp4S/bqwU7BvgyvvodGfCA== +"@polkadot/ts@^0.1.83": + version "0.1.83" + resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.83.tgz#9aa7cc96f66a79d6744d922707f52b62ab374143" + integrity sha512-cBIWE60GK/QtyEOpN+NmdKmBcMGdMzm5KgLjfiJaJBcSGFzI1Cd3+z1SPVwzy8Yk0bmdhxHij4lF9A9jEik9fQ== dependencies: "@types/chrome" "^0.0.91"