@alextheman/utility 5.14.0 → 5.15.1

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,5 +1,5 @@
1
1
  //#region src/v6/CodeError.d.ts
2
- interface ExpectErrorOptions$1<ErrorCode extends string = string> {
2
+ interface ExpectErrorOptions<ErrorCode extends string = string> {
3
3
  expectedCode?: ErrorCode;
4
4
  }
5
5
  /**
@@ -24,8 +24,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
24
24
  *
25
25
  * @returns `true` if the input is a CodeError, and `false` otherwise. The type of the input will also be narrowed down to CodeError if `true`.
26
26
  */
27
- static check(input: unknown): input is CodeError<string>;
28
- protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
27
+ static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
28
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
29
+ /**
30
+ * Check a `CodeError` against its error code
31
+ *
32
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
33
+ *
34
+ * @template ErrorCode The type of the error code
35
+ *
36
+ * @param input - The input to check.
37
+ * @param code - The expected code of the resulting error.
38
+ *
39
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
40
+ */
41
+ static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
29
42
  /**
30
43
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
31
44
  *
@@ -37,7 +50,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
37
50
  *
38
51
  * @returns The `CodeError` that was thrown by the `errorFunction`
39
52
  */
40
- static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
53
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
41
54
  /**
42
55
  * Gets the thrown `CodeError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
43
56
  *
@@ -49,31 +62,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
49
62
  *
50
63
  * @returns The `CodeError` that was thrown by the `errorFunction`
51
64
  */
52
- static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
65
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
53
66
  }
54
67
  //#endregion
55
- //#region src/root/types/CreateEnumType.d.ts
56
- /**
57
- * Get the value types from a const object so the object can behave similarly to an enum.
58
- *
59
- * @category Types
60
- *
61
- * @template ObjectType - The type of the object to get the value types for.
62
- */
63
- type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
64
- //#endregion
65
- //#region src/root/types/IsTypeArgumentString.d.ts
66
- type IsTypeArgumentString<Argument extends string> = Argument;
67
- //#endregion
68
68
  //#region src/v6/DataError.d.ts
69
- type DefaultDataErrorCode = "INVALID_DATA";
70
- interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
71
- expectedCode?: ErrorCode | DefaultDataErrorCode;
72
- }
73
- declare const DataErrorCode: {
74
- readonly INVALID_DATA: "INVALID_DATA";
75
- };
76
- type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
77
69
  /**
78
70
  * Represents errors you may get that may've been caused by a specific piece of data.
79
71
  *
@@ -81,7 +73,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
81
73
  *
82
74
  * @template DataType - The type of the data that caused the error.
83
75
  */
84
- declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
76
+ declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
85
77
  data: DataType;
86
78
  /**
87
79
  * @param data - The data that caused the error.
@@ -89,7 +81,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
89
81
  * @param message - A human-readable error message (e.g. The data provided is invalid).
90
82
  * @param options - Extra options to pass to super Error constructor.
91
83
  */
92
- constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
84
+ constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
93
85
  /**
94
86
  * Checks whether the given input may have been caused by a DataError.
95
87
  *
@@ -97,7 +89,20 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
97
89
  *
98
90
  * @returns `true` if the input is a DataError, and `false` otherwise. The type of the input will also be narrowed down to DataError if `true`.
99
91
  */
100
- static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError<DataType, ErrorCode>;
92
+ static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError<DataType, ErrorCode>;
93
+ /**
94
+ * Check a `DataError` against its error code
95
+ *
96
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
97
+ *
98
+ * @template ErrorCode The type of the error code
99
+ *
100
+ * @param input - The input to check.
101
+ * @param code - The expected code of the resulting error.
102
+ *
103
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
104
+ */
105
+ static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError<DataType, ErrorCode>;
101
106
  /**
102
107
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
103
108
  *
@@ -109,7 +114,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
109
114
  *
110
115
  * @returns The `DataError` that was thrown by the `errorFunction`
111
116
  */
112
- static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
117
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
113
118
  /**
114
119
  * Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
115
120
  *
@@ -121,7 +126,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
121
126
  *
122
127
  * @returns The `DataError` that was thrown by the `errorFunction`
123
128
  */
124
- static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
129
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
125
130
  }
126
131
  //#endregion
127
132
  //#region src/v6/sayHello.d.ts
@@ -134,4 +139,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
134
139
  */
135
140
  declare function sayHello(): string;
