Skip to content

Commit

Permalink
feat(utils): add functions to check bigint types
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Feb 19, 2024
1 parent 85bed72 commit eb2fba2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/utils/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import {
SupportedType,
isArray,
isBigInt,
isDefined,
isFunction,
isNumber,
Expand Down Expand Up @@ -98,6 +99,17 @@ export function requireObject(parameterValue: object, parameterName: string) {
}
}

/**
* It throws a type error if the parameter value is not a bigint.
* @param parameterValue The parameter value.
* @param parameterName The parameter name.
*/
export function requireBigInt(parameterValue: bigint, parameterName: string) {
if (!isBigInt(parameterValue)) {
throw new TypeError(`Parameter '${parameterName}' is not a bigint`)
}
}

/**
* It throws a type error if the parameter value type is not part of the list of types.
* @param parameterValue The parameter value.
Expand Down
13 changes: 12 additions & 1 deletion packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

// The list of types supported by this utility functions.
const supportedTypes = ["number", "string", "function", "array", "uint8array", "object"] as const
const supportedTypes = ["number", "string", "function", "array", "uint8array", "object", "bigint"] as const

// Type extracted from the list above.
export type SupportedType = (typeof supportedTypes)[number]
Expand Down Expand Up @@ -76,6 +76,15 @@ export function isObject(value: any): boolean {
return typeof value === "object"
}

/**
* It returns true if the value is a bigint, false otherwise.
* @param value The value to be checked.
* @returns True or false.
*/
export function isBigInt(value: any): boolean {
return typeof value === "bigint"
}

/**
* It returns true if the value type is the same as the type passed
* as the second parameter, false otherwise.
Expand All @@ -96,6 +105,8 @@ export function isType(value: any, type: SupportedType): boolean {
return isUint8Array(value)
case "object":
return isObject(value)
case "bigint":
return isBigInt(value)
default:
return false
}
Expand Down
22 changes: 22 additions & 0 deletions packages/utils/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,22 @@ describe("Utils", () => {
expect(types.isObject(1)).toBeFalsy()
})

it("Should return true if the value is a bigint", () => {
expect(types.isBigInt(BigInt(1))).toBeTruthy()
})

it("Should return false if the value is not a bigint", () => {
expect(types.isBigInt(1)).toBeFalsy()
})

it("Should return true if the value type is the one expected", () => {
expect(types.isType(1, "number")).toBeTruthy()
expect(types.isType("string", "string")).toBeTruthy()
expect(types.isType(() => true, "function")).toBeTruthy()
expect(types.isType([], "array")).toBeTruthy()
expect(types.isType(new Uint8Array([]), "uint8array")).toBeTruthy()
expect(types.isType({}, "object")).toBeTruthy()
expect(types.isType(BigInt(1), "bigint")).toBeTruthy()
})

it("Should return false if the value type is not the one expected or is not supported", () => {
Expand All @@ -66,6 +75,7 @@ describe("Utils", () => {
expect(types.isType(1, "array")).toBeFalsy()
expect(types.isType(1, "uint8array")).toBeFalsy()
expect(types.isType(1, "object")).toBeFalsy()
expect(types.isType(1, "bigint")).toBeFalsy()
expect(types.isType(1, "type" as any)).toBeFalsy()
})

Expand Down Expand Up @@ -163,6 +173,18 @@ describe("Utils", () => {
expect(fun).not.toThrow()
})

it("Should throw an error if the parameter is not a bigint", () => {
const fun = () => errors.requireBigInt(1 as any, "parameter")

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

it("Should not throw an error if the parameter is a bigint", () => {
const fun = () => errors.requireBigInt(BigInt(1), "parameter")

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

it("Should throw an error if the parameter is neither a function nor a number", () => {
const fun = () => errors.requireTypes("string", "parameter", ["function", "number"])

Expand Down

0 comments on commit eb2fba2

Please sign in to comment.