diff --git a/packages/util/src/bn/toHex.ts b/packages/util/src/bn/toHex.ts index cc3dc81dc..9e593df31 100644 --- a/packages/util/src/bn/toHex.ts +++ b/packages/util/src/bn/toHex.ts @@ -31,7 +31,7 @@ function bnToHex (value?: ExtToBn | BN | bigint | number /** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ function bnToHex (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString; /** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ -function bnToHex (value?: ExtToBn | BN | bigint | number | null, arg1: number | NumberOptions = DEFAULT_OPTS, arg2?: boolean): HexString { +function bnToHex (value?: ExtToBn | BN | bigint | number | null, arg1: number | NumberOptions = DEFAULT_OPTS, arg2 = false): HexString { return !value ? ZERO_STR : u8aToHex( diff --git a/packages/util/src/bn/toU8a.ts b/packages/util/src/bn/toU8a.ts index a1c597014..41360cc6c 100644 --- a/packages/util/src/bn/toU8a.ts +++ b/packages/util/src/bn/toU8a.ts @@ -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 (value?: ExtToBn | BN | bigint | number /** @deprecated Use bnToU8a(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ function bnToU8a (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): Uint8Array; /** @deprecated Use bnToU8a(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */ -function bnToU8a (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 (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 diff --git a/packages/util/src/hex/toBn.ts b/packages/util/src/hex/toBn.ts index cddce9ef6..fc9c6ca8e 100644 --- a/packages/util/src/hex/toBn.ts +++ b/packages/util/src/hex/toBn.ts @@ -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( - { 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'); diff --git a/packages/util/src/object/spread.spec.ts b/packages/util/src/object/spread.spec.ts index 53d055883..df235663b 100644 --- a/packages/util/src/object/spread.spec.ts +++ b/packages/util/src/object/spread.spec.ts @@ -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 })); }); diff --git a/packages/util/src/object/spread.ts b/packages/util/src/object/spread.ts index 6d7d8f484..7a00aefb5 100644 --- a/packages/util/src/object/spread.ts +++ b/packages/util/src/object/spread.ts @@ -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 (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); } } diff --git a/packages/util/src/u8a/toBigInt.ts b/packages/util/src/u8a/toBigInt.ts index 02aeeb539..bcf7f2d53 100644 --- a/packages/util/src/u8a/toBigInt.ts +++ b/packages/util/src/u8a/toBigInt.ts @@ -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({ isLe: true, isNegative: false }, options); const u8a = isLe ? value.reverse() : value; diff --git a/packages/util/src/u8a/toBn.ts b/packages/util/src/u8a/toBn.ts index bc0383013..12b9e6656 100644 --- a/packages/util/src/u8a/toBn.ts +++ b/packages/util/src/u8a/toBn.ts @@ -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( - { 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;