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

feat(utils): support boolean in types-checks and error-handlers #373

Merged
merged 2 commits into from
Jan 31, 2025
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
12 changes: 12 additions & 0 deletions packages/utils/src/error-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isBigInt,
isBigNumber,
isBigNumberish,
isBoolean,
isBuffer,
isDefined,
isFunction,
Expand Down Expand Up @@ -49,6 +50,17 @@ export function requireNumber(parameterValue: number, parameterName: string) {
}
}

/**
* @throws Throws a type error if the parameter value is not a boolean.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireBoolean(parameterValue: boolean, parameterName: string) {
if (!isBoolean(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a boolean, received type: ${typeof parameterValue}`)
}
}

/**
* @throws Throws a type error if the parameter value is not a string.
* @param parameterValue The parameter value.
Expand Down
11 changes: 11 additions & 0 deletions packages/utils/src/type-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Buffer } from "buffer"
/** @internal */
export const supportedTypes = [
"number",
"boolean",
"string",
"function",
"Array",
Expand Down Expand Up @@ -43,6 +44,14 @@ export function isNumber(value: any): boolean {
return typeof value === "number"
}

/**
* Returns true if the value is a boolean, false otherwise.
* @param value The value to be checked.
*/
export function isBoolean(value: any): boolean {
return typeof value === "boolean"
}

/**
* Returns true if the value is a string, false otherwise.
* @param value The value to be checked.
Expand Down Expand Up @@ -179,6 +188,8 @@ export function isType(value: any, type: SupportedType): boolean {
switch (type) {
case "number":
return isNumber(value)
case "boolean":
return isBoolean(value)
case "string":
return isString(value)
case "function":
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/tests/error-handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
requireBigInt,
requireBigNumber,
requireBigNumberish,
requireBoolean,
requireBuffer,
requireDefined,
requireFunction,
Expand Down Expand Up @@ -40,6 +41,18 @@ describe("# error-handlers", () => {
expect(fun).not.toThrow()
})

it("Should throw an error if the parameter is not a boolean", () => {
const fun = () => requireBoolean("euo" as any, "parameter")

expect(fun).toThrow("Parameter 'parameter' is not a boolean")
})

it("Should not throw an error if the parameter is a boolean", () => {
const fun = () => requireBoolean(true, "parameter")

expect(fun).not.toThrow()
})

it("Should throw an error if the parameter is not a string", () => {
const fun = () => requireString(1 as any, "parameter")

Expand Down
23 changes: 23 additions & 0 deletions packages/utils/tests/type-checks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isBigInt,
isBigNumber,
isBigNumberish,
isBoolean,
isBuffer,
isFunction,
isHexadecimal,
Expand All @@ -25,6 +26,14 @@ describe("# type-checks", () => {
expect(isNumber("string")).toBeFalsy()
})

it("Should return true if the value is a boolean", () => {
expect(isBoolean(true)).toBeTruthy()
})

it("Should return false if the value is not a boolean", () => {
expect(isBoolean("string")).toBeFalsy()
})

it("Should return true if the value is a string", () => {
expect(isString("string")).toBeTruthy()
})
Expand Down Expand Up @@ -120,6 +129,7 @@ describe("# type-checks", () => {

it("Should return true if the value type is the one expected", () => {
expect(isType(1, "number")).toBeTruthy()
expect(isType(false, "boolean")).toBeTruthy()
expect(isType("string", "string")).toBeTruthy()
expect(isType(() => true, "function")).toBeTruthy()
expect(isType([], "Array")).toBeTruthy()
Expand All @@ -137,6 +147,7 @@ describe("# type-checks", () => {

it("Should return false if the value type is not the one expected or is not supported", () => {
expect(isType("string", "number")).toBeFalsy()
expect(isType(1, "boolean")).toBeFalsy()
expect(isType(1, "string")).toBeFalsy()
expect(isType(1, "function")).toBeFalsy()
expect(isType(1, "Array")).toBeFalsy()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not vital in this PR, but it would be nice to flesh out the testing of isSupportedType below so it actually checks each of the types. This occurs to me because I was looking for a utest change corresponding to each functional code change, and the change to supporeted types has no corresponding test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @artwyman! Thank you very much for the review. Yes, it makes sense. I just added the remaining types to the Should return true if the type is supported test.

Does this cover what you needed?

Expand All @@ -154,6 +165,18 @@ describe("# type-checks", () => {

it("Should return true if the type is supported", () => {
expect(isSupportedType("number")).toBeTruthy()
expect(isSupportedType("boolean")).toBeTruthy()
expect(isSupportedType("string")).toBeTruthy()
expect(isSupportedType("function")).toBeTruthy()
expect(isSupportedType("Array")).toBeTruthy()
expect(isSupportedType("Uint8Array")).toBeTruthy()
expect(isSupportedType("Buffer")).toBeTruthy()
expect(isSupportedType("object")).toBeTruthy()
expect(isSupportedType("bigint")).toBeTruthy()
expect(isSupportedType("stringified-bigint")).toBeTruthy()
expect(isSupportedType("hexadecimal")).toBeTruthy()
expect(isSupportedType("bignumber")).toBeTruthy()
expect(isSupportedType("bignumberish")).toBeTruthy()
})

it("Should return false if the type is not supported", () => {
Expand Down
Loading