Small util return adjustments (#1351)
This commit is contained in:
@@ -21,8 +21,8 @@ import { isUndefined } from '../is/undefined';
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function arrayFilter <T = any> (array: T[], allowNulls = true): T[] {
|
||||
return array.filter((value) =>
|
||||
!isUndefined(value) &&
|
||||
(allowNulls || !isNull(value))
|
||||
return array.filter((v) =>
|
||||
!isUndefined(v) &&
|
||||
(allowNulls || !isNull(v))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,15 +19,13 @@ const ZERO_STR = '0x00';
|
||||
* @summary Creates a hex value from a bigint object.
|
||||
*/
|
||||
export function nToHex <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString {
|
||||
if (!value) {
|
||||
return ZERO_STR;
|
||||
}
|
||||
|
||||
return u8aToHex(
|
||||
nToU8a(value, objectSpread(
|
||||
// We spread here, the default for hex values is BE (JSONRPC via substrate)
|
||||
{ isLe: false, isNegative: false },
|
||||
options
|
||||
))
|
||||
);
|
||||
return !value
|
||||
? ZERO_STR
|
||||
: u8aToHex(
|
||||
nToU8a(value, objectSpread(
|
||||
// We spread here, the default for hex values is BE (JSONRPC via substrate)
|
||||
{ isLe: false, isNegative: false },
|
||||
options
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -34,19 +34,17 @@ const DEFAULT_OPTS: Options = { bitLength: -1, isLe: false, isNegative: false };
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString;
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString;
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = DEFAULT_OPTS, arg2?: boolean): HexString {
|
||||
if (!value) {
|
||||
return ZERO_STR;
|
||||
}
|
||||
|
||||
return 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
|
||||
))
|
||||
);
|
||||
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
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
export { bnToHex };
|
||||
|
||||
@@ -7,6 +7,7 @@ import { objectSpread } from './object/spread';
|
||||
|
||||
const HRS = 60 * 60;
|
||||
const DAY = HRS * 24;
|
||||
const ZERO: Time = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
|
||||
|
||||
/**
|
||||
* @name addTime
|
||||
@@ -23,8 +24,6 @@ function addTime (a: Time, b: Time): Time {
|
||||
};
|
||||
}
|
||||
|
||||
const ZERO: Time = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
|
||||
|
||||
function extractDays (milliseconds: number, hrs: number): Time {
|
||||
const days = Math.floor(hrs / 24);
|
||||
|
||||
@@ -80,11 +79,9 @@ function extractSecs (milliseconds: number): Time {
|
||||
* ```
|
||||
*/
|
||||
export function extractTime (milliseconds?: number): Time {
|
||||
if (!milliseconds) {
|
||||
return ZERO;
|
||||
} else if (milliseconds < 1000) {
|
||||
return objectSpread({}, ZERO, { milliseconds });
|
||||
}
|
||||
|
||||
return extractSecs(milliseconds);
|
||||
return !milliseconds
|
||||
? ZERO
|
||||
: milliseconds < 1000
|
||||
? objectSpread({}, ZERO, { milliseconds })
|
||||
: extractSecs(milliseconds);
|
||||
}
|
||||
|
||||
@@ -20,9 +20,7 @@ import { hexHasPrefix } from './hasPrefix';
|
||||
* ```
|
||||
*/
|
||||
export function hexAddPrefix (value?: string | null): HexString {
|
||||
if (value && hexHasPrefix(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`;
|
||||
return value && hexHasPrefix(value)
|
||||
? value
|
||||
: `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`;
|
||||
}
|
||||
|
||||
@@ -14,12 +14,10 @@ import { hexToU8a } from './toU8a';
|
||||
* @summary Creates a BigInt instance object from a hex string.
|
||||
*/
|
||||
export function hexToBigInt (value?: string | null, options: ToBnOptions = {}): bigint {
|
||||
if (!value || value === '0x') {
|
||||
return BigInt(0);
|
||||
}
|
||||
|
||||
return u8aToBigInt(
|
||||
hexToU8a(value),
|
||||
objectSpread({ isLe: false, isNegative: false }, options)
|
||||
);
|
||||
return !value || value === '0x'
|
||||
? BigInt(0)
|
||||
: u8aToBigInt(
|
||||
hexToU8a(value),
|
||||
objectSpread({ isLe: false, isNegative: false }, options)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -21,11 +21,9 @@ import { numberToHex } from './toHex';
|
||||
* ```
|
||||
*/
|
||||
export function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
|
||||
if (isUndefined(value) || isNull(value) || isNaN(value)) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
return hexToU8a(
|
||||
numberToHex(value, bitLength)
|
||||
);
|
||||
return isUndefined(value) || isNull(value) || isNaN(value)
|
||||
? new Uint8Array()
|
||||
: hexToU8a(
|
||||
numberToHex(value, bitLength)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { u8aEmpty } from '.';
|
||||
|
||||
const ztest = new Uint8Array(32);
|
||||
|
||||
describe('u8aEmpty', (): void => {
|
||||
it('returns true on zero length', (): void => {
|
||||
expect(
|
||||
@@ -21,4 +24,6 @@ describe('u8aEmpty', (): void => {
|
||||
u8aEmpty(new Uint8Array([0, 0, 0, 0, 0, 1]))
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
performance('u8aEmpty (32 cmp)', 1000000, [[ztest]], u8aEmpty);
|
||||
});
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
* Checks to see if the input `Uint8Array` has zero length or contains all 0 values.
|
||||
*/
|
||||
export function u8aEmpty (value: Uint8Array): boolean {
|
||||
// on smaller values < 64 bytes, the byte-by-byte compare is faster than
|
||||
// allocating yet another object for DataView (on large buffers the DataView
|
||||
// is much faster)
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (value[i]) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user