Adjust option extraction for helpers (#1533)
* Adjust option extraction for helpers * Adjust
This commit is contained in:
@@ -31,7 +31,7 @@ function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number
|
||||
/** @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?: boolean): HexString {
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | NumberOptions = DEFAULT_OPTS, arg2 = false): HexString {
|
||||
return !value
|
||||
? ZERO_STR
|
||||
: u8aToHex(
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { NumberOptions, ToBn } from '../types';
|
||||
import type { BN } from './bn';
|
||||
|
||||
import { isNumber } from '../is/number';
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { bnToBn } from './toBn';
|
||||
|
||||
const DEFAULT_OPTS: NumberOptions = { bitLength: -1, isLe: true, isNegative: false };
|
||||
@@ -28,13 +27,17 @@ function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number
|
||||
/** @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?: boolean): Uint8Array {
|
||||
const { bitLength, isLe, isNegative }: NumberOptions = objectSpread(
|
||||
{ bitLength: -1, isLe: true, isNegative: false },
|
||||
isNumber(arg1)
|
||||
? { bitLength: arg1, isLe: arg2 }
|
||||
: arg1
|
||||
);
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | NumberOptions = DEFAULT_OPTS, arg2 = true): Uint8Array {
|
||||
// const { bitLength, isLe, isNegative }: NumberOptions = objectSpread(
|
||||
// { bitLength: -1, isLe: true, isNegative: false },
|
||||
// isNumber(arg1)
|
||||
// ? { bitLength: arg1, isLe: arg2 }
|
||||
// : arg1
|
||||
// );
|
||||
|
||||
const { bitLength = -1, isLe = true, isNegative = false } = isNumber(arg1)
|
||||
? { bitLength: arg1, isLe: arg2 }
|
||||
: arg1;
|
||||
|
||||
const valueBn = bnToBn(value);
|
||||
const byteLength = bitLength === -1
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { ToBnOptions } from '../types';
|
||||
|
||||
import { BN } from '../bn/bn';
|
||||
import { isBoolean } from '../is/boolean';
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { hexStripPrefix } from './stripPrefix';
|
||||
|
||||
/**
|
||||
@@ -36,12 +35,9 @@ function hexToBn (value?: string | null, options: ToBnOptions | boolean = {}): B
|
||||
}
|
||||
|
||||
// For hex, default to BE
|
||||
const { isLe, isNegative } = objectSpread<ToBnOptions>(
|
||||
{ isLe: false, isNegative: false },
|
||||
isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options
|
||||
);
|
||||
const { isLe = false, isNegative = false } = isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options;
|
||||
const stripped = hexStripPrefix(value);
|
||||
const bn = new BN(stripped, 16, isLe ? 'le' : 'be');
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { objectSpread } from '.';
|
||||
|
||||
describe('objectSpread', (): void => {
|
||||
@@ -15,4 +16,6 @@ describe('objectSpread', (): void => {
|
||||
objectSpread({ a: 1 }, null, { b: 2 }, undefined, { a: 2, c: { d: 3, e: 4 } })
|
||||
).toEqual({ a: 2, b: 2, c: { d: 3, e: 4 } });
|
||||
});
|
||||
|
||||
performance('objectSpread', 500000, [[]], () => objectSpread({}, { a: 1, b: 2, c: 3 }, { d: 4, e: 7, f: 6 }));
|
||||
});
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { objectKeys } from './keys';
|
||||
|
||||
/**
|
||||
* @name objectSpread
|
||||
* @summary Concats all sources into the destination
|
||||
@@ -12,13 +10,7 @@ export function objectSpread <T extends object> (dest: object, ...sources: (obje
|
||||
const src = sources[i];
|
||||
|
||||
if (src) {
|
||||
const keys = objectKeys(src);
|
||||
|
||||
for (let j = 0; j < keys.length; j++) {
|
||||
const key = keys[j];
|
||||
|
||||
dest[key] = src[key];
|
||||
}
|
||||
Object.assign(dest, src);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import type { ToBnOptions } from '../types';
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
import { _1n } from '../bi/consts';
|
||||
import { objectSpread } from '../object/spread';
|
||||
|
||||
const U8_MAX = BigInt(256);
|
||||
const U16_MAX = BigInt(256 * 256);
|
||||
@@ -52,12 +51,11 @@ function toBigInt (input: Uint8Array): bigint {
|
||||
* @name u8aToBigInt
|
||||
* @summary Creates a BigInt from a Uint8Array object.
|
||||
*/
|
||||
export function u8aToBigInt (value: Uint8Array, options: ToBnOptions = {}): bigint {
|
||||
export function u8aToBigInt (value: Uint8Array, { isLe = true, isNegative = false }: ToBnOptions = {}): bigint {
|
||||
if (!value || !value.length) {
|
||||
return BigInt(0);
|
||||
}
|
||||
|
||||
const { isLe, isNegative } = objectSpread<ToBnOptions>({ isLe: true, isNegative: false }, options);
|
||||
const u8a = isLe
|
||||
? value.reverse()
|
||||
: value;
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { ToBnOptions } from '../types';
|
||||
|
||||
import { BN } from '../bn/bn';
|
||||
import { isBoolean } from '../is/boolean';
|
||||
import { objectSpread } from '../object/spread';
|
||||
|
||||
/**
|
||||
* @name u8aToBn
|
||||
@@ -32,12 +31,9 @@ function u8aToBn (value: Uint8Array, options?: boolean): BN;
|
||||
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, isNegative } = objectSpread<ToBnOptions>(
|
||||
{ isLe: true, isNegative: false },
|
||||
isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options
|
||||
);
|
||||
const { isLe = true, isNegative = false } = isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options;
|
||||
const count = value.length;
|
||||
let bn: BN;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user