Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: remove isTruthy and isFalsy #2261

Merged
merged 5 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/evm/tests/precompiles/hardfork.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { Address, isFalsy } from '@ethereumjs/util'
import { Address } from '@ethereumjs/util'
import * as tape from 'tape'

import { EVM } from '../../src'
Expand Down Expand Up @@ -36,7 +36,7 @@ tape('Precompiles: hardfork availability', (t) => {
// Check if ECPAIR is available in future hard forks.
const commonPetersburg = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Petersburg })
ECPAIRING = getActivePrecompiles(commonPetersburg).get(ECPAIR_AddressStr)!
if (isFalsy(ECPAIRING)) {
if (ECPAIRING === undefined) {
st.fail('ECPAIRING is not available in petersburg while it should be available')
} else {
st.pass('ECPAIRING available in petersburg')
Expand Down
7 changes: 3 additions & 4 deletions packages/evm/tests/runCode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isFalsy, isTruthy } from '@ethereumjs/util'
import * as tape from 'tape'

import { EVM } from '../src'
Expand Down Expand Up @@ -46,13 +45,13 @@ tape('VM.runCode: initial program counter', async (t) => {
err = e
}

if (isTruthy(testData.error)) {
err = isTruthy(err) ? err.message : 'no error thrown'
if (testData.error !== undefined) {
err = err?.message ?? 'no error thrown'
t.equal(err, testData.error, 'error message should match')
err = false
}

t.assert(isFalsy(err))
t.assert(err === false || err === undefined)
}
})

Expand Down
4 changes: 2 additions & 2 deletions packages/evm/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Chain, Common } from '@ethereumjs/common'
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { Account, isTruthy } from '@ethereumjs/util'
import { Account } from '@ethereumjs/util'
import path from 'path'

import { Blockchain } from '../../blockchain/src'
Expand All @@ -24,7 +24,7 @@ export function createAccount(nonce = BigInt(0), balance = BigInt(0xfff384)) {
*/
export function isRunningInKarma(): boolean {
// eslint-disable-next-line no-undef
return isTruthy((<any>globalThis).window?.__karma__)
return (<any>globalThis).window?.__karma__ !== undefined
}

/**
Expand Down
11 changes: 5 additions & 6 deletions packages/util/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import { KECCAK256_NULL, KECCAK256_RLP } from './constants'
import { assertIsBuffer, assertIsHexString, assertIsString } from './helpers'
import { stripHexPrefix } from './internal'
import { isTruthy } from './types'

import type { BigIntLike, BufferLike } from './types'

Expand All @@ -38,10 +37,10 @@ export class Account {
const { nonce, balance, storageRoot, codeHash } = accountData

return new Account(
isTruthy(nonce) ? bufferToBigInt(toBuffer(nonce)) : undefined,
isTruthy(balance) ? bufferToBigInt(toBuffer(balance)) : undefined,
isTruthy(storageRoot) ? toBuffer(storageRoot) : undefined,
isTruthy(codeHash) ? toBuffer(codeHash) : undefined
nonce !== undefined ? bufferToBigInt(toBuffer(nonce)) : undefined,
balance !== undefined ? bufferToBigInt(toBuffer(balance)) : undefined,
storageRoot !== undefined ? toBuffer(storageRoot) : undefined,
codeHash !== undefined ? toBuffer(codeHash) : undefined
)
}

Expand Down Expand Up @@ -158,7 +157,7 @@ export const toChecksumAddress = function (
const address = stripHexPrefix(hexAddress).toLowerCase()

let prefix = ''
if (isTruthy(eip1191ChainId)) {
if (eip1191ChainId !== undefined) {
const chainId = bufferToBigInt(toBuffer(eip1191ChainId))
prefix = chainId.toString() + '0x'
}
Expand Down
32 changes: 0 additions & 32 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,3 @@ export function toType<T extends TypeOutput>(
throw new Error('unknown outputType')
}
}

type Falsy = false | '' | 0 | null | undefined | 0n

/**
* Returns true if a value is falsy
*
* @param value - Value to check for falseness
*
* @deprecated This helper function should only be used temporarily until the monorepo types are explicit enough
*/
export function isFalsy(value: unknown): value is Falsy {
return !!(
value === false ||
value === '' ||
value === 0 ||
Number.isNaN(value) ||
value === null ||
typeof value === 'undefined' ||
value === BigInt(0)
)
}

/**
* Returns true if a value is truthy
*
* @param value - Value to check for truthiness
*
* @deprecated This helper function should only be used temporarily until the monorepo types are explicit enough
*/
export function isTruthy<T>(value: T | Falsy): value is T {
return !isFalsy(value)
}
34 changes: 0 additions & 34 deletions packages/util/test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
bufferToHex,
intToBuffer,
intToHex,
isFalsy,
isTruthy,
toBuffer,
toType,
} from '../src'
Expand Down Expand Up @@ -137,35 +135,3 @@ tape('toType', function (t) {
})
})
})

tape('isFalsy and isTruthy', function (t) {
const falsyValues = [false, '', 0, NaN, null, undefined, BigInt(0)]
const truthyValues = [true, 'test', -1, 1, BigInt(1), [], {}]
t.test('isFalsy should return true for all falsy values', function (st) {
for (const falsyValue of falsyValues) {
st.ok(isFalsy(falsyValue) === true)
}
st.end()
})

t.test('isFalsy should return false for truthy values', function (st) {
for (const truthyValue of truthyValues) {
st.ok(isFalsy(truthyValue) === false)
}
st.end()
})

t.test('isTruthy should return false for all falsy values', function (st) {
for (const falsyValue of falsyValues) {
st.ok(isTruthy(falsyValue) === false)
}
st.end()
})

t.test('isTruthy should return true for truthy values', function (st) {
for (const truthyValue of truthyValues) {
st.ok(isTruthy(truthyValue) === true)
}
st.end()
})
})