|
| 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 | + isDefined, |
| 15 | + isFunction, |
| 16 | + isNumber, |
| 17 | + isString, |
| 18 | + isSupportedType, |
| 19 | + isType, |
| 20 | + isUint8Array |
| 21 | +} from "./types" |
| 22 | + |
| 23 | +/** |
| 24 | + * It throws a type error if the parameter value has not been defined. |
| 25 | + * @param parameterValue The parameter value. |
| 26 | + * @param parameterName The parameter name. |
| 27 | + */ |
| 28 | +export function requireDefined(parameterValue: any, parameterName: string) { |
| 29 | + if (!isDefined(parameterValue)) { |
| 30 | + throw new TypeError(`Parameter '${parameterName}' is not defined`) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * It throws a type error if the parameter value is not a number. |
| 36 | + * @param parameterValue The parameter value. |
| 37 | + * @param parameterName The parameter name. |
| 38 | + */ |
| 39 | +export function requireNumber(parameterValue: number, parameterName: string) { |
| 40 | + if (!isNumber(parameterValue)) { |
| 41 | + throw new TypeError(`Parameter '${parameterName}' is not a number`) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * It throws a type error if the parameter value is not a string. |
| 47 | + * @param parameterValue The parameter value. |
| 48 | + * @param parameterName The parameter name. |
| 49 | + */ |
| 50 | +export function requireString(parameterValue: string, parameterName: string) { |
| 51 | + if (!isString(parameterValue)) { |
| 52 | + throw new TypeError(`Parameter '${parameterName}' is not a string`) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * It throws a type error if the parameter value is not a function. |
| 58 | + * @param parameterValue The parameter value. |
| 59 | + * @param parameterName The parameter name. |
| 60 | + */ |
| 61 | +export function requireFunction(parameterValue: Function, parameterName: string) { |
| 62 | + if (!isFunction(parameterValue)) { |
| 63 | + throw new TypeError(`Parameter '${parameterName}' is not a function`) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * It throws a type error if the parameter value is not an array. |
| 69 | + * @param parameterValue The parameter value. |
| 70 | + * @param parameterName The parameter name. |
| 71 | + */ |
| 72 | +export function requireArray(parameterValue: any[], parameterName: string) { |
| 73 | + if (!isArray(parameterValue)) { |
| 74 | + throw new TypeError(`Parameter '${parameterName}' is not an array`) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * It throws a type error if the parameter value is not a uint8array. |
| 80 | + * @param parameterValue The parameter value. |
| 81 | + * @param parameterName The parameter name. |
| 82 | + */ |
| 83 | +export function requireUint8Array(parameterValue: Uint8Array, parameterName: string) { |
| 84 | + if (!isUint8Array(parameterValue)) { |
| 85 | + throw new TypeError(`Parameter '${parameterName}' is not a Uint8Array`) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * It throws a type error if the parameter value type is not part of the list of types. |
| 91 | + * @param parameterValue The parameter value. |
| 92 | + * @param parameterName The parameter name. |
| 93 | + */ |
| 94 | +export function requireTypes(parameterValue: any, parameterName: string, types: SupportedType[]) { |
| 95 | + for (const type of types) { |
| 96 | + if (!isSupportedType(type)) { |
| 97 | + throw new Error(`Type '${type}' is not supported`) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + for (const type of types) { |
| 102 | + if (isType(parameterValue, type)) { |
| 103 | + return |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + throw new TypeError(`Parameter '${parameterName}' is none of the following types: ${types.join(", ")}`) |
| 108 | +} |
0 commit comments