diff --git a/packages/utils/src/error-handlers.ts b/packages/utils/src/error-handlers.ts index 34cfd5c9b..3b2908b11 100644 --- a/packages/utils/src/error-handlers.ts +++ b/packages/utils/src/error-handlers.ts @@ -14,6 +14,7 @@ import { isBigInt, isBigNumber, isBigNumberish, + isBoolean, isBuffer, isDefined, isFunction, @@ -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. diff --git a/packages/utils/src/type-checks.ts b/packages/utils/src/type-checks.ts index 8be244312..5eb56d335 100644 --- a/packages/utils/src/type-checks.ts +++ b/packages/utils/src/type-checks.ts @@ -12,6 +12,7 @@ import { Buffer } from "buffer" /** @internal */ export const supportedTypes = [ "number", + "boolean", "string", "function", "Array", @@ -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. @@ -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": diff --git a/packages/utils/tests/error-handlers.test.ts b/packages/utils/tests/error-handlers.test.ts index 69ac62a9e..c501cfe65 100644 --- a/packages/utils/tests/error-handlers.test.ts +++ b/packages/utils/tests/error-handlers.test.ts @@ -3,6 +3,7 @@ import { requireBigInt, requireBigNumber, requireBigNumberish, + requireBoolean, requireBuffer, requireDefined, requireFunction, @@ -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") diff --git a/packages/utils/tests/type-checks.test.ts b/packages/utils/tests/type-checks.test.ts index a4bb6f1c7..5e92526ae 100644 --- a/packages/utils/tests/type-checks.test.ts +++ b/packages/utils/tests/type-checks.test.ts @@ -4,6 +4,7 @@ import { isBigInt, isBigNumber, isBigNumberish, + isBoolean, isBuffer, isFunction, isHexadecimal, @@ -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() }) @@ -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() @@ -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()