Allow bnToBn to handle compact (or any with toBn) (#449)

This commit is contained in:
Jaco Greeff
2019-07-27 21:42:06 +02:00
committed by GitHub
parent 5e5776c614
commit 5d7ddcb708
12 changed files with 75 additions and 60 deletions
+27 -6
View File
@@ -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);
});
});
+12 -2
View File
@@ -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 <ExtToBn extends ToBn> (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);
}
@@ -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)
+5 -13
View File
@@ -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;
<ExtToBn extends ToBn> (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 <prefix>.<postfix><type> 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 <ExtToBn extends ToBn> (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
@@ -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');
});
+7 -5
View File
@@ -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 <ExtToBn extends ToBn> (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 <ExtToBn extends ToBn> (now?: Date | null, value?: BN | ExtToBn | Date | number | null): string {
const tsNow = (now && now.getTime()) || 0;
const tsValue = getValue(value);
let display = '0.0s';
+3 -11
View File
@@ -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 <ExtToBn extends ToBn> (value?: ExtToBn | BN | number | null): string {
return formatDecimal(bnToBn(value).toString());
}
-10
View File
@@ -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;
}
+1 -4
View File
@@ -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);
}
+1
View File
@@ -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';
+12
View File
@@ -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);
}
+4
View File
@@ -4,6 +4,10 @@
import BN from 'bn.js';
export interface ToBn {
toBn: () => BN;
}
export interface ExtErrorInterface {
code: number;
data?: string | number;