@semaphore-protocol/utils 4.0.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +79 -0
- package/dist/index.cjs +259 -0
- package/dist/index.js +256 -0
- package/dist/lib.commonjs/errors.cjs +131 -0
- package/dist/lib.commonjs/index.cjs +17 -0
- package/dist/lib.commonjs/types.cjs +130 -0
- package/dist/lib.esm/errors.js +121 -0
- package/dist/lib.esm/index.js +12 -0
- package/dist/lib.esm/types.js +119 -0
- package/dist/types/errors.d.ts +64 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/types.d.ts +73 -0
- package/package.json +50 -0
- package/src/errors.ts +132 -0
- package/src/index.ts +4 -0
- package/src/types.ts +123 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ethereum Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<h1 align="center">
|
|
3
|
+
Semaphore utils
|
|
4
|
+
</h1>
|
|
5
|
+
<p align="center">A library to provide utility functions to the other Semaphore packages.</p>
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
<p align="center">
|
|
9
|
+
<a href="https://github.com/semaphore-protocol">
|
|
10
|
+
<img src="https://img.shields.io/badge/project-Semaphore-blue.svg?style=flat-square">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/semaphore-protocol/semaphore/blob/main/LICENSE">
|
|
13
|
+
<img alt="NPM license" src="https://img.shields.io/npm/l/%40semaphore-protocol%2Futils?style=flat-square">
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://www.npmjs.com/package/@semaphore-protocol/utils">
|
|
16
|
+
<img alt="NPM version" src="https://img.shields.io/npm/v/@semaphore-protocol/utils?style=flat-square" />
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://npmjs.org/package/@semaphore-protocol/utils">
|
|
19
|
+
<img alt="Downloads" src="https://img.shields.io/npm/dm/@semaphore-protocol/utils.svg?style=flat-square" />
|
|
20
|
+
</a>
|
|
21
|
+
<a href="https://js.semaphore.pse.dev/modules/_semaphore_protocol_utils">
|
|
22
|
+
<img alt="Documentation typedoc" src="https://img.shields.io/badge/docs-typedoc-744C7C?style=flat-square">
|
|
23
|
+
</a>
|
|
24
|
+
<a href="https://eslint.org/">
|
|
25
|
+
<img alt="Linter eslint" src="https://img.shields.io/badge/linter-eslint-8080f2?style=flat-square&logo=eslint" />
|
|
26
|
+
</a>
|
|
27
|
+
<a href="https://prettier.io/">
|
|
28
|
+
<img alt="Code style prettier" src="https://img.shields.io/badge/code%20style-prettier-f8bc45?style=flat-square&logo=prettier" />
|
|
29
|
+
</a>
|
|
30
|
+
</p>
|
|
31
|
+
|
|
32
|
+
<div align="center">
|
|
33
|
+
<h4>
|
|
34
|
+
<a href="https://github.com/semaphore-protocol/semaphore/blob/main/CONTRIBUTING.md">
|
|
35
|
+
👥 Contributing
|
|
36
|
+
</a>
|
|
37
|
+
<span> | </span>
|
|
38
|
+
<a href="https://github.com/semaphore-protocol/semaphore/blob/main/CODE_OF_CONDUCT.md">
|
|
39
|
+
🤝 Code of conduct
|
|
40
|
+
</a>
|
|
41
|
+
<span> | </span>
|
|
42
|
+
<a href="https://github.com/semaphore-protocol/semaphore/contribute">
|
|
43
|
+
🔎 Issues
|
|
44
|
+
</a>
|
|
45
|
+
<span> | </span>
|
|
46
|
+
<a href="https://semaphore.pse.dev/discord">
|
|
47
|
+
🗣️ Chat & Support
|
|
48
|
+
</a>
|
|
49
|
+
</h4>
|
|
50
|
+
</div>
|
|
51
|
+
|
|
52
|
+
## 🛠 Install
|
|
53
|
+
|
|
54
|
+
### npm or yarn
|
|
55
|
+
|
|
56
|
+
Install the `@semaphore-protocol/utils` package with npm:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm i @semaphore-protocol/utils
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
or yarn:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
yarn add @semaphore-protocol/utils
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 📜 Usage
|
|
69
|
+
|
|
70
|
+
For more information on the functions and modules provided by `@semaphore-protocol/utils`, please refer to the [TypeDoc documentation](https://js.semaphore.pse.dev/modules/_semaphore_protocol_utils).
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// You can import modules from the main bundle.
|
|
74
|
+
import { errors, types } from "@semaphore-protocol/utils"
|
|
75
|
+
|
|
76
|
+
// Or by using conditional exports.
|
|
77
|
+
import { requireNumber } from "@semaphore-protocol/utils/errors"
|
|
78
|
+
import { isNumber } from "@semaphore-protocol/utils/types"
|
|
79
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @semaphore-protocol/utils
|
|
3
|
+
* @version 4.0.0-beta
|
|
4
|
+
* @file A library to provide utility functions to the other Semaphore packages.
|
|
5
|
+
* @copyright Ethereum Foundation 2024
|
|
6
|
+
* @license MIT
|
|
7
|
+
* @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/utils}
|
|
8
|
+
*/
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @module Types
|
|
13
|
+
* This module provides utility functions to check data types.
|
|
14
|
+
* It defines a set of supported types and includes functions to check if
|
|
15
|
+
* a value is defined and if it matches a supported type. These functions
|
|
16
|
+
* are useful for type checking and validation in the other libraries,
|
|
17
|
+
* enhancing code robustness and reliability.
|
|
18
|
+
*/
|
|
19
|
+
// The list of types supported by this utility functions.
|
|
20
|
+
var supportedTypes = ["number", "string", "function", "array", "uint8array", "object", "bigint"];
|
|
21
|
+
/**
|
|
22
|
+
* It returns true if the value is defined, false otherwise.
|
|
23
|
+
* @param value The value to be checked.
|
|
24
|
+
* @returns True or false.
|
|
25
|
+
*/
|
|
26
|
+
function isDefined(value) {
|
|
27
|
+
return typeof value !== "undefined";
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* It returns true if the value is a number, false otherwise.
|
|
31
|
+
* @param value The value to be checked.
|
|
32
|
+
* @returns True or false.
|
|
33
|
+
*/
|
|
34
|
+
function isNumber(value) {
|
|
35
|
+
return typeof value === "number";
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* It returns true if the value is a string, false otherwise.
|
|
39
|
+
* @param value The value to be checked.
|
|
40
|
+
* @returns True or false.
|
|
41
|
+
*/
|
|
42
|
+
function isString(value) {
|
|
43
|
+
return typeof value === "string";
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* It returns true if the value is a function, false otherwise.
|
|
47
|
+
* @param value The value to be checked.
|
|
48
|
+
* @returns True or false.
|
|
49
|
+
*/
|
|
50
|
+
function isFunction(value) {
|
|
51
|
+
return typeof value === "function";
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* It returns true if the value is an array, false otherwise.
|
|
55
|
+
* @param value The value to be checked.
|
|
56
|
+
* @returns True or false.
|
|
57
|
+
*/
|
|
58
|
+
function isArray(value) {
|
|
59
|
+
return typeof value === "object" && Array.isArray(value);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* It returns true if the value is a uint8array, false otherwise.
|
|
63
|
+
* @param value The value to be checked.
|
|
64
|
+
* @returns True or false.
|
|
65
|
+
*/
|
|
66
|
+
function isUint8Array(value) {
|
|
67
|
+
return value instanceof Uint8Array;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* It returns true if the value is an object, false otherwise.
|
|
71
|
+
* @param value The value to be checked.
|
|
72
|
+
* @returns True or false.
|
|
73
|
+
*/
|
|
74
|
+
function isObject(value) {
|
|
75
|
+
return typeof value === "object";
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* It returns true if the value is a bigint, false otherwise.
|
|
79
|
+
* @param value The value to be checked.
|
|
80
|
+
* @returns True or false.
|
|
81
|
+
*/
|
|
82
|
+
function isBigInt(value) {
|
|
83
|
+
return typeof value === "bigint";
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* It returns true if the value type is the same as the type passed
|
|
87
|
+
* as the second parameter, false otherwise.
|
|
88
|
+
* @param type The expected type.
|
|
89
|
+
* @returns True or false.
|
|
90
|
+
*/
|
|
91
|
+
function isType(value, type) {
|
|
92
|
+
switch (type) {
|
|
93
|
+
case "number":
|
|
94
|
+
return isNumber(value);
|
|
95
|
+
case "string":
|
|
96
|
+
return isString(value);
|
|
97
|
+
case "function":
|
|
98
|
+
return isFunction(value);
|
|
99
|
+
case "array":
|
|
100
|
+
return isArray(value);
|
|
101
|
+
case "uint8array":
|
|
102
|
+
return isUint8Array(value);
|
|
103
|
+
case "object":
|
|
104
|
+
return isObject(value);
|
|
105
|
+
case "bigint":
|
|
106
|
+
return isBigInt(value);
|
|
107
|
+
default:
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Return true if the type is being supported by this utility
|
|
113
|
+
* functions, false otherwise.
|
|
114
|
+
* @param type The type to be checked.
|
|
115
|
+
* @returns True or false
|
|
116
|
+
*/
|
|
117
|
+
function isSupportedType(type) {
|
|
118
|
+
return supportedTypes.includes(type);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
var types = /*#__PURE__*/Object.freeze({
|
|
122
|
+
__proto__: null,
|
|
123
|
+
isArray: isArray,
|
|
124
|
+
isBigInt: isBigInt,
|
|
125
|
+
isDefined: isDefined,
|
|
126
|
+
isFunction: isFunction,
|
|
127
|
+
isNumber: isNumber,
|
|
128
|
+
isObject: isObject,
|
|
129
|
+
isString: isString,
|
|
130
|
+
isSupportedType: isSupportedType,
|
|
131
|
+
isType: isType,
|
|
132
|
+
isUint8Array: isUint8Array
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @module Errors
|
|
137
|
+
* This module is designed to provide utility functions for validating
|
|
138
|
+
* function parameters. It includes functions that throw type errors if
|
|
139
|
+
* the parameters do not meet specified criteria, such as being defined,
|
|
140
|
+
* a number, a string, a function, or an array. This module helps ensure
|
|
141
|
+
* that functions receive the correct types of inputs, enhancing code
|
|
142
|
+
* reliability and reducing runtime errors.
|
|
143
|
+
*/
|
|
144
|
+
/**
|
|
145
|
+
* It throws a type error if the parameter value has not been defined.
|
|
146
|
+
* @param parameterValue The parameter value.
|
|
147
|
+
* @param parameterName The parameter name.
|
|
148
|
+
*/
|
|
149
|
+
function requireDefined(parameterValue, parameterName) {
|
|
150
|
+
if (!isDefined(parameterValue)) {
|
|
151
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not defined"));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* It throws a type error if the parameter value is not a number.
|
|
156
|
+
* @param parameterValue The parameter value.
|
|
157
|
+
* @param parameterName The parameter name.
|
|
158
|
+
*/
|
|
159
|
+
function requireNumber(parameterValue, parameterName) {
|
|
160
|
+
if (!isNumber(parameterValue)) {
|
|
161
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a number"));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* It throws a type error if the parameter value is not a string.
|
|
166
|
+
* @param parameterValue The parameter value.
|
|
167
|
+
* @param parameterName The parameter name.
|
|
168
|
+
*/
|
|
169
|
+
function requireString(parameterValue, parameterName) {
|
|
170
|
+
if (!isString(parameterValue)) {
|
|
171
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a string"));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* It throws a type error if the parameter value is not a function.
|
|
176
|
+
* @param parameterValue The parameter value.
|
|
177
|
+
* @param parameterName The parameter name.
|
|
178
|
+
*/
|
|
179
|
+
function requireFunction(parameterValue, parameterName) {
|
|
180
|
+
if (!isFunction(parameterValue)) {
|
|
181
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a function"));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* It throws a type error if the parameter value is not an array.
|
|
186
|
+
* @param parameterValue The parameter value.
|
|
187
|
+
* @param parameterName The parameter name.
|
|
188
|
+
*/
|
|
189
|
+
function requireArray(parameterValue, parameterName) {
|
|
190
|
+
if (!isArray(parameterValue)) {
|
|
191
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not an array"));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* It throws a type error if the parameter value is not a uint8array.
|
|
196
|
+
* @param parameterValue The parameter value.
|
|
197
|
+
* @param parameterName The parameter name.
|
|
198
|
+
*/
|
|
199
|
+
function requireUint8Array(parameterValue, parameterName) {
|
|
200
|
+
if (!isUint8Array(parameterValue)) {
|
|
201
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a Uint8Array"));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* It throws a type error if the parameter value is not an object.
|
|
206
|
+
* @param parameterValue The parameter value.
|
|
207
|
+
* @param parameterName The parameter name.
|
|
208
|
+
*/
|
|
209
|
+
function requireObject(parameterValue, parameterName) {
|
|
210
|
+
if (!isObject(parameterValue)) {
|
|
211
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not an object"));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* It throws a type error if the parameter value is not a bigint.
|
|
216
|
+
* @param parameterValue The parameter value.
|
|
217
|
+
* @param parameterName The parameter name.
|
|
218
|
+
*/
|
|
219
|
+
function requireBigInt(parameterValue, parameterName) {
|
|
220
|
+
if (!isBigInt(parameterValue)) {
|
|
221
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a bigint"));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* It throws a type error if the parameter value type is not part of the list of types.
|
|
226
|
+
* @param parameterValue The parameter value.
|
|
227
|
+
* @param parameterName The parameter name.
|
|
228
|
+
*/
|
|
229
|
+
function requireTypes(parameterValue, parameterName, types) {
|
|
230
|
+
for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
|
|
231
|
+
var type = types_1[_i];
|
|
232
|
+
if (!isSupportedType(type)) {
|
|
233
|
+
throw new Error("Type '".concat(type, "' is not supported"));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
for (var _a = 0, types_2 = types; _a < types_2.length; _a++) {
|
|
237
|
+
var type = types_2[_a];
|
|
238
|
+
if (isType(parameterValue, type)) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is none of the following types: ").concat(types.join(", ")));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
var errors = /*#__PURE__*/Object.freeze({
|
|
246
|
+
__proto__: null,
|
|
247
|
+
requireArray: requireArray,
|
|
248
|
+
requireBigInt: requireBigInt,
|
|
249
|
+
requireDefined: requireDefined,
|
|
250
|
+
requireFunction: requireFunction,
|
|
251
|
+
requireNumber: requireNumber,
|
|
252
|
+
requireObject: requireObject,
|
|
253
|
+
requireString: requireString,
|
|
254
|
+
requireTypes: requireTypes,
|
|
255
|
+
requireUint8Array: requireUint8Array
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
exports.errors = errors;
|
|
259
|
+
exports.types = types;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @semaphore-protocol/utils
|
|
3
|
+
* @version 4.0.0-beta
|
|
4
|
+
* @file A library to provide utility functions to the other Semaphore packages.
|
|
5
|
+
* @copyright Ethereum Foundation 2024
|
|
6
|
+
* @license MIT
|
|
7
|
+
* @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/utils}
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* @module Types
|
|
11
|
+
* This module provides utility functions to check data types.
|
|
12
|
+
* It defines a set of supported types and includes functions to check if
|
|
13
|
+
* a value is defined and if it matches a supported type. These functions
|
|
14
|
+
* are useful for type checking and validation in the other libraries,
|
|
15
|
+
* enhancing code robustness and reliability.
|
|
16
|
+
*/
|
|
17
|
+
// The list of types supported by this utility functions.
|
|
18
|
+
var supportedTypes = ["number", "string", "function", "array", "uint8array", "object", "bigint"];
|
|
19
|
+
/**
|
|
20
|
+
* It returns true if the value is defined, false otherwise.
|
|
21
|
+
* @param value The value to be checked.
|
|
22
|
+
* @returns True or false.
|
|
23
|
+
*/
|
|
24
|
+
function isDefined(value) {
|
|
25
|
+
return typeof value !== "undefined";
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* It returns true if the value is a number, false otherwise.
|
|
29
|
+
* @param value The value to be checked.
|
|
30
|
+
* @returns True or false.
|
|
31
|
+
*/
|
|
32
|
+
function isNumber(value) {
|
|
33
|
+
return typeof value === "number";
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* It returns true if the value is a string, false otherwise.
|
|
37
|
+
* @param value The value to be checked.
|
|
38
|
+
* @returns True or false.
|
|
39
|
+
*/
|
|
40
|
+
function isString(value) {
|
|
41
|
+
return typeof value === "string";
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* It returns true if the value is a function, false otherwise.
|
|
45
|
+
* @param value The value to be checked.
|
|
46
|
+
* @returns True or false.
|
|
47
|
+
*/
|
|
48
|
+
function isFunction(value) {
|
|
49
|
+
return typeof value === "function";
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* It returns true if the value is an array, false otherwise.
|
|
53
|
+
* @param value The value to be checked.
|
|
54
|
+
* @returns True or false.
|
|
55
|
+
*/
|
|
56
|
+
function isArray(value) {
|
|
57
|
+
return typeof value === "object" && Array.isArray(value);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* It returns true if the value is a uint8array, false otherwise.
|
|
61
|
+
* @param value The value to be checked.
|
|
62
|
+
* @returns True or false.
|
|
63
|
+
*/
|
|
64
|
+
function isUint8Array(value) {
|
|
65
|
+
return value instanceof Uint8Array;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* It returns true if the value is an object, false otherwise.
|
|
69
|
+
* @param value The value to be checked.
|
|
70
|
+
* @returns True or false.
|
|
71
|
+
*/
|
|
72
|
+
function isObject(value) {
|
|
73
|
+
return typeof value === "object";
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* It returns true if the value is a bigint, false otherwise.
|
|
77
|
+
* @param value The value to be checked.
|
|
78
|
+
* @returns True or false.
|
|
79
|
+
*/
|
|
80
|
+
function isBigInt(value) {
|
|
81
|
+
return typeof value === "bigint";
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* It returns true if the value type is the same as the type passed
|
|
85
|
+
* as the second parameter, false otherwise.
|
|
86
|
+
* @param type The expected type.
|
|
87
|
+
* @returns True or false.
|
|
88
|
+
*/
|
|
89
|
+
function isType(value, type) {
|
|
90
|
+
switch (type) {
|
|
91
|
+
case "number":
|
|
92
|
+
return isNumber(value);
|
|
93
|
+
case "string":
|
|
94
|
+
return isString(value);
|
|
95
|
+
case "function":
|
|
96
|
+
return isFunction(value);
|
|
97
|
+
case "array":
|
|
98
|
+
return isArray(value);
|
|
99
|
+
case "uint8array":
|
|
100
|
+
return isUint8Array(value);
|
|
101
|
+
case "object":
|
|
102
|
+
return isObject(value);
|
|
103
|
+
case "bigint":
|
|
104
|
+
return isBigInt(value);
|
|
105
|
+
default:
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Return true if the type is being supported by this utility
|
|
111
|
+
* functions, false otherwise.
|
|
112
|
+
* @param type The type to be checked.
|
|
113
|
+
* @returns True or false
|
|
114
|
+
*/
|
|
115
|
+
function isSupportedType(type) {
|
|
116
|
+
return supportedTypes.includes(type);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
var types = /*#__PURE__*/Object.freeze({
|
|
120
|
+
__proto__: null,
|
|
121
|
+
isArray: isArray,
|
|
122
|
+
isBigInt: isBigInt,
|
|
123
|
+
isDefined: isDefined,
|
|
124
|
+
isFunction: isFunction,
|
|
125
|
+
isNumber: isNumber,
|
|
126
|
+
isObject: isObject,
|
|
127
|
+
isString: isString,
|
|
128
|
+
isSupportedType: isSupportedType,
|
|
129
|
+
isType: isType,
|
|
130
|
+
isUint8Array: isUint8Array
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @module Errors
|
|
135
|
+
* This module is designed to provide utility functions for validating
|
|
136
|
+
* function parameters. It includes functions that throw type errors if
|
|
137
|
+
* the parameters do not meet specified criteria, such as being defined,
|
|
138
|
+
* a number, a string, a function, or an array. This module helps ensure
|
|
139
|
+
* that functions receive the correct types of inputs, enhancing code
|
|
140
|
+
* reliability and reducing runtime errors.
|
|
141
|
+
*/
|
|
142
|
+
/**
|
|
143
|
+
* It throws a type error if the parameter value has not been defined.
|
|
144
|
+
* @param parameterValue The parameter value.
|
|
145
|
+
* @param parameterName The parameter name.
|
|
146
|
+
*/
|
|
147
|
+
function requireDefined(parameterValue, parameterName) {
|
|
148
|
+
if (!isDefined(parameterValue)) {
|
|
149
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not defined"));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* It throws a type error if the parameter value is not a number.
|
|
154
|
+
* @param parameterValue The parameter value.
|
|
155
|
+
* @param parameterName The parameter name.
|
|
156
|
+
*/
|
|
157
|
+
function requireNumber(parameterValue, parameterName) {
|
|
158
|
+
if (!isNumber(parameterValue)) {
|
|
159
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a number"));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* It throws a type error if the parameter value is not a string.
|
|
164
|
+
* @param parameterValue The parameter value.
|
|
165
|
+
* @param parameterName The parameter name.
|
|
166
|
+
*/
|
|
167
|
+
function requireString(parameterValue, parameterName) {
|
|
168
|
+
if (!isString(parameterValue)) {
|
|
169
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a string"));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* It throws a type error if the parameter value is not a function.
|
|
174
|
+
* @param parameterValue The parameter value.
|
|
175
|
+
* @param parameterName The parameter name.
|
|
176
|
+
*/
|
|
177
|
+
function requireFunction(parameterValue, parameterName) {
|
|
178
|
+
if (!isFunction(parameterValue)) {
|
|
179
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a function"));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* It throws a type error if the parameter value is not an array.
|
|
184
|
+
* @param parameterValue The parameter value.
|
|
185
|
+
* @param parameterName The parameter name.
|
|
186
|
+
*/
|
|
187
|
+
function requireArray(parameterValue, parameterName) {
|
|
188
|
+
if (!isArray(parameterValue)) {
|
|
189
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not an array"));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* It throws a type error if the parameter value is not a uint8array.
|
|
194
|
+
* @param parameterValue The parameter value.
|
|
195
|
+
* @param parameterName The parameter name.
|
|
196
|
+
*/
|
|
197
|
+
function requireUint8Array(parameterValue, parameterName) {
|
|
198
|
+
if (!isUint8Array(parameterValue)) {
|
|
199
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a Uint8Array"));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* It throws a type error if the parameter value is not an object.
|
|
204
|
+
* @param parameterValue The parameter value.
|
|
205
|
+
* @param parameterName The parameter name.
|
|
206
|
+
*/
|
|
207
|
+
function requireObject(parameterValue, parameterName) {
|
|
208
|
+
if (!isObject(parameterValue)) {
|
|
209
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not an object"));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* It throws a type error if the parameter value is not a bigint.
|
|
214
|
+
* @param parameterValue The parameter value.
|
|
215
|
+
* @param parameterName The parameter name.
|
|
216
|
+
*/
|
|
217
|
+
function requireBigInt(parameterValue, parameterName) {
|
|
218
|
+
if (!isBigInt(parameterValue)) {
|
|
219
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is not a bigint"));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* It throws a type error if the parameter value type is not part of the list of types.
|
|
224
|
+
* @param parameterValue The parameter value.
|
|
225
|
+
* @param parameterName The parameter name.
|
|
226
|
+
*/
|
|
227
|
+
function requireTypes(parameterValue, parameterName, types) {
|
|
228
|
+
for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
|
|
229
|
+
var type = types_1[_i];
|
|
230
|
+
if (!isSupportedType(type)) {
|
|
231
|
+
throw new Error("Type '".concat(type, "' is not supported"));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
for (var _a = 0, types_2 = types; _a < types_2.length; _a++) {
|
|
235
|
+
var type = types_2[_a];
|
|
236
|
+
if (isType(parameterValue, type)) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
throw new TypeError("Parameter '".concat(parameterName, "' is none of the following types: ").concat(types.join(", ")));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
var errors = /*#__PURE__*/Object.freeze({
|
|
244
|
+
__proto__: null,
|
|
245
|
+
requireArray: requireArray,
|
|
246
|
+
requireBigInt: requireBigInt,
|
|
247
|
+
requireDefined: requireDefined,
|
|
248
|
+
requireFunction: requireFunction,
|
|
249
|
+
requireNumber: requireNumber,
|
|
250
|
+
requireObject: requireObject,
|
|
251
|
+
requireString: requireString,
|
|
252
|
+
requireTypes: requireTypes,
|
|
253
|
+
requireUint8Array: requireUint8Array
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
export { errors, types };
|