Skip to content

Commit eb2fba2

Browse files
committed
feat(utils): add functions to check bigint types
1 parent 85bed72 commit eb2fba2

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

packages/utils/src/errors.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import {
1212
SupportedType,
1313
isArray,
14+
isBigInt,
1415
isDefined,
1516
isFunction,
1617
isNumber,
@@ -98,6 +99,17 @@ export function requireObject(parameterValue: object, parameterName: string) {
9899
}
99100
}
100101

102+
/**
103+
* It throws a type error if the parameter value is not a bigint.
104+
* @param parameterValue The parameter value.
105+
* @param parameterName The parameter name.
106+
*/
107+
export function requireBigInt(parameterValue: bigint, parameterName: string) {
108+
if (!isBigInt(parameterValue)) {
109+
throw new TypeError(`Parameter '${parameterName}' is not a bigint`)
110+
}
111+
}
112+
101113
/**
102114
* It throws a type error if the parameter value type is not part of the list of types.
103115
* @param parameterValue The parameter value.

packages/utils/src/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

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

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

79+
/**
80+
* It returns true if the value is a bigint, false otherwise.
81+
* @param value The value to be checked.
82+
* @returns True or false.
83+
*/
84+
export function isBigInt(value: any): boolean {
85+
return typeof value === "bigint"
86+
}
87+
7988
/**
8089
* It returns true if the value type is the same as the type passed
8190
* as the second parameter, false otherwise.
@@ -96,6 +105,8 @@ export function isType(value: any, type: SupportedType): boolean {
96105
return isUint8Array(value)
97106
case "object":
98107
return isObject(value)
108+
case "bigint":
109+
return isBigInt(value)
99110
default:
100111
return false
101112
}

packages/utils/tests/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,22 @@ describe("Utils", () => {
5050
expect(types.isObject(1)).toBeFalsy()
5151
})
5252

53+
it("Should return true if the value is a bigint", () => {
54+
expect(types.isBigInt(BigInt(1))).toBeTruthy()
55+
})
56+
57+
it("Should return false if the value is not a bigint", () => {
58+
expect(types.isBigInt(1)).toBeFalsy()
59+
})
60+
5361
it("Should return true if the value type is the one expected", () => {
5462
expect(types.isType(1, "number")).toBeTruthy()
5563
expect(types.isType("string", "string")).toBeTruthy()
5664
expect(types.isType(() => true, "function")).toBeTruthy()
5765
expect(types.isType([], "array")).toBeTruthy()
5866
expect(types.isType(new Uint8Array([]), "uint8array")).toBeTruthy()
5967
expect(types.isType({}, "object")).toBeTruthy()
68+
expect(types.isType(BigInt(1), "bigint")).toBeTruthy()
6069
})
6170

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

@@ -163,6 +173,18 @@ describe("Utils", () => {
163173
expect(fun).not.toThrow()
164174
})
165175

176+
it("Should throw an error if the parameter is not a bigint", () => {
177+
const fun = () => errors.requireBigInt(1 as any, "parameter")
178+
179+
expect(fun).toThrow("Parameter 'parameter' is not a bigint")
180+
})
181+
182+
it("Should not throw an error if the parameter is a bigint", () => {
183+
const fun = () => errors.requireBigInt(BigInt(1), "parameter")
184+
185+
expect(fun).not.toThrow()
186+
})
187+
166188
it("Should throw an error if the parameter is neither a function nor a number", () => {
167189
const fun = () => errors.requireTypes("string", "parameter", ["function", "number"])
168190

0 commit comments

Comments
 (0)