From 0f0b2438151f904a9c8334feb5a192504cfadf26 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 20 Jun 2022 16:06:35 +0200 Subject: [PATCH] Adjust isNull & isUndefined checks (#1554) --- packages/keyring/src/keyring.ts | 4 ++-- packages/keyring/src/pair/decode.ts | 4 ++-- packages/util-crypto/src/key/extractSuri.ts | 6 +++--- packages/util/src/array/filter.ts | 7 ++----- packages/util/src/assert.ts | 4 +--- packages/util/src/format/formatBalance.ts | 5 ++--- packages/util/src/lazy.ts | 4 +--- packages/util/src/memoize.ts | 5 ++--- packages/util/src/number/toHex.ts | 4 +--- packages/util/src/number/toU8a.ts | 4 +--- 10 files changed, 17 insertions(+), 30 deletions(-) diff --git a/packages/keyring/src/keyring.ts b/packages/keyring/src/keyring.ts index 95ac32c1c..0eaf26ca7 100644 --- a/packages/keyring/src/keyring.ts +++ b/packages/keyring/src/keyring.ts @@ -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); }; /** diff --git a/packages/keyring/src/pair/decode.ts b/packages/keyring/src/pair/decode.ts index e08fccdf0..be9045913 100644 --- a/packages/keyring/src/pair/decode.ts +++ b/packages/keyring/src/pair/decode.ts @@ -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); diff --git a/packages/util-crypto/src/key/extractSuri.ts b/packages/util-crypto/src/key/extractSuri.ts index 437425743..8a7af77bd 100644 --- a/packages/util-crypto/src/key/extractSuri.ts +++ b/packages/util-crypto/src/key/extractSuri.ts @@ -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); diff --git a/packages/util/src/array/filter.ts b/packages/util/src/array/filter.ts index cd06e4fa4..e48096995 100644 --- a/packages/util/src/array/filter.ts +++ b/packages/util/src/array/filter.ts @@ -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 (array: (T | null | undefined)[], allowNulls = true): T[] { return array.filter((v): v is T => - !isUndefined(v) && - (allowNulls || !isNull(v)) + v !== undefined && + (allowNulls || v !== null) ); } diff --git a/packages/util/src/assert.ts b/packages/util/src/assert.ts index 2932d59e4..3ba6667b0 100644 --- a/packages/util/src/assert.ts +++ b/packages/util/src/assert.ts @@ -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 (value: T | undefined | null, message: string | MessageFn): T { - assert(!isUndefined(value) && !isNull(value), message); + assert(value !== undefined && value !== null, message); return value; } diff --git a/packages/util/src/format/formatBalance.ts b/packages/util/src/format/formatBalance.ts index 69d48d797..6d6f771a1 100644 --- a/packages/util/src/format/formatBalance.ts +++ b/packages/util/src/format/formatBalance.ts @@ -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] diff --git a/packages/util/src/lazy.ts b/packages/util/src/lazy.ts index a14dc100a..d08eab68f 100644 --- a/packages/util/src/lazy.ts +++ b/packages/util/src/lazy.ts @@ -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 (result: Record | 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 { diff --git a/packages/util/src/memoize.ts b/packages/util/src/memoize.ts index e47b0068d..10b53a13a 100644 --- a/packages/util/src/memoize.ts +++ b/packages/util/src/memoize.ts @@ -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> (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> (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]; } }; diff --git a/packages/util/src/number/toHex.ts b/packages/util/src/number/toHex.ts index c11f50dc4..4e57373ee 100644 --- a/packages/util/src/number/toHex.ts +++ b/packages/util/src/number/toHex.ts @@ -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'; } diff --git a/packages/util/src/number/toU8a.ts b/packages/util/src/number/toU8a.ts index 60cc2efe0..8648666ac 100644 --- a/packages/util/src/number/toU8a.ts +++ b/packages/util/src/number/toU8a.ts @@ -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)