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
Changes from 2 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
11 changes: 5 additions & 6 deletions packages/util/src/account.ts
Original file line number Diff line number Diff line change
@@ -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'

@@ -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
)
}

@@ -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'
}
32 changes: 0 additions & 32 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -8,8 +8,6 @@ import {
bufferToHex,
intToBuffer,
intToHex,
isFalsy,
isTruthy,
toBuffer,
toType,
} from '../src'
@@ -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()
})
})