136
141
  //#endregion
137
- export { CodeError, DataError, DataErrorCode, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };
142
+ //#region src/root/types/IsTypeArgumentString.d.ts
143
+ type IsTypeArgumentString<Argument extends string> = Argument;
144
+ //#endregion
145
+ export { CodeError, DataError, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };
@@ -1,5 +1,5 @@
1
1
  //#region src/v6/CodeError.d.ts
2
- interface ExpectErrorOptions$1<ErrorCode extends string = string> {
2
+ interface ExpectErrorOptions<ErrorCode extends string = string> {
3
3
  expectedCode?: ErrorCode;
4
4
  }
5
5
  /**
@@ -24,8 +24,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
24
24
  *
25
25
  * @returns `true` if the input is a CodeError, and `false` otherwise. The type of the input will also be narrowed down to CodeError if `true`.
26
26
  */
27
- static check(input: unknown): input is CodeError<string>;
28
- protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
27
+ static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
28
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
29
+ /**
30
+ * Check a `CodeError` against its error code
31
+ *
32
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
33
+ *
34
+ * @template ErrorCode The type of the error code
35
+ *
36
+ * @param input - The input to check.
37
+ * @param code - The expected code of the resulting error.
38
+ *
39
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
40
+ */
41
+ static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
29
42
  /**
30
43
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
31
44
  *
@@ -37,7 +50,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
37
50
  *
38
51
  * @returns The `CodeError` that was thrown by the `errorFunction`
39
52
  */
40
- static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
53
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
41
54
  /**
42
55
  * Gets the thrown `CodeError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
43
56
  *
@@ -49,31 +62,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
49
62
  *
50
63
  * @returns The `CodeError` that was thrown by the `errorFunction`
51
64
  */
52
- static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
65
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
53
66
  }
54
67
  //#endregion
55
- //#region src/root/types/CreateEnumType.d.ts
56
- /**
57
- * Get the value types from a const object so the object can behave similarly to an enum.
58
- *
59
- * @category Types
60
- *
61
- * @template ObjectType - The type of the object to get the value types for.
62
- */
63
- type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
64
- //#endregion
65
- //#region src/root/types/IsTypeArgumentString.d.ts
66
- type IsTypeArgumentString<Argument extends string> = Argument;
67
- //#endregion
68
68
  //#region src/v6/DataError.d.ts
69
- type DefaultDataErrorCode = "INVALID_DATA";
70
- interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
71
- expectedCode?: ErrorCode | DefaultDataErrorCode;
72
- }
73
- declare const DataErrorCode: {
74
- readonly INVALID_DATA: "INVALID_DATA";
75
- };
76
- type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
77
69
  /**
78
70
  * Represents errors you may get that may've been caused by a specific piece of data.
79
71
  *
@@ -81,7 +73,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
81
73
  *
82
74
  * @template DataType - The type of the data that caused the error.
83
75
  */
84
- declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
76
+ declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
85
77
  data: DataType;
86
78
  /**
87
79
  * @param data - The data that caused the error.
@@ -89,7 +81,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
89
81
  * @param message - A human-readable error message (e.g. The data provided is invalid).
90
82
  * @param options - Extra options to pass to super Error constructor.
91
83
  */
92
- constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
84
+ constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
93
85
  /**
94
86
  * Checks whether the given input may have been caused by a DataError.
95
87
  *
@@ -97,7 +89,20 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
97
89
  *
98
90
  * @returns `true` if the input is a DataError, and `false` otherwise. The type of the input will also be narrowed down to DataError if `true`.
99
91
  */
100
- static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError<DataType, ErrorCode>;
92
+ static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError<DataType, ErrorCode>;
93
+ /**
94
+ * Check a `DataError` against its error code
95
+ *
96
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
97
+ *
98
+ * @template ErrorCode The type of the error code
99
+ *
100
+ * @param input - The input to check.
101
+ * @param code - The expected code of the resulting error.
102
+ *
103
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
104
+ */
105
+ static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError<DataType, ErrorCode>;
101
106
  /**
102
107
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
103
108
  *
@@ -109,7 +114,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
109
114
  *
110
115
  * @returns The `DataError` that was thrown by the `errorFunction`
111
116
  */
112
- static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
117
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
113
118
  /**
114
119
  * Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
115
120
  *
@@ -121,7 +126,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
121
126
  *
122
127
  * @returns The `DataError` that was thrown by the `errorFunction`
123
128
  */
