Add objectClear & objectSpread (#1157)

* Add objectClear & objectSpread

* Allow null/undefined as a source
This commit is contained in:
Jaco
2021-11-04 08:16:55 +02:00
committed by GitHub
parent 08ac55d7cd
commit 733d7c1948
13 changed files with 105 additions and 35 deletions
+9 -8
View File
@@ -5,6 +5,7 @@ import type { HexString, ToBn, ToBnOptions } 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';
@@ -36,14 +37,14 @@ function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number
return ZERO_STR;
}
const _options = {
isLe: false,
isNegative: false,
// Backwards-compatibility
...(isNumber(arg1) ? { bitLength: arg1, isLe: arg2 } : arg1)
};
return u8aToHex(bnToU8a(value, _options));
return u8aToHex(
bnToU8a(value, objectSpread(
{ isLe: false, isNegative: false },
isNumber(arg1)
? { bitLength: arg1, isLe: arg2 }
: arg1
))
);
}
export { bnToHex };
+7 -6
View File
@@ -5,6 +5,7 @@ import type { ToBn, ToBnOptions } from '../types';
import type { BN } from './bn';
import { isNumber } from '../is/number';
import { objectSpread } from '../object/spread';
import { bnToBn } from './toBn';
interface Options extends ToBnOptions {
@@ -43,12 +44,12 @@ function createValue (valueBn: BN, byteLength: number, { isLe, isNegative }: Opt
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: Options): Uint8Array;
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false }, arg2?: boolean): Uint8Array {
const options: Options = {
bitLength: -1,
isLe: true,
isNegative: false,
...isNumber(arg1) ? { bitLength: arg1, isLe: arg2 } : arg1
};
const options: Options = objectSpread(
{ bitLength: -1, isLe: true, isNegative: false },
isNumber(arg1)
? { bitLength: arg1, isLe: arg2 }
: arg1
);
const valueBn = bnToBn(value);
const byteLength = options.bitLength === -1