Remove deprecated call signatures (#1584)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Remove deprecated signatures for `bnToHex`, `bnToU8a`, `formatBalance`, `hexToBn`, `u8aToBn`
|
||||
- Allow for usage without `@types/node` installed
|
||||
- Adjust for available ESM-only dependencies
|
||||
|
||||
|
||||
@@ -18,16 +18,10 @@ describe('bnToHex', (): void => {
|
||||
|
||||
it('converts BN values to a prefixed hex representation (bitLength)', (): void => {
|
||||
expect(
|
||||
bnToHex(new BN(128), 16)
|
||||
bnToHex(new BN(128), { bitLength: 16 })
|
||||
).toBe('0x0080');
|
||||
});
|
||||
|
||||
it('converts BN values to a prefixed hex representation (bitLength + le)', (): void => {
|
||||
expect(
|
||||
bnToHex(new BN(128), 16, true)
|
||||
).toBe('0x8000');
|
||||
});
|
||||
|
||||
it('converts BN values to a prefixed hex representation (LE)', (): void => {
|
||||
expect(
|
||||
bnToHex(new BN(128), { bitLength: 16, isLe: true })
|
||||
@@ -45,12 +39,4 @@ describe('bnToHex', (): void => {
|
||||
bnToHex(new BN(-1234), { bitLength: 32, isNegative: true })
|
||||
).toBe('0xfffffb2e');
|
||||
});
|
||||
|
||||
it('handles legacy compatibility', (): void => {
|
||||
expect(
|
||||
bnToHex(new BN(1234), 32)
|
||||
).toBe(
|
||||
bnToHex(new BN(1234), { bitLength: 32 })
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,13 +4,10 @@
|
||||
import type { HexString, NumberOptions, ToBn } from '../types';
|
||||
import type { BN } from './bn';
|
||||
|
||||
import { isNumber } from '../is/number';
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { bnToU8a } from './toU8a';
|
||||
|
||||
const ZERO_STR = '0x00';
|
||||
const DEFAULT_OPTS: NumberOptions = { bitLength: -1, isLe: false, isNegative: false };
|
||||
|
||||
/**
|
||||
* @name bnToHex
|
||||
@@ -27,22 +24,8 @@ const DEFAULT_OPTS: NumberOptions = { bitLength: -1, isLe: false, isNegative: fa
|
||||
* bnToHex(new BN(0x123456)); // => '0x123456'
|
||||
* ```
|
||||
*/
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions): HexString;
|
||||
/** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString;
|
||||
/** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | NumberOptions = DEFAULT_OPTS, arg2 = false): HexString {
|
||||
export function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = false, isNegative = false }: NumberOptions = {}): HexString {
|
||||
return !value
|
||||
? ZERO_STR
|
||||
: u8aToHex(
|
||||
bnToU8a(value, objectSpread(
|
||||
// We spread here, the default for hex values is BE (JSONRPC via substrate)
|
||||
{ isLe: false, isNegative: false },
|
||||
isNumber(arg1)
|
||||
? { bitLength: arg1, isLe: arg2 }
|
||||
: arg1
|
||||
))
|
||||
);
|
||||
: u8aToHex(bnToU8a(value, { bitLength, isLe, isNegative }));
|
||||
}
|
||||
|
||||
export { bnToHex };
|
||||
|
||||
@@ -10,42 +10,34 @@ const ptest = arrayRange(65536).map((v) => [v]);
|
||||
describe('bnToU8a', (): void => {
|
||||
it('converts null values to 0x00', (): void => {
|
||||
expect(
|
||||
bnToU8a(null, -1, false)
|
||||
bnToU8a(null)
|
||||
).toEqual(new Uint8Array());
|
||||
});
|
||||
|
||||
it('converts null values to 0x00000000 (bitLength)', (): void => {
|
||||
expect(
|
||||
bnToU8a(null, 32, false)
|
||||
bnToU8a(null, { bitLength: 32 })
|
||||
).toEqual(new Uint8Array([0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('converts BN values to a prefixed hex representation', (): void => {
|
||||
expect(
|
||||
bnToU8a(new BN(0x123456), -1, false)
|
||||
bnToU8a(new BN(0x123456), { isLe: false })
|
||||
).toEqual(new Uint8Array([0x12, 0x34, 0x56]));
|
||||
});
|
||||
|
||||
it('converts BN values to a prefixed hex representation (bitLength)', (): void => {
|
||||
expect(
|
||||
bnToU8a(new BN(0x123456), 32, false)
|
||||
bnToU8a(new BN(0x123456), { bitLength: 32, isLe: false })
|
||||
).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56]));
|
||||
});
|
||||
|
||||
it('converts using little endian (as set)', (): void => {
|
||||
expect(
|
||||
bnToU8a(new BN(0x123456), 32, true)
|
||||
bnToU8a(new BN(0x123456), { bitLength: 32, isLe: true })
|
||||
).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00]));
|
||||
});
|
||||
|
||||
it('handles backwards compatibility', (): void => {
|
||||
expect(
|
||||
bnToU8a(new BN(1234), 32, false)
|
||||
).toEqual(
|
||||
bnToU8a(new BN(1234), { bitLength: 32, isLe: false })
|
||||
);
|
||||
});
|
||||
|
||||
describe('signed', (): void => {
|
||||
it('converts negative numbers (BE)', (): void => {
|
||||
expect(
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import type { NumberOptions, ToBn } from '../types';
|
||||
import type { BN } from './bn';
|
||||
|
||||
import { isNumber } from '../is/number';
|
||||
import { bnToBn } from './toBn';
|
||||
|
||||
const DEFAULT_OPTS: NumberOptions = { bitLength: -1, isLe: true, isNegative: false };
|
||||
@@ -23,15 +22,7 @@ const DEFAULT_OPTS: NumberOptions = { bitLength: -1, isLe: true, isNegative: fal
|
||||
* bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
|
||||
* ```
|
||||
*/
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions): Uint8Array;
|
||||
/** @deprecated Use bnToU8a(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
|
||||
/** @deprecated Use bnToU8a(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | NumberOptions = DEFAULT_OPTS, arg2 = true): Uint8Array {
|
||||
const { bitLength = -1, isLe = true, isNegative = false } = isNumber(arg1)
|
||||
? { bitLength: arg1, isLe: arg2 }
|
||||
: arg1;
|
||||
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = true, isNegative = false } = DEFAULT_OPTS): Uint8Array {
|
||||
const valueBn = bnToBn(value);
|
||||
const byteLength = bitLength === -1
|
||||
? Math.ceil(valueBn.bitLength() / 8)
|
||||
|
||||
@@ -33,16 +33,10 @@ describe('formatBalance', (): void => {
|
||||
|
||||
it('formats 123,456,789,000 (decimals=15, old style)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: true }, 15)
|
||||
formatBalance(TESTVAL, { decimals: 15, withSi: true })
|
||||
).toEqual('123.4567 µUnit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=36, option)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { decimals: 36, withSi: true })
|
||||
).toEqual('0.1234 yUnit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=15, Compact)', (): void => {
|
||||
const compact = {
|
||||
something: 'else',
|
||||
@@ -63,85 +57,85 @@ describe('formatBalance', (): void => {
|
||||
|
||||
it('formats 123,456,789,000 (decimals=9)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: true }, 9)
|
||||
formatBalance(TESTVAL, { decimals: 9, withSi: true })
|
||||
).toEqual('123.4567 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=9, no unit)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: true, withUnit: false }, 9)
|
||||
formatBalance(TESTVAL, { decimals: 9, withSi: true, withUnit: false })
|
||||
).toEqual('123.4567');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=9, unit given)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: true, withUnit: 'FOO' }, 9)
|
||||
formatBalance(TESTVAL, { decimals: 9, withSi: true, withUnit: 'FOO' })
|
||||
).toEqual('123.4567 FOO');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=12, no SI)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: false }, 12)
|
||||
formatBalance(TESTVAL, { decimals: 12, withSi: false })
|
||||
).toEqual('123.4567');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=12, full SI)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSiFull: true }, 12)
|
||||
formatBalance(TESTVAL, { decimals: 12, withSiFull: true })
|
||||
).toEqual('123.4567 milli Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=12, full SI, no unit)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSiFull: true, withUnit: false }, 12)
|
||||
formatBalance(TESTVAL, { decimals: 12, withSiFull: true, withUnit: false })
|
||||
).toEqual('123.4567 milli');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=9, full SI)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSiFull: true }, 9)
|
||||
formatBalance(TESTVAL, { decimals: 9, withSiFull: true })
|
||||
).toEqual('123.4567 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=6)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: true }, 6)
|
||||
formatBalance(TESTVAL, { decimals: 6, withSi: true })
|
||||
).toEqual('123.4567 kUnit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=6, unit specified)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: true, withUnit: 'BAR' }, 6)
|
||||
formatBalance(TESTVAL, { decimals: 6, withSi: true, withUnit: 'BAR' })
|
||||
).toEqual('123.4567 kBAR');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=0, unit specified)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: true, withUnit: 'BAR' }, 0)
|
||||
formatBalance(TESTVAL, { decimals: 0, withSi: true, withUnit: 'BAR' })
|
||||
).toEqual('123.4567 BBAR');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 10 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(10), { withSi: true }, 12)
|
||||
formatBalance(TESTVAL.muln(10), { decimals: 12, withSi: true })
|
||||
).toEqual('1.2345 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 100 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(100), { withSi: true }, 12)
|
||||
formatBalance(TESTVAL.muln(100), { decimals: 12, withSi: true })
|
||||
).toEqual('12.3456 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 1000 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(1000), { withSi: true }, 12)
|
||||
formatBalance(TESTVAL.muln(1000), { decimals: 12, withSi: true })
|
||||
).toEqual('123.4567 Unit');
|
||||
});
|
||||
|
||||
it('formats -123,456,789,000 (decimals=15)', (): void => {
|
||||
expect(
|
||||
formatBalance(new BN('-123456789000'), { withSi: true }, 15)
|
||||
formatBalance(new BN('-123456789000'), { decimals: 15, withSi: true })
|
||||
).toEqual('-123.4567 µUnit');
|
||||
});
|
||||
});
|
||||
@@ -149,25 +143,25 @@ describe('formatBalance', (): void => {
|
||||
describe('Forced formatting', (): void => {
|
||||
it('formats 123,456,789,000 (decimals=12, forceUnit=base)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { forceUnit: '-' }, 12)
|
||||
formatBalance(TESTVAL, { decimals: 12, forceUnit: '-' })
|
||||
).toEqual('0.1234 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=9, forceUnit=base)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { forceUnit: '-' }, 9)
|
||||
formatBalance(TESTVAL, { decimals: 9, forceUnit: '-' })
|
||||
).toEqual('123.4567 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=7, forceUnit=base)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { forceUnit: '-' }, 7)
|
||||
formatBalance(TESTVAL, { decimals: 7, forceUnit: '-' })
|
||||
).toEqual('12,345.6789 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=15, forceUnit=µ)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { forceUnit: 'µ' }, 15)
|
||||
formatBalance(TESTVAL, { decimals: 15, forceUnit: 'µ' })
|
||||
).toEqual('123.4567 µUnit');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -44,10 +44,6 @@ interface Options {
|
||||
|
||||
interface BalanceFormatter {
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, options?: Options): string;
|
||||
/** @deprecated Use balanceFormat(input?: number | string | BN | bigint | ExtToBn, options?: Options) */
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, options?: boolean, decimals?: number): string;
|
||||
/** @deprecated Use balanceFormat(input?: number | string | BN | bigint | ExtToBn, options?: Options) */
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, options?: Options, decimals?: number): string;
|
||||
calcSi (text: string, decimals?: number): SiDef;
|
||||
findSi (type: string): SiDef;
|
||||
getDefaults (): Defaults;
|
||||
@@ -89,18 +85,13 @@ function getPrePost (text: string, decimals: number, forceUnit?: string): [SiDef
|
||||
}
|
||||
|
||||
// Formats a string/number with <prefix>.<postfix><type> notation
|
||||
function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, options: Options | boolean = true, optDecimals: number = defaultDecimals): string {
|
||||
function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, { decimals = defaultDecimals, forceUnit, withSi = true, withSiFull = false, withUnit = true }: Options = {}): string {
|
||||
let text = bnToBn(input).toString();
|
||||
|
||||
if (text.length === 0 || text === '0') {
|
||||
return '0';
|
||||
}
|
||||
|
||||
// extract options - the boolean case is for backwards-compat
|
||||
const { decimals = optDecimals, forceUnit = undefined, withSi = true, withSiFull = false, withUnit = true } = isBoolean(options)
|
||||
? { withSi: options }
|
||||
: options;
|
||||
|
||||
// strip the negative sign so we can work with clean groupings, re-add this in the
|
||||
// end when we return the result (from here on we work with positive numbers)
|
||||
let sign = '';
|
||||
|
||||
@@ -69,17 +69,4 @@ describe('hexToBn', (): void => {
|
||||
hexToBn('0x0001000000000000', { isLe: true }).toNumber()
|
||||
).toBe(256);
|
||||
});
|
||||
|
||||
it('handles legacy compatibility', (): void => {
|
||||
expect(
|
||||
hexToBn('0x0001000000000000', false).eq(
|
||||
hexToBn('0x0001000000000000', { isLe: false })
|
||||
)
|
||||
).toBe(true);
|
||||
expect(
|
||||
hexToBn('0x0001000000000000', true).eq(
|
||||
hexToBn('0x0001000000000000', { isLe: true })
|
||||
)
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import type { ToBnOptions } from '../types';
|
||||
|
||||
import { BN } from '../bn/bn';
|
||||
import { isBoolean } from '../is/boolean';
|
||||
import { hexStripPrefix } from './stripPrefix';
|
||||
|
||||
/**
|
||||
@@ -25,19 +24,11 @@ import { hexStripPrefix } from './stripPrefix';
|
||||
* hexToBn('0x123480001f'); // => BN(0x123480001f)
|
||||
* ```
|
||||
*/
|
||||
function hexToBn (value?: string | null, options?: ToBnOptions): BN;
|
||||
/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
|
||||
function hexToBn (value?: string | null, options?: boolean): BN;
|
||||
/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
|
||||
function hexToBn (value?: string | null, options: ToBnOptions | boolean = {}): BN {
|
||||
function hexToBn (value?: string | null, { isLe = false, isNegative = false }: ToBnOptions = {}): BN {
|
||||
if (!value || value === '0x') {
|
||||
return new BN(0);
|
||||
}
|
||||
|
||||
// For hex, default to BE
|
||||
const { isLe = false, isNegative = false } = isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options;
|
||||
const stripped = hexStripPrefix(value);
|
||||
const bn = new BN(stripped, 16, isLe ? 'le' : 'be');
|
||||
|
||||
|
||||
@@ -5,21 +5,6 @@ import { perf } from '../test/performance';
|
||||
import { u8aToBn } from '.';
|
||||
|
||||
describe('u8aToBn', (): void => {
|
||||
it('supports legacy compatibility', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0x12, 0x34]),
|
||||
true
|
||||
).toString(16)
|
||||
).toBe('3412');
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0x12, 0x34]),
|
||||
false
|
||||
).toString(16)
|
||||
).toBe('1234');
|
||||
});
|
||||
|
||||
it('converts little-endian by default', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
@@ -274,31 +259,6 @@ describe('u8aToBn', (): void => {
|
||||
).toBe(256);
|
||||
});
|
||||
|
||||
it('handles backward compatibility)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]),
|
||||
false
|
||||
).eq(
|
||||
u8aToBn(
|
||||
new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]),
|
||||
{ isLe: false }
|
||||
)
|
||||
)
|
||||
).toBe(true);
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]),
|
||||
true
|
||||
).eq(
|
||||
u8aToBn(
|
||||
new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]),
|
||||
{ isLe: true }
|
||||
)
|
||||
)
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
perf('u8aToBn (u32)', 1_000_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
||||
perf('u8aToBn (i32)', 1_000_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], (v: Uint8Array) => u8aToBn(v, { isNegative: true }));
|
||||
perf('u8aToBn (u64)', 500_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import type { ToBnOptions } from '../types';
|
||||
|
||||
import { BN } from '../bn/bn';
|
||||
import { isBoolean } from '../is/boolean';
|
||||
|
||||
/**
|
||||
* @name u8aToBn
|
||||
@@ -24,16 +23,7 @@ import { isBoolean } from '../is/boolean';
|
||||
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
* ```
|
||||
*/
|
||||
function u8aToBn (value: Uint8Array, options?: ToBnOptions): BN;
|
||||
/** @deprecated Use u8aToBn(value: Uint8Array, options?: ToBnOptions) */
|
||||
function u8aToBn (value: Uint8Array, options?: boolean): BN;
|
||||
/** @deprecated Use u8aToBn (value?: string | null, options?: ToBnOptions) */
|
||||
function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = {}): BN {
|
||||
// NOTE: This is the same process as followed in the hexToBn conversion
|
||||
// For Uint8Array, default to LE
|
||||
const { isLe = true, isNegative = false } = isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options;
|
||||
function u8aToBn (value: Uint8Array, { isLe = true, isNegative = false }: ToBnOptions = {}): BN {
|
||||
const count = value.length;
|
||||
|
||||
// shortcut for <= u48 values - in this case the manual conversion
|
||||
|
||||
Reference in New Issue
Block a user