124
- static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
129
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
125
130
  }
126
131
  //#endregion
127
132
  //#region src/v6/sayHello.d.ts
@@ -134,4 +139,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
134
139
  */
135
140
  declare function sayHello(): string;
136
141
  //#endregion
137
- export { CodeError, DataError, DataErrorCode, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };
142
+ //#region src/root/types/IsTypeArgumentString.d.ts
143
+ type IsTypeArgumentString<Argument extends string> = Argument;
144
+ //#endregion
145
+ export { CodeError, DataError, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };
package/dist/v6/index.js CHANGED
@@ -183,6 +183,21 @@ var CodeError = class CodeError extends Error {
183
183
  throw error;
184
184
  }
185
185
  /**
186
+ * Check a `CodeError` against its error code
187
+ *
188
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
189
+ *
190
+ * @template ErrorCode The type of the error code
191
+ *
192
+ * @param input - The input to check.
193
+ * @param code - The expected code of the resulting error.
194
+ *
195
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
196
+ */
197
+ static checkWithCode(input, code) {
198
+ return this.check(input) && input.code === code;
199
+ }
200
+ /**
186
201
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
187
202
  *
188
203
  * @param errorFunction - The function expected to throw the error.
@@ -223,7 +238,6 @@ var CodeError = class CodeError extends Error {
223
238
  };
224
239
  //#endregion
225
240
  //#region src/v6/DataError.ts
226
- const DataErrorCode = { INVALID_DATA: "INVALID_DATA" };
227
241
  /**
228
242
  * Represents errors you may get that may've been caused by a specific piece of data.
229
243
  *
@@ -239,11 +253,10 @@ var DataError = class DataError extends CodeError {
239
253
  * @param message - A human-readable error message (e.g. The data provided is invalid).
240
254
  * @param options - Extra options to pass to super Error constructor.
241
255
  */
242
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
256
+ constructor(data, code, message = "The data provided is invalid", options) {
243
257
  super(code, message, options);
244
258
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
245
259
  this.name = new.target.name;
246
- this.code = code;
247
260
  this.data = data;
248
261
  Object.defineProperty(this, "message", { enumerable: true });
249
262
  Object.setPrototypeOf(this, new.target.prototype);
@@ -260,6 +273,21 @@ var DataError = class DataError extends CodeError {
260
273
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
261
274
  }
262
275
  /**
276
+ * Check a `DataError` against its error code
277
+ *
278
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
279
+ *
280
+ * @template ErrorCode The type of the error code
281
+ *
282
+ * @param input - The input to check.
283
+ * @param code - The expected code of the resulting error.
284
+ *
285
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
286
+ */
287
+ static checkWithCode(input, code) {
288
+ return this.check(input) && input.code === code;
289
+ }
290
+ /**
263
291
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
264
292
  *
265
293
  * @param errorFunction - The function expected to throw the error.
@@ -402,4 +430,4 @@ I'll commit to you!
402
430
  `;
403
431
  }
404
432
  //#endregion
405
- export { CodeError, DataError, DataErrorCode, sayHello };
433
+ export { CodeError, DataError, sayHello };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alextheman/utility",
3
- "version": "5.14.0",
3
+ "version": "5.15.1",
4
4
  "description": "Helpful utility functions.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -41,9 +41,9 @@
41
41
  "zod": "4.3.6"
42
42
  },
43
43
  "devDependencies": {
44
- "@alextheman/eslint-plugin": "5.13.0",
44
+ "@alextheman/eslint-plugin": "5.13.1",
45
45
  "@types/node": "25.6.0",
46
- "alex-c-line": "2.7.0",
46
+ "alex-c-line": "2.7.1",
47
47
  "cross-env": "10.1.0",
48
48
  "dotenv-cli": "11.0.0",
49
49
  "eslint": "10.2.1",
@@ -58,9 +58,9 @@
58
58
  "typedoc-plugin-markdown": "4.11.0",
59
59
  "typedoc-rhineai-theme": "1.2.0",
60
60
  "typescript": "6.0.3",
61
- "typescript-eslint": "8.58.2",
62
- "vite": "8.0.8",
63
- "vitest": "4.1.4"
61
+ "typescript-eslint": "8.59.0",
62
+ "vite": "8.0.10",
63
+ "vitest": "4.1.5"
64
64
  },
65
65
  "engines": {
66
66
  "node": ">=22.3.0"