Skip to content

Commit

Permalink
feat(utils): support boolean in types-checks and error-handlers
Browse files Browse the repository at this point in the history
re #372
  • Loading branch information
vplasencia committed Jan 29, 2025
1 parent 34c781c commit eef2994
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
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
11 changes: 11 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()
Expand Down

0 comments on commit eef2994

Please sign in to comment.