diff --git a/packages/util/src/bn/toBn.spec.ts b/packages/util/src/bn/toBn.spec.ts index ad6551464..355f3e703 100644 --- a/packages/util/src/bn/toBn.spec.ts +++ b/packages/util/src/bn/toBn.spec.ts @@ -9,19 +9,40 @@ import { bnToBn } from '.'; describe('bnToBn', (): void => { it('converts null values to 0x00', (): void => { expect( - bnToBn(null) - ).toEqual(new BN(0)); + bnToBn(null).toNumber() + ).toEqual(0); }); it('converts BN values to BN', (): void => { expect( - bnToBn(new BN(128)) - ).toEqual(new BN(128)); + bnToBn(new BN(128)).toNumber() + ).toEqual(128); }); it('converts number values to BN', (): void => { expect( - bnToBn(128) - ).toEqual(new BN(128)); + bnToBn(128).toNumber() + ).toEqual(128); + }); + + it('converts string to BN', (): void => { + expect( + bnToBn('123').toNumber() + ).toEqual(123); + }); + + it('converts hex to BN', (): void => { + expect( + bnToBn('0x0123').toNumber() + ).toEqual(0x123); + }); + + it('converts Compact to BN', (): void => { + expect( + bnToBn({ + toBn: (): BN => new BN(1234), + something: 'test' + }).toNumber() + ).toEqual(1234); }); }); diff --git a/packages/util/src/bn/toBn.ts b/packages/util/src/bn/toBn.ts index fef7d1ba5..e0af27bad 100644 --- a/packages/util/src/bn/toBn.ts +++ b/packages/util/src/bn/toBn.ts @@ -2,8 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { ToBn } from '../types'; + import BN from 'bn.js'; +import isToBn from '../is/toBn'; +import isHex from '../is/hex'; +import hexToBn from '../hex/toBn'; + /** * @name bnToBn * @summary Creates a BN value from a BN.js bignumber or number input. @@ -20,12 +26,16 @@ import BN from 'bn.js'; * bnToBn(new BN(0x1234)); // => BN(0x1234) * ``` */ -export default function bnToBn (value?: BN | number | null): BN { +export default function bnToBn (value?: ExtToBn | BN | string | number | null): BN { if (!value) { return new BN(0); + } else if (isHex(value)) { + return hexToBn(value.toString()); } return BN.isBN(value) ? value - : new BN(value); + : isToBn(value) + ? value.toBn() + : new BN(value as number); } diff --git a/packages/util/src/format/formatBalance.spec.ts b/packages/util/src/format/formatBalance.spec.ts index fdee6a308..eb6b9eb3b 100644 --- a/packages/util/src/format/formatBalance.spec.ts +++ b/packages/util/src/format/formatBalance.spec.ts @@ -9,12 +9,6 @@ import formatBalance from './formatBalance'; describe('formatBalance', (): void => { const TESTVAL = new BN('123456789000'); - it('does not allow float inputs', (): void => { - expect( - (): string => formatBalance(0.01234) - ).toThrow(/integer input value/); - }); - it('formats empty to 0', (): void => { expect(formatBalance()).toEqual('0'); expect(formatBalance('0')).toEqual('0'); @@ -29,7 +23,8 @@ describe('formatBalance', (): void => { it('formats 123,456,789,000 (decimals=15, Compact)', (): void => { const compact = { toBn: (): BN => TESTVAL, - unwrap: (): BN => TESTVAL + unwrap: (): BN => TESTVAL, + something: 'else' }; expect( formatBalance(compact, true, 15) diff --git a/packages/util/src/format/formatBalance.ts b/packages/util/src/format/formatBalance.ts index 8455a7c60..b6d77ec2e 100644 --- a/packages/util/src/format/formatBalance.ts +++ b/packages/util/src/format/formatBalance.ts @@ -2,11 +2,11 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Compact } from './types'; +import { ToBn } from '../types'; import BN from 'bn.js'; -import assert from '../assert'; +import bnToBn from '../bn/toBn'; import isUndefined from '../is/undefined'; import formatDecimal from './formatDecimal'; import { SI, SI_MID, SiDef, calcSi, findSi } from './si'; @@ -17,7 +17,7 @@ interface Defaults { } interface BalanceFormatter { - (input?: number | string | BN | Compact, withSi?: boolean, decimals?: number): string; + (input?: number | string | BN | ExtToBn, withSi?: boolean, decimals?: number): string; calcSi (text: string, decimals?: number): SiDef; findSi (type: string): SiDef; getDefaults (): Defaults; @@ -32,14 +32,8 @@ let defaultDecimals = DEFAULT_DECIMALS; let defaultUnit = DEFAULT_UNIT; // Formats a string/number with . notation -function _formatBalance (input?: number | string | BN | Compact, withSi: boolean = true, decimals: number = defaultDecimals): string { - let text = ( - input - ? (input as Compact).toBn - ? (input as Compact).toBn().toString() - : input.toString() - : '' - ).toString(); +function _formatBalance (input?: number | string | BN | ExtToBn, withSi: boolean = true, decimals: number = defaultDecimals): string { + let text = bnToBn(input).toString(); if (text.length === 0 || text === '0') { return '0'; @@ -53,8 +47,6 @@ function _formatBalance (input?: number | string | BN | Compact, withSi: boolean text = text.substr(1); } - assert(/^\d+$/.test(text), `Non-integer input value '${text}' supplied to balanceFormat`); - // 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 diff --git a/packages/util/src/format/formatElapsed.spec.ts b/packages/util/src/format/formatElapsed.spec.ts index e22431e59..ecf657ab4 100644 --- a/packages/util/src/format/formatElapsed.spec.ts +++ b/packages/util/src/format/formatElapsed.spec.ts @@ -25,8 +25,7 @@ describe('formatElapsed', (): void => { it('formats a Compact', (): void => { expect( formatElapsed(now, { - toBn: (): BN => new BN(0), - unwrap: (): BN => new BN(start + (5.3 * 60000)) + toBn: (): BN => new BN(start + (5.3 * 60000)) }) ).toEqual('5m'); }); diff --git a/packages/util/src/format/formatElapsed.ts b/packages/util/src/format/formatElapsed.ts index dda7d1e40..7bdc7f4ae 100644 --- a/packages/util/src/format/formatElapsed.ts +++ b/packages/util/src/format/formatElapsed.ts @@ -2,14 +2,16 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Compact } from './types'; +import { ToBn } from '../types'; import BN from 'bn.js'; -function getValue (value?: BN | Compact | Date | number | null): number { +import isToBn from '../is/toBn'; + +function getValue (value?: BN | ExtToBn | Date | number | null): number { if (value) { - if ((value as Compact).unwrap) { - return getValue((value as Compact).unwrap()); + if (isToBn(value)) { + return getValue(value.toBn()); } else if (value instanceof Date) { return getValue(value.getTime()); } else if (value instanceof BN) { @@ -20,7 +22,7 @@ function getValue (value?: BN | Compact | Date | number | null): number { return (value as number) || 0; } -export default function formatElapsed (now?: Date | null, value?: BN | Compact | Date | number | null): string { +export default function formatElapsed (now?: Date | null, value?: BN | ExtToBn | Date | number | null): string { const tsNow = (now && now.getTime()) || 0; const tsValue = getValue(value); let display = '0.0s'; diff --git a/packages/util/src/format/formatNumber.ts b/packages/util/src/format/formatNumber.ts index 79677f12f..e3f21d574 100644 --- a/packages/util/src/format/formatNumber.ts +++ b/packages/util/src/format/formatNumber.ts @@ -2,21 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Compact } from './types'; +import { ToBn } from '../types'; import BN from 'bn.js'; import bnToBn from '../bn/toBn'; import formatDecimal from './formatDecimal'; -export default function formatNumber (_value?: Compact | BN | number | null): string { - if (!_value) { - return '0'; - } - - const value = (_value as Compact).toBn - ? (_value as Compact).toBn() - : bnToBn(_value as BN); - - return formatDecimal(value.toString()); +export default function formatNumber (value?: ExtToBn | BN | number | null): string { + return formatDecimal(bnToBn(value).toString()); } diff --git a/packages/util/src/format/types.ts b/packages/util/src/format/types.ts deleted file mode 100644 index 3fc6b4a1a..000000000 --- a/packages/util/src/format/types.ts +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2017-2019 @polkadot/util authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import BN from 'bn.js'; - -export interface Compact { - toBn: () => BN; - unwrap: () => BN | Date; -} diff --git a/packages/util/src/hex/toBn.ts b/packages/util/src/hex/toBn.ts index ee6d080f1..329dc6519 100644 --- a/packages/util/src/hex/toBn.ts +++ b/packages/util/src/hex/toBn.ts @@ -32,10 +32,7 @@ function reverse (value: string): string { * hexToBn('0x123480001f'); // => BN(0x123480001f) * ``` */ -export default function hexToBn ( - value?: string | number | null, - options: ToBnOptions | boolean = { isLe: false, isNegative: false } -): BN { +export default function hexToBn (value?: string | number | null, options: ToBnOptions | boolean = { isLe: false, isNegative: false }): BN { if (!value) { return new BN(0); } diff --git a/packages/util/src/is/index.ts b/packages/util/src/is/index.ts index d1cde591d..9b6a658ac 100644 --- a/packages/util/src/is/index.ts +++ b/packages/util/src/is/index.ts @@ -21,5 +21,6 @@ export { default as isObject } from './object'; export { default as isObservable } from './observable'; export { default as isString } from './string'; export { default as isTestChain } from './testChain'; +export { default as isToBn } from './toBn'; export { default as isU8a } from './u8a'; export { default as isUndefined } from './undefined'; diff --git a/packages/util/src/is/toBn.ts b/packages/util/src/is/toBn.ts new file mode 100644 index 000000000..11834cac7 --- /dev/null +++ b/packages/util/src/is/toBn.ts @@ -0,0 +1,12 @@ +// Copyright 2017-2019 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { ToBn } from '../types'; + +import isFunction from './function'; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export default function isToBn (value?: any): value is ToBn { + return !!value && isFunction((value as ToBn).toBn); +} diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 5e91d6395..a0861966f 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -4,6 +4,10 @@ import BN from 'bn.js'; +export interface ToBn { + toBn: () => BN; +} + export interface ExtErrorInterface { code: number; data?: string | number;