Adjust isNull & isUndefined checks (#1554)

This commit is contained in:
Jaco
2022-06-20 17:06:35 +03:00
committed by GitHub
parent 29f53fe906
commit 0f0b243815
10 changed files with 17 additions and 30 deletions
+2 -2
View File
@@ -4,7 +4,7 @@
import type { EncryptedJsonEncoding, Keypair, KeypairType } from '@polkadot/util-crypto/types';
import type { KeyringInstance, KeyringOptions, KeyringPair, KeyringPair$Json, KeyringPair$Meta } from './types';
import { hexToU8a, isHex, isUndefined, stringToU8a } from '@polkadot/util';
import { hexToU8a, isHex, stringToU8a } from '@polkadot/util';
import { base64Decode, decodeAddress, ed25519PairFromSeed as ed25519FromSeed, encodeAddress, ethereumEncode, hdEthereum, keyExtractSuri, keyFromPath, mnemonicToLegacySeed, mnemonicToMiniSecret, secp256k1PairFromSeed as secp256k1FromSeed, sr25519PairFromSeed as sr25519FromSeed } from '@polkadot/util-crypto';
import { DEV_PHRASE } from './defaults';
@@ -246,7 +246,7 @@ export class Keyring implements KeyringInstance {
public encodeAddress = (address: Uint8Array | string, ss58Format?: number): string => {
return this.type === 'ethereum'
? ethereumEncode(address)
: encodeAddress(address, isUndefined(ss58Format) ? this.#ss58 : ss58Format);
: encodeAddress(address, ss58Format === undefined ? this.#ss58 : ss58Format);
};
/**
+2 -2
View File
@@ -4,7 +4,7 @@
import type { EncryptedJsonEncoding } from '@polkadot/util-crypto/types';
import type { PairInfo } from './types';
import { assert, isUndefined, u8aEq } from '@polkadot/util';
import { assert, u8aEq } from '@polkadot/util';
import { jsonDecryptData } from '@polkadot/util-crypto';
import { PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH } from './defaults';
@@ -16,7 +16,7 @@ type DecodeResult = PairInfo & {
};
export function decodePair (passphrase?: string, encrypted?: Uint8Array | null, _encType?: EncryptedJsonEncoding | EncryptedJsonEncoding[]): DecodeResult {
const encType = Array.isArray(_encType) || isUndefined(_encType)
const encType = Array.isArray(_encType) || _encType === undefined
? _encType
: [_encType];
const decrypted = jsonDecryptData(encrypted, passphrase, encType);
+3 -3
View File
@@ -1,8 +1,6 @@
// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert, isNull } from '@polkadot/util';
import { DeriveJunction } from './DeriveJunction';
import { keyExtractPath } from './extractPath';
@@ -22,7 +20,9 @@ export function keyExtractSuri (suri: string): ExtractResult {
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
const matches = suri.match(RE_CAPTURE);
assert(!isNull(matches), 'Unable to match provided value to a secret URI');
if (matches === null) {
throw new Error('Unable to match provided value to a secret URI');
}
const [, phrase, , derivePath, , , password] = matches as string[];
const { path } = keyExtractPath(derivePath);
+2 -5
View File
@@ -1,9 +1,6 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isNull } from '../is/null';
import { isUndefined } from '../is/undefined';
/**
* @name arrayFilter
* @summary Filters undefined and (optionally) null values from an array
@@ -21,7 +18,7 @@ import { isUndefined } from '../is/undefined';
*/
export function arrayFilter <T = unknown> (array: (T | null | undefined)[], allowNulls = true): T[] {
return array.filter((v): v is T =>
!isUndefined(v) &&
(allowNulls || !isNull(v))
v !== undefined &&
(allowNulls || v !== null)
);
}
+1 -3
View File
@@ -2,8 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
import { isFunction } from './is/function';
import { isNull } from './is/null';
import { isUndefined } from './is/undefined';
type MessageFn = () => string;
@@ -38,7 +36,7 @@ export function assert (condition: unknown, message: string | MessageFn): assert
* @description Returns when the value is not undefined/null, otherwise throws assertion error
*/
export function assertReturn <T> (value: T | undefined | null, message: string | MessageFn): T {
assert(!isUndefined(value) && !isNull(value), message);
assert(value !== undefined && value !== null, message);
return value;
}
+2 -3
View File
@@ -6,7 +6,6 @@ import type { SiDef, ToBn } from '../types';
import { bnToBn } from '../bn/toBn';
import { isBoolean } from '../is/boolean';
import { isUndefined } from '../is/undefined';
import { formatDecimal } from './formatDecimal';
import { calcSi, findSi, SI, SI_MID } from './si';
@@ -147,12 +146,12 @@ formatBalance.getOptions = (decimals: number = defaultDecimals): SiDef[] => {
// Sets the default decimals to use for formatting (ui-wide)
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.setDefaults = ({ decimals, unit }: SetDefaults): void => {
defaultDecimals = isUndefined(decimals)
defaultDecimals = decimals === undefined
? defaultDecimals
: Array.isArray(decimals)
? decimals[0]
: decimals;
defaultUnit = isUndefined(unit)
defaultUnit = unit === undefined
? defaultUnit
: Array.isArray(unit)
? unit[0]
+1 -3
View File
@@ -1,8 +1,6 @@
// Copyright 2017-2022 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isUndefined } from './is/undefined';
type AnyFn = (...args: unknown[]) => unknown;
type WithToString = { toString: () => string };
@@ -31,7 +29,7 @@ export function lazyMethod <T, K> (result: Record<string, T> | AnyFn, item: K, c
// This check should _always_ be false and unneeded, since we override
// with a value below ... however we ensure we are quire vigilant against
// all environment failures, so we are rather be safe than sorry
if (isUndefined(value)) {
if (value === undefined) {
value = creator(item);
try {
+2 -3
View File
@@ -3,7 +3,6 @@
import type { Memoized } from './types';
import { isUndefined } from './is/undefined';
import { stringify } from './stringify';
interface Options {
@@ -30,7 +29,7 @@ export function memoize <T, F extends (...args: any[]) => T> (fn: F, { getInstan
cache[instanceId] = {};
}
if (isUndefined(cache[instanceId][stringParams])) {
if (cache[instanceId][stringParams] === undefined) {
cache[instanceId][stringParams] = fn(...args);
}
@@ -41,7 +40,7 @@ export function memoize <T, F extends (...args: any[]) => T> (fn: F, { getInstan
const stringParams = stringify(args);
const instanceId = getInstanceId();
if (cache[instanceId] && !isUndefined(cache[instanceId][stringParams])) {
if (cache[instanceId] && cache[instanceId][stringParams] !== undefined) {
delete cache[instanceId][stringParams];
}
};
+1 -3
View File
@@ -4,8 +4,6 @@
import type { HexString } from '../types';
import { hexFixLength } from '../hex/fixLength';
import { isNull } from '../is/null';
import { isUndefined } from '../is/undefined';
/**
* @name numberToHex
@@ -23,7 +21,7 @@ import { isUndefined } from '../is/undefined';
* ```
*/
export function numberToHex (value?: number | null, bitLength = -1): HexString {
if (isUndefined(value) || isNull(value) || isNaN(value)) {
if (value === undefined || value === null || isNaN(value)) {
return '0x';
}
+1 -3
View File
@@ -2,8 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
import { hexToU8a } from '../hex/toU8a';
import { isNull } from '../is/null';
import { isUndefined } from '../is/undefined';
import { numberToHex } from './toHex';
/**
@@ -21,7 +19,7 @@ import { numberToHex } from './toHex';
* ```
*/
export function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
return isUndefined(value) || isNull(value) || isNaN(value)
return value === undefined || value === null || isNaN(value)
? new Uint8Array()
: hexToU8a(
numberToHex(value, bitLength)