Skip to content

Commit

Permalink
Fix code smells
Browse files Browse the repository at this point in the history
Fix code smells
  • Loading branch information
fabiorigam authored and libotony committed Aug 4, 2023
1 parent 5ebb9a7 commit 4fe1b70
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
)
}
}
2 changes: 1 addition & 1 deletion src/mnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ')
}

Expand Down
4 changes: 2 additions & 2 deletions src/rlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
10 changes: 5 additions & 5 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/abi.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4fe1b70

Please sign in to comment.