@semaphore-protocol/utils 4.0.0-beta.1 → 4.0.0-beta.3

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.
@@ -1,119 +0,0 @@
1
- /**
2
- * @module @semaphore-protocol/utils
3
- * @version 4.0.0-beta.1
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
- export { isArray, isBigInt, isDefined, isFunction, isNumber, isObject, isString, isSupportedType, isType, isUint8Array };
@@ -1,64 +0,0 @@
1
- /**
2
- * @module Errors
3
- * This module is designed to provide utility functions for validating
4
- * function parameters. It includes functions that throw type errors if
5
- * the parameters do not meet specified criteria, such as being defined,
6
- * a number, a string, a function, or an array. This module helps ensure
7
- * that functions receive the correct types of inputs, enhancing code
8
- * reliability and reducing runtime errors.
9
- */
10
- import { SupportedType } from "./types";
11
- /**
12
- * It throws a type error if the parameter value has not been defined.
13
- * @param parameterValue The parameter value.
14
- * @param parameterName The parameter name.
15
- */
16
- export declare function requireDefined(parameterValue: any, parameterName: string): void;
17
- /**
18
- * It throws a type error if the parameter value is not a number.
19
- * @param parameterValue The parameter value.
20
- * @param parameterName The parameter name.
21
- */
22
- export declare function requireNumber(parameterValue: number, parameterName: string): void;
23
- /**
24
- * It throws a type error if the parameter value is not a string.
25
- * @param parameterValue The parameter value.
26
- * @param parameterName The parameter name.
27
- */
28
- export declare function requireString(parameterValue: string, parameterName: string): void;
29
- /**
30
- * It throws a type error if the parameter value is not a function.
31
- * @param parameterValue The parameter value.
32
- * @param parameterName The parameter name.
33
- */
34
- export declare function requireFunction(parameterValue: Function, parameterName: string): void;
35
- /**
36
- * It throws a type error if the parameter value is not an array.
37
- * @param parameterValue The parameter value.
38
- * @param parameterName The parameter name.
39
- */
40
- export declare function requireArray(parameterValue: any[], parameterName: string): void;
41
- /**
42
- * It throws a type error if the parameter value is not a uint8array.
43
- * @param parameterValue The parameter value.
44
- * @param parameterName The parameter name.
45
- */
46
- export declare function requireUint8Array(parameterValue: Uint8Array, parameterName: string): void;
47
- /**
48
- * It throws a type error if the parameter value is not an object.
49
- * @param parameterValue The parameter value.
50
- * @param parameterName The parameter name.
51
- */
52
- export declare function requireObject(parameterValue: object, parameterName: string): void;
53
- /**
54
- * It throws a type error if the parameter value is not a bigint.
55
- * @param parameterValue The parameter value.
56
- * @param parameterName The parameter name.
57
- */
58
- export declare function requireBigInt(parameterValue: bigint, parameterName: string): void;
59
- /**
60
- * It throws a type error if the parameter value type is not part of the list of types.
61
- * @param parameterValue The parameter value.
62
- * @param parameterName The parameter name.
63
- */
64
- export declare function requireTypes(parameterValue: any, parameterName: string, types: SupportedType[]): void;
@@ -1,73 +0,0 @@
1
- /**
2
- * @module Types
3
- * This module provides utility functions to check data types.
4
- * It defines a set of supported types and includes functions to check if
5
- * a value is defined and if it matches a supported type. These functions
6
- * are useful for type checking and validation in the other libraries,
7
- * enhancing code robustness and reliability.
8
- */
9
- declare const supportedTypes: readonly ["number", "string", "function", "array", "uint8array", "object", "bigint"];
10
- export type SupportedType = (typeof supportedTypes)[number];
11
- /**
12
- * It returns true if the value is defined, false otherwise.
13
- * @param value The value to be checked.
14
- * @returns True or false.
15
- */
16
- export declare function isDefined(value: any): boolean;
17
- /**
18
- * It returns true if the value is a number, false otherwise.
19
- * @param value The value to be checked.
20
- * @returns True or false.
21
- */
22
- export declare function isNumber(value: any): boolean;
23
- /**
24
- * It returns true if the value is a string, false otherwise.
25
- * @param value The value to be checked.
26
- * @returns True or false.
27
- */
28
- export declare function isString(value: any): boolean;
29
- /**
30
- * It returns true if the value is a function, false otherwise.
31
- * @param value The value to be checked.
32
- * @returns True or false.
33
- */
34
- export declare function isFunction(value: any): boolean;
35
- /**
36
- * It returns true if the value is an array, false otherwise.
37
- * @param value The value to be checked.
38
- * @returns True or false.
39
- */
40
- export declare function isArray(value: any): boolean;
41
- /**
42
- * It returns true if the value is a uint8array, false otherwise.
43
- * @param value The value to be checked.
44
- * @returns True or false.
45
- */
46
- export declare function isUint8Array(value: any): boolean;
47
- /**
48
- * It returns true if the value is an object, false otherwise.
49
- * @param value The value to be checked.
50
- * @returns True or false.
51
- */
52
- export declare function isObject(value: any): boolean;
53
- /**
54
- * It returns true if the value is a bigint, false otherwise.
55
- * @param value The value to be checked.
56
- * @returns True or false.
57
- */
58
- export declare function isBigInt(value: any): boolean;
59
- /**
60
- * It returns true if the value type is the same as the type passed
61
- * as the second parameter, false otherwise.
62
- * @param type The expected type.
63
- * @returns True or false.
64
- */
65
- export declare function isType(value: any, type: SupportedType): boolean;
66
- /**
67
- * Return true if the type is being supported by this utility
68
- * functions, false otherwise.
69
- * @param type The type to be checked.
70
- * @returns True or false
71
- */
72
- export declare function isSupportedType(type: string): type is SupportedType;
73
- export {};
package/src/errors.ts DELETED
@@ -1,132 +0,0 @@
1
- /**
2
- * @module Errors
3
- * This module is designed to provide utility functions for validating
4
- * function parameters. It includes functions that throw type errors if
5
- * the parameters do not meet specified criteria, such as being defined,
6
- * a number, a string, a function, or an array. This module helps ensure
7
- * that functions receive the correct types of inputs, enhancing code
8
- * reliability and reducing runtime errors.
9
- */
10
-
11
- import {
12
- SupportedType,
13
- isArray,
14
- isBigInt,
15
- isDefined,
16
- isFunction,
17
- isNumber,
18
- isObject,
19
- isString,
20
- isSupportedType,
21
- isType,
22
- isUint8Array
23
- } from "./types"
24
-
25
- /**
26
- * It throws a type error if the parameter value has not been defined.
27
- * @param parameterValue The parameter value.
28
- * @param parameterName The parameter name.
29
- */
30
- export function requireDefined(parameterValue: any, parameterName: string) {
31
- if (!isDefined(parameterValue)) {
32
- throw new TypeError(`Parameter '${parameterName}' is not defined`)
33
- }
34
- }
35
-
36
- /**
37
- * It throws a type error if the parameter value is not a number.
38
- * @param parameterValue The parameter value.
39
- * @param parameterName The parameter name.
40
- */
41
- export function requireNumber(parameterValue: number, parameterName: string) {
42
- if (!isNumber(parameterValue)) {
43
- throw new TypeError(`Parameter '${parameterName}' is not a number`)
44
- }
45
- }
46
-
47
- /**
48
- * It throws a type error if the parameter value is not a string.
49
- * @param parameterValue The parameter value.
50
- * @param parameterName The parameter name.
51
- */
52
- export function requireString(parameterValue: string, parameterName: string) {
53
- if (!isString(parameterValue)) {
54
- throw new TypeError(`Parameter '${parameterName}' is not a string`)
55
- }
56
- }
57
-
58
- /**
59
- * It throws a type error if the parameter value is not a function.
60
- * @param parameterValue The parameter value.
61
- * @param parameterName The parameter name.
62
- */
63
- export function requireFunction(parameterValue: Function, parameterName: string) {
64
- if (!isFunction(parameterValue)) {
65
- throw new TypeError(`Parameter '${parameterName}' is not a function`)
66
- }
67
- }
68
-
69
- /**
70
- * It throws a type error if the parameter value is not an array.
71
- * @param parameterValue The parameter value.
72
- * @param parameterName The parameter name.
73
- */
74
- export function requireArray(parameterValue: any[], parameterName: string) {
75
- if (!isArray(parameterValue)) {
76
- throw new TypeError(`Parameter '${parameterName}' is not an array`)
77
- }
78
- }
79
-
80
- /**
81
- * It throws a type error if the parameter value is not a uint8array.
82
- * @param parameterValue The parameter value.
83
- * @param parameterName The parameter name.
84
- */
85
- export function requireUint8Array(parameterValue: Uint8Array, parameterName: string) {
86
- if (!isUint8Array(parameterValue)) {
87
- throw new TypeError(`Parameter '${parameterName}' is not a Uint8Array`)
88
- }
89
- }
90
-
91
- /**
92
- * It throws a type error if the parameter value is not an object.
93
- * @param parameterValue The parameter value.
94
- * @param parameterName The parameter name.
95
- */
96
- export function requireObject(parameterValue: object, parameterName: string) {
97
- if (!isObject(parameterValue)) {
98
- throw new TypeError(`Parameter '${parameterName}' is not an object`)
99
- }
100
- }
101
-
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
-
113
- /**
114
- * It throws a type error if the parameter value type is not part of the list of types.
115
- * @param parameterValue The parameter value.
116
- * @param parameterName The parameter name.
117
- */
118
- export function requireTypes(parameterValue: any, parameterName: string, types: SupportedType[]) {
119
- for (const type of types) {
120
- if (!isSupportedType(type)) {
121
- throw new Error(`Type '${type}' is not supported`)
122
- }
123
- }
124
-
125
- for (const type of types) {
126
- if (isType(parameterValue, type)) {
127
- return
128
- }
129
- }
130
-
131
- throw new TypeError(`Parameter '${parameterName}' is none of the following types: ${types.join(", ")}`)
132
- }
package/src/types.ts DELETED
@@ -1,123 +0,0 @@
1
- /**
2
- * @module Types
3
- * This module provides utility functions to check data types.
4
- * It defines a set of supported types and includes functions to check if
5
- * a value is defined and if it matches a supported type. These functions
6
- * are useful for type checking and validation in the other libraries,
7
- * enhancing code robustness and reliability.
8
- */
9
-
10
- // The list of types supported by this utility functions.
11
- const supportedTypes = ["number", "string", "function", "array", "uint8array", "object", "bigint"] as const
12
-
13
- // Type extracted from the list above.
14
- export type SupportedType = (typeof supportedTypes)[number]
15
-
16
- /**
17
- * It returns true if the value is defined, false otherwise.
18
- * @param value The value to be checked.
19
- * @returns True or false.
20
- */
21
- export function isDefined(value: any): boolean {
22
- return typeof value !== "undefined"
23
- }
24
-
25
- /**
26
- * It returns true if the value is a number, false otherwise.
27
- * @param value The value to be checked.
28
- * @returns True or false.
29
- */
30
- export function isNumber(value: any): boolean {
31
- return typeof value === "number"
32
- }
33
-
34
- /**
35
- * It returns true if the value is a string, false otherwise.
36
- * @param value The value to be checked.
37
- * @returns True or false.
38
- */
39
- export function isString(value: any): boolean {
40
- return typeof value === "string"
41
- }
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
- export function isFunction(value: any): boolean {
49
- return typeof value === "function"
50
- }
51
-
52
- /**
53
- * It returns true if the value is an array, false otherwise.
54
- * @param value The value to be checked.
55
- * @returns True or false.
56
- */
57
- export function isArray(value: any): boolean {
58
- return typeof value === "object" && Array.isArray(value)
59
- }
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
- export function isUint8Array(value: any): boolean {
67
- return value instanceof Uint8Array
68
- }
69
-
70
- /**
71
- * It returns true if the value is an object, false otherwise.
72
- * @param value The value to be checked.
73
- * @returns True or false.
74
- */
75
- export function isObject(value: any): boolean {
76
- return typeof value === "object"
77
- }
78
-
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
-
88
- /**
89
- * It returns true if the value type is the same as the type passed
90
- * as the second parameter, false otherwise.
91
- * @param type The expected type.
92
- * @returns True or false.
93
- */
94
- export function isType(value: any, type: SupportedType): boolean {
95
- switch (type) {
96
- case "number":
97
- return isNumber(value)
98
- case "string":
99
- return isString(value)
100
- case "function":
101
- return isFunction(value)
102
- case "array":
103
- return isArray(value)
104
- case "uint8array":
105
- return isUint8Array(value)
106
- case "object":
107
- return isObject(value)
108
- case "bigint":
109
- return isBigInt(value)
110
- default:
111
- return false
112
- }
113
- }
114
-
115
- /**
116
- * Return true if the type is being supported by this utility
117
- * functions, false otherwise.
118
- * @param type The type to be checked.
119
- * @returns True or false
120
- */
121
- export function isSupportedType(type: string): type is SupportedType {
122
- return (supportedTypes as readonly string[]).includes(type)
123
- }