From 4fe1b703f60cb7337ca0f7be8ea8c19076be935f Mon Sep 17 00:00:00 2001 From: fabiorigam Date: Thu, 20 Jul 2023 17:06:34 +0200 Subject: [PATCH] Fix code smells Fix code smells --- src/abi.ts | 9 +++++++-- src/mnemonic.ts | 2 +- src/rlp.ts | 4 ++-- src/secp256k1.ts | 2 +- src/transaction.ts | 10 +++++----- tests/abi.test.ts | 3 +-- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/abi.ts b/src/abi.ts index 8f0877c..62aa6f3 100644 --- a/src/abi.ts +++ b/src/abi.ts @@ -42,7 +42,7 @@ function formatSignature(fragment: any) { try { return _formatSignature(fragment) .replace(/\(tuple\(/g, '((') - .replace(/\,tuple\(/g, ',(') + .replace(/,tuple\(/g, ',(') } catch (err) { if (err.reason) { throw new Error(err.reason) @@ -260,6 +260,11 @@ export namespace abi { export type Decoded = { [name: string]: any } & { [index: number]: any } function isValueType(type: string) { - return type === 'address' || type === 'bool' || /^(u?int)([0-9]*)$/.test(type) || /^bytes([0-9]+)$/.test(type) + return ( + type === "address" || + type === "bool" || + /^(u?int)(\d*)$/.test(type) || + /^bytes(\d+)$/.test(type) + ) } } diff --git a/src/mnemonic.ts b/src/mnemonic.ts index 31830d2..94f1663 100644 --- a/src/mnemonic.ts +++ b/src/mnemonic.ts @@ -9,7 +9,7 @@ export namespace mnemonic { * Every 4 bytes produce 3 words. */ export function generate(rng?: () => Buffer) { - rng = rng || (() => randomBytes(128 / 8)) + rng = rng ?? (() => randomBytes(128 / 8)) return HD.entropyToMnemonic(rng()).split(' ') } diff --git a/src/rlp.ts b/src/rlp.ts index d8ea63a..85896ec 100644 --- a/src/rlp.ts +++ b/src/rlp.ts @@ -142,7 +142,7 @@ export namespace RLP { public data(data: string, ctx: string) { const encoder = super.data(data, ctx) - assert(data!.length === this.bytes * 2 + 2, ctx, + assert(data.length === this.bytes * 2 + 2, ctx, `expected hex string presents ${this.bytes} bytes`) return encoder } @@ -274,7 +274,7 @@ function isHexString(str: string) { } function isDecString(str: string) { - return /^[0-9]+$/.test(str) + return /^\d+$/.test(str) } class RLPError extends Error { diff --git a/src/secp256k1.ts b/src/secp256k1.ts index fb950e0..f296952 100644 --- a/src/secp256k1.ts +++ b/src/secp256k1.ts @@ -25,7 +25,7 @@ export namespace secp256k1 { * @param rng the optional random number generator, which exactly generates 32 random bytes */ export function generatePrivateKey(rng?: () => Buffer) { - rng = rng || (() => randomBytes(32)) + rng = rng ?? (() => randomBytes(32)) for (; ;) { const privKey = rng() if (isValidPrivateKey(privKey)) { diff --git a/src/transaction.ts b/src/transaction.ts index 5a9f3b0..e8bd3aa 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -142,7 +142,7 @@ export class Transaction { /** returns whether delegated. see https://github.com/vechain/VIPs/blob/master/vips/VIP-191.md */ get delegated() { // tslint:disable-next-line:no-bitwise - return (((this.body.reserved || {}).features || 0) & Transaction.DELEGATED_MASK) === Transaction.DELEGATED_MASK + return (((this.body.reserved ?? {}).features ?? 0) & Transaction.DELEGATED_MASK) === Transaction.DELEGATED_MASK } /** returns intrinsic gas it takes */ @@ -161,9 +161,9 @@ export class Transaction { } private _encodeReserved() { - const reserved = this.body.reserved || {} - const list = [featuresKind.data(reserved.features || 0, 'reserved.features').encode(), - ...(reserved.unused || [])] + const reserved = this.body.reserved ?? {} + const list = [featuresKind.data(reserved.features ?? 0, 'reserved.features').encode(), + ...(reserved.unused ?? [])] // trim while (list.length > 0) { @@ -254,7 +254,7 @@ export namespace Transaction { let sum = 0 for (let i = 2; i < data.length; i += 2) { - if (data.substr(i, 2) === '00') { + if (data.substring(i, i + 2) === "00") { sum += zgas } else { sum += nzgas diff --git a/tests/abi.test.ts b/tests/abi.test.ts index 8ac29e1..5ecaec7 100644 --- a/tests/abi.test.ts +++ b/tests/abi.test.ts @@ -1,6 +1,5 @@ import { expect } from 'chai' -import { abi } from '../src' -import { keccak256 } from '../src' +import { abi, keccak256 } from "../src" // tslint:disable:quotemark // tslint:disable:object-literal-key-quotes