From 171015fbba0366997a79c9fa63e6df9e191cf0b3 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 17 Feb 2020 12:16:02 +0100 Subject: [PATCH] formatBalance adjustments (#536) * formatBalance extensions * Fly-by any removals * isBn updates * Remove unneeded ()'s --- CHANGELOG.md | 5 +++++ package.json | 2 +- packages/keyring/src/index.ts | 9 ++------- packages/util/src/assert.ts | 3 +-- packages/util/src/format/formatBalance.spec.ts | 18 ++++++++++++++++++ packages/util/src/format/formatBalance.ts | 12 ++++++------ packages/util/src/is/bn.ts | 3 +-- yarn.lock | 8 ++++---- 8 files changed, 38 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cca09d31e..fd00a79c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 2.4.0-beta.x + +- Ensure that the `formatBalance` no unit overrides apply when no SI is applicable +- Adjust `formatBalance` to take `withUnit: string | boolean` where string is an actual unit, e.g. `KSM` + # 2.3.1 Feb 15, 2020 - Remove dependency on moment.js with `formatDate` function (Thanks to https://github.com/AndreasGassmann) diff --git a/package.json b/package.json index 19ad87005..ef0a075e1 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ }, "devDependencies": { "@babel/core": "^7.8.4", - "@polkadot/dev": "^0.40.16", + "@polkadot/dev": "^0.40.17", "@polkadot/ts": "^0.2.3" } } diff --git a/packages/keyring/src/index.ts b/packages/keyring/src/index.ts index 372e1df35..57ae7c8d0 100644 --- a/packages/keyring/src/index.ts +++ b/packages/keyring/src/index.ts @@ -7,13 +7,8 @@ import { decodeAddress, encodeAddress, setSS58Format } from '@polkadot/util-cryp import Keyring from './keyring'; -// eslint-disable-next-line no-useless-catch -try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - detectPackage(require('./package.json'), __dirname); -} catch (error) { - throw error; -} +// eslint-disable-next-line @typescript-eslint/no-var-requires +detectPackage(require('./package.json'), __dirname); export default Keyring; diff --git a/packages/util/src/assert.ts b/packages/util/src/assert.ts index 26510641d..92b2205c6 100644 --- a/packages/util/src/assert.ts +++ b/packages/util/src/assert.ts @@ -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) diff --git a/packages/util/src/format/formatBalance.spec.ts b/packages/util/src/format/formatBalance.spec.ts index 8790647f0..ec9b2631e 100644 --- a/packages/util/src/format/formatBalance.spec.ts +++ b/packages/util/src/format/formatBalance.spec.ts @@ -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) diff --git a/packages/util/src/format/formatBalance.ts b/packages/util/src/format/formatBalance.ts index 1cb1ef0c7..0c1c49acc 100644 --- a/packages/util/src/format/formatBalance.ts +++ b/packages/util/src/format/formatBalance.ts @@ -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 (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}`; diff --git a/packages/util/src/is/bn.ts b/packages/util/src/is/bn.ts index 260187f7b..414601b31 100644 --- a/packages/util/src/is/bn.ts +++ b/packages/util/src/is/bn.ts @@ -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); } diff --git a/yarn.lock b/yarn.lock index ba2cd4fd7..6332ccc2a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2016,10 +2016,10 @@ dependencies: "@types/node" ">= 8" -"@polkadot/dev@^0.40.16": - version "0.40.16" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.40.16.tgz#45dc5ec8f791e38bed0fb4f9d8fde2a0854e488a" - integrity sha512-/vlk7E8TwnmAlLfwNE/7ePMuqSgapPcheWxnH7L+6hXNWKaAVAphjNEDq89D4DpHECxuL9pQvlwGTFM1r8/qkw== +"@polkadot/dev@^0.40.17": + version "0.40.17" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.40.17.tgz#493f4a7263797e416b12e6b735b39d428f6c0ef1" + integrity sha512-tAfUlcdOzixa6c4/5fxaXjO4cEmXpGZzx3EAIyqsQw+74FBFVR1BKEeGoGuWetTQfIWKqAMOKsFjF5O6VS+VfA== dependencies: "@babel/cli" "^7.8.4" "@babel/core" "^7.8.4"