@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.
package/dist/index.js CHANGED
@@ -109,6 +109,21 @@ var CodeError = class CodeError extends Error {
109
109
  throw error;
110
110
  }
111
111
  /**
112
+ * Check a `CodeError` against its error code
113
+ *
114
+ * 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.
115
+ *
116
+ * @template ErrorCode The type of the error code
117
+ *
118
+ * @param input - The input to check.
119
+ * @param code - The expected code of the resulting error.
120
+ *
121
+ * @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`.
122
+ */
123
+ static checkWithCode(input, code) {
124
+ return this.check(input) && input.code === code;
125
+ }
126
+ /**
112
127
  * 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.
113
128
  *
114
129
  * @param errorFunction - The function expected to throw the error.
@@ -164,11 +179,10 @@ var DataError$1 = class DataError$1 extends CodeError {
164
179
  * @param message - A human-readable error message (e.g. The data provided is invalid).
165
180
  * @param options - Extra options to pass to super Error constructor.
166
181
  */
167
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
182
+ constructor(data, code, message = "The data provided is invalid", options) {
168
183
  super(code, message, options);
169
184
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
170
185
  this.name = new.target.name;
171
- this.code = code;
172
186
  this.data = data;
173
187
  Object.defineProperty(this, "message", { enumerable: true });
174
188
  Object.setPrototypeOf(this, new.target.prototype);
@@ -185,6 +199,21 @@ var DataError$1 = class DataError$1 extends CodeError {
185
199
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
186
200
  }
187
201
  /**
202
+ * Check a `DataError` against its error code
203
+ *
204
+ * 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.
205
+ *
206
+ * @template ErrorCode The type of the error code
207
+ *
208
+ * @param input - The input to check.
209
+ * @param code - The expected code of the resulting error.
210
+ *
211
+ * @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`.
212
+ */
213
+ static checkWithCode(input, code) {
214
+ return this.check(input) && input.code === code;
215
+ }
216
+ /**
188
217
  * 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.
189
218
  *
190
219
  * @param errorFunction - The function expected to throw the error.
@@ -1777,4 +1806,20 @@ var DataError = class DataError extends Error {
1777
1806
  }
1778
1807
  };
1779
1808
  //#endregion
1780
- export { APIError, DataError, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, ONE_DAY_IN_MILLISECONDS, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, VersionType, addDaysToDate, appendSemicolon, az, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
1809
+ //#region src/root/errors/assertNotNull.ts
1810
+ /**
1811
+ * Asserts that a given input is not `null`, and throws a DataError if it does.
1812
+ *
1813
+ * If no error is thrown from this, the input type gets narrowed down to not include `null`.
1814
+ *
1815
+ * @template InputType The type of the input.
1816
+ *
1817
+ * @param input - The input to assert against
1818
+ *
1819
+ * @throws {DataError} If the input is `null`.
1820
+ */
1821
+ function assertNotNull(input) {
1822
+ if (input === null) throw new DataError$1({ input }, "NULL_INPUT", "Expected the input not to be null");
1823
+ }
1824
+ //#endregion
1825
+ export { APIError, DataError, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, ONE_DAY_IN_MILLISECONDS, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, VersionType, addDaysToDate, appendSemicolon, assertNotNull, az, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
@@ -132,6 +132,21 @@ var CodeError = class CodeError extends Error {
132
132
  throw error;
133
133
  }
134
134
  /**
135
+ * Check a `CodeError` against its error code
136
+ *
137
+ * 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.
138
+ *
139
+ * @template ErrorCode The type of the error code
140
+ *
141
+ * @param input - The input to check.
142
+ * @param code - The expected code of the resulting error.
143
+ *
144
+ * @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`.
145
+ */
146
+ static checkWithCode(input, code) {
147
+ return this.check(input) && input.code === code;
148
+ }
149
+ /**
135
150
  * 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.
136
151
  *
137
152
  * @param errorFunction - The function expected to throw the error.
@@ -187,11 +202,10 @@ var DataError = class DataError extends CodeError {
187
202
  * @param message - A human-readable error message (e.g. The data provided is invalid).
188
203
  * @param options - Extra options to pass to super Error constructor.
189
204
  */
190
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
205
+ constructor(data, code, message = "The data provided is invalid", options) {
191
206
  super(code, message, options);
192
207
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
193
208
  this.name = new.target.name;
194
- this.code = code;
195
209
  this.data = data;
196
210
  Object.defineProperty(this, "message", { enumerable: true });
197
211
  Object.setPrototypeOf(this, new.target.prototype);
@@ -208,6 +222,21 @@ var DataError = class DataError extends CodeError {
208
222
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
209
223
  }
210
224
  /**
225
+ * Check a `DataError` against its error code
226
+ *
227
+ * 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.
228
+ *
229
+ * @template ErrorCode The type of the error code
230
+ *
231
+ * @param input - The input to check.
232
+ * @param code - The expected code of the resulting error.
233
+ *
234
+ * @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`.
235
+ */
236
+ static checkWithCode(input, code) {
237
+ return this.check(input) && input.code === code;
238
+ }
239
+ /**
211
240
  * 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.
212
241
  *
213
242
  * @param errorFunction - The function expected to throw the error.
@@ -25,7 +25,7 @@ type IsTypeArgumentString<Argument extends string> = Argument;
25
25
  type NullableOnCondition<Condition extends boolean, ResolvedTypeIfTrue> = Condition extends true ? ResolvedTypeIfTrue : ResolvedTypeIfTrue | null;
26
26
  //#endregion
27
27
  //#region src/v6/CodeError.d.ts
28
- interface ExpectErrorOptions$1<ErrorCode extends string = string> {
28
+ interface ExpectErrorOptions<ErrorCode extends string = string> {
29
29
  expectedCode?: ErrorCode;
30
30
  }
31
31
  /**
@@ -50,8 +50,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
50
50
  *
51
51
  * @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`.
52
52
  */
53
- static check(input: unknown): input is CodeError<string>;
54
- protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
53
+ static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
54
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
55
+ /**
56
+ * Check a `CodeError` against its error code
57
+ *
58
+ * 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.
59
+ *
60
+ * @template ErrorCode The type of the error code
61
+ *
62
+ * @param input - The input to check.
63
+ * @param code - The expected code of the resulting error.
64
+ *
65
+ * @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`.
66
+ */
67
+ static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
55
68
  /**
56
69
  * 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.
57
70
  *
@@ -63,7 +76,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
63
76
  *
64
77
  * @returns The `CodeError` that was thrown by the `errorFunction`
65
78
  */
66
- static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
79
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
67
80
  /**
68
81
  * 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.
69
82
  *
@@ -75,18 +88,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
75
88
  *
76
89
  * @returns The `CodeError` that was thrown by the `errorFunction`
77
90
  */
78
- static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
91
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
79
92
  }
80
93
  //#endregion
81
94
  //#region src/v6/DataError.d.ts
82
- type DefaultDataErrorCode = "INVALID_DATA";
83
- interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
84
- expectedCode?: ErrorCode | DefaultDataErrorCode;
85
- }
86
- declare const DataErrorCode: {
87
- readonly INVALID_DATA: "INVALID_DATA";
88
- };
89
- type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
90
95
  /**
91
96
  * Represents errors you may get that may've been caused by a specific piece of data.
92
97
  *
@@ -94,7 +99,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
94
99
  *
95
100
  * @template DataType - The type of the data that caused the error.
96
101
  */
97
- declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
102
+ declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
98
103
  data: DataType;
99
104
  /**
100
105
  * @param data - The data that caused the error.
@@ -102,7 +107,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
102
107
  * @param message - A human-readable error message (e.g. The data provided is invalid).
103
108
  * @param options - Extra options to pass to super Error constructor.
104
109
  */
105
- constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
110
+ constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
106
111
  /**
107
112
  * Checks whether the given input may have been caused by a DataError.
108
113
  *
@@ -110,7 +115,20 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
110
115
  *
111
116
  * @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`.
112
117
  */
113
- static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError<DataType, ErrorCode>;
118
+ static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError<DataType, ErrorCode>;
119
+ /**
120
+ * Check a `DataError` against its error code
121
+ *
122
+ * 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.
123
+ *
124
+ * @template ErrorCode The type of the error code
125
+ *
126
+ * @param input - The input to check.
127
+ * @param code - The expected code of the resulting error.
128
+ *
129
+ * @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`.
130
+ */
131
+ static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError<DataType, ErrorCode>;
114
132
  /**
115
133
  * 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.
116
134
  *
@@ -122,7 +140,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
122
140
  *
123
141
  * @returns The `DataError` that was thrown by the `errorFunction`
124
142
  */
125
- static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
143
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
126
144
  /**
127
145
  * 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.
128
146
  *
@@ -134,7 +152,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
134
152
  *
135
153
  * @returns The `DataError` that was thrown by the `errorFunction`
136
154
  */
137
- static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
155
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
138
156
  }
139
157
  //#endregion
140
158
  //#region src/internal/DependencyGroup.d.ts
@@ -25,7 +25,7 @@ type IsTypeArgumentString<Argument extends string> = Argument;
25
25
  type NullableOnCondition<Condition extends boolean, ResolvedTypeIfTrue> = Condition extends true ? ResolvedTypeIfTrue : ResolvedTypeIfTrue | null;
26
26
  //#endregion
27
27
  //#region src/v6/CodeError.d.ts
28
- interface ExpectErrorOptions$1<ErrorCode extends string = string> {
28
+ interface ExpectErrorOptions<ErrorCode extends string = string> {
29
29
  expectedCode?: ErrorCode;
30
30
  }
31
31
  /**
@@ -50,8 +50,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
50
50
  *
51
51
  * @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`.
52
52
  */
53
- static check(input: unknown): input is CodeError<string>;
54
- protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
53
+ static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
54
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
55
+ /**
56
+ * Check a `CodeError` against its error code
57
+ *
58
+ * 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.
59
+ *
60
+ * @template ErrorCode The type of the error code
61
+ *
62
+ * @param input - The input to check.
63
+ * @param code - The expected code of the resulting error.
64
+ *
65
+ * @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`.
66
+ */
67
+ static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
55
68
  /**
56
69
  * 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.
57
70
  *
@@ -63,7 +76,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
63
76
  *
64
77
  * @returns The `CodeError` that was thrown by the `errorFunction`
65
78
  */
66
- static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
79
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
67
80
  /**
68
81
  * 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.
69
82
  *
@@ -75,18 +88,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
75
88
  *
76
89
  * @returns The `CodeError` that was thrown by the `errorFunction`
77
90
  */
78
- static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
91
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
79
92
  }
80
93
  //#endregion
81
94
  //#region src/v6/DataError.d.ts
82
- type DefaultDataErrorCode = "INVALID_DATA";
83
- interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
84
- expectedCode?: ErrorCode | DefaultDataErrorCode;
85
- }
86
- declare const DataErrorCode: {
87
- readonly INVALID_DATA: "INVALID_DATA";
88
- };
89
- type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
90
95
  /**
91
96
  * Represents errors you may get that may've been caused by a specific piece of data.
92
97
  *
@@ -94,7 +99,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
94
99
  *
95
100
  * @template DataType - The type of the data that caused the error.
96
101
  */
97
- declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
102
+ declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
98
103
  data: DataType;
99
104
  /**
100
105
  * @param data - The data that caused the error.
@@ -102,7 +107,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
102
107
  * @param message - A human-readable error message (e.g. The data provided is invalid).
103
108
  * @param options - Extra options to pass to super Error constructor.
104
109
  */
105
- constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
110
+ constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
106
111
  /**
107
112
  * Checks whether the given input may have been caused by a DataError.
108
113
  *
@@ -110,7 +115,20 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
110
115
  *
111
116
  * @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`.
112
117
  */
113
- static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError<DataType, ErrorCode>;
118
+ static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError<DataType, ErrorCode>;
119
+ /**
120
+ * Check a `DataError` against its error code
121
+ *
122
+ * 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.
123
+ *
124
+ * @template ErrorCode The type of the error code
125
+ *
126
+ * @param input - The input to check.
127
+ * @param code - The expected code of the resulting error.
128
+ *
129
+ * @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`.
130
+ */
131
+ static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError<DataType, ErrorCode>;
114
132
  /**
115
133
  * 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.
116
134
  *
@@ -122,7 +140,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
122
140
  *
123
141
  * @returns The `DataError` that was thrown by the `errorFunction`
124
142
  */
125
- static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
143
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
126
144
  /**
127
145
  * 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.
128
146
  *
@@ -134,7 +152,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
134
152
  *
135
153
  * @returns The `DataError` that was thrown by the `errorFunction`
136
154
  */
137
- static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
155
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
138
156
  }
139
157
  //#endregion
140
158
  //#region src/internal/DependencyGroup.d.ts
@@ -107,6 +107,21 @@ var CodeError = class CodeError extends Error {
107
107
  throw error;
108
108
  }
109
109
  /**
110
+ * Check a `CodeError` against its error code
111
+ *
112
+ * 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.
113
+ *
114
+ * @template ErrorCode The type of the error code
115
+ *
116
+ * @param input - The input to check.
117
+ * @param code - The expected code of the resulting error.
118
+ *
119
+ * @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`.
120
+ */
121
+ static checkWithCode(input, code) {
122
+ return this.check(input) && input.code === code;
123
+ }
124
+ /**
110
125
  * 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.
111
126
  *
112
127
  * @param errorFunction - The function expected to throw the error.
@@ -162,11 +177,10 @@ var DataError = class DataError extends CodeError {
162
177
  * @param message - A human-readable error message (e.g. The data provided is invalid).
163
178
  * @param options - Extra options to pass to super Error constructor.
164
179
  */
165
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
180
+ constructor(data, code, message = "The data provided is invalid", options) {
166
181
  super(code, message, options);
167
182
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
168
183
  this.name = new.target.name;
169
- this.code = code;
170
184
  this.data = data;
171
185
  Object.defineProperty(this, "message", { enumerable: true });
172
186
  Object.setPrototypeOf(this, new.target.prototype);
@@ -183,6 +197,21 @@ var DataError = class DataError extends CodeError {
183
197
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
184
198
  }
185
199
  /**
200
+ * Check a `DataError` against its error code
201
+ *
202
+ * 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.
203
+ *
204
+ * @template ErrorCode The type of the error code
205
+ *
206
+ * @param input - The input to check.
207
+ * @param code - The expected code of the resulting error.
208
+ *
209
+ * @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`.
210
+ */
211
+ static checkWithCode(input, code) {
212
+ return this.check(input) && input.code === code;
213
+ }
214
+ /**
186
215
  * 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.
187
216
  *
188
217
  * @param errorFunction - The function expected to throw the error.
@@ -248,6 +248,21 @@ var CodeError = class CodeError extends Error {
248
248
  throw error;
249
249
  }
250
250
  /**
251
+ * Check a `CodeError` against its error code
252
+ *
253
+ * 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.
254
+ *
255
+ * @template ErrorCode The type of the error code
256
+ *
257
+ * @param input - The input to check.
258
+ * @param code - The expected code of the resulting error.
259
+ *
260
+ * @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`.
261
+ */
262
+ static checkWithCode(input, code) {
263
+ return this.check(input) && input.code === code;
264
+ }
265
+ /**
251
266
  * 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.
252
267
  *
253
268
  * @param errorFunction - The function expected to throw the error.
@@ -303,11 +318,10 @@ var DataError = class DataError extends CodeError {
303
318
  * @param message - A human-readable error message (e.g. The data provided is invalid).
304
319
  * @param options - Extra options to pass to super Error constructor.
305
320
  */
306
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
321
+ constructor(data, code, message = "The data provided is invalid", options) {
307
322
  super(code, message, options);
308
323
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
309
324
  this.name = new.target.name;
310
- this.code = code;
311
325
  this.data = data;
312
326
  Object.defineProperty(this, "message", { enumerable: true });
313
327
  Object.setPrototypeOf(this, new.target.prototype);
@@ -324,6 +338,21 @@ var DataError = class DataError extends CodeError {
324
338
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
325
339
  }
326
340
  /**
341
+ * Check a `DataError` against its error code
342
+ *
343
+ * 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.
344
+ *
345
+ * @template ErrorCode The type of the error code
346
+ *
347
+ * @param input - The input to check.
348
+ * @param code - The expected code of the resulting error.
349
+ *
350
+ * @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`.
351
+ */
352
+ static checkWithCode(input, code) {
353
+ return this.check(input) && input.code === code;
354
+ }
355
+ /**
327
356
  * 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.
328
357
  *
329
358
  * @param errorFunction - The function expected to throw the error.
@@ -224,6 +224,21 @@ var CodeError = class CodeError extends Error {
224
224
  throw error;
225
225
  }
226
226
  /**
227
+ * Check a `CodeError` against its error code
228
+ *
229
+ * 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.
230
+ *
231
+ * @template ErrorCode The type of the error code
232
+ *
233
+ * @param input - The input to check.
234
+ * @param code - The expected code of the resulting error.
235
+ *
236
+ * @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`.
237
+ */
238
+ static checkWithCode(input, code) {
239
+ return this.check(input) && input.code === code;
240
+ }
241
+ /**
227
242
  * 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.
228
243
  *
229
244
  * @param errorFunction - The function expected to throw the error.
@@ -279,11 +294,10 @@ var DataError = class DataError extends CodeError {
279
294
  * @param message - A human-readable error message (e.g. The data provided is invalid).
280
295
  * @param options - Extra options to pass to super Error constructor.
281
296
  */
282
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
297
+ constructor(data, code, message = "The data provided is invalid", options) {
283
298
  super(code, message, options);
284
299
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
285
300
  this.name = new.target.name;
286
- this.code = code;
287
301
  this.data = data;
288
302
  Object.defineProperty(this, "message", { enumerable: true });
289
303
  Object.setPrototypeOf(this, new.target.prototype);
@@ -300,6 +314,21 @@ var DataError = class DataError extends CodeError {
300
314
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
301
315
  }
302
316
  /**
317
+ * Check a `DataError` against its error code
318
+ *
319
+ * 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.
320
+ *
321
+ * @template ErrorCode The type of the error code
322
+ *
323
+ * @param input - The input to check.
324
+ * @param code - The expected code of the resulting error.
325
+ *
326
+ * @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`.
327
+ */
328
+ static checkWithCode(input, code) {
329
+ return this.check(input) && input.code === code;
330
+ }
331
+ /**
303
332
  * 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.
304
333
  *
305
334
  * @param errorFunction - The function expected to throw the error.
package/dist/v6/index.cjs CHANGED
@@ -184,6 +184,21 @@ var CodeError = class CodeError extends Error {
184
184
  throw error;
185
185
  }
186
186
  /**
187
+ * Check a `CodeError` against its error code
188
+ *
189
+ * 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.
190
+ *
191
+ * @template ErrorCode The type of the error code
192
+ *
193
+ * @param input - The input to check.
194
+ * @param code - The expected code of the resulting error.
195
+ *
196
+ * @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`.
197
+ */
198
+ static checkWithCode(input, code) {
199
+ return this.check(input) && input.code === code;
200
+ }
201
+ /**
187
202
  * 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.
188
203
  *
189
204
  * @param errorFunction - The function expected to throw the error.
@@ -224,7 +239,6 @@ var CodeError = class CodeError extends Error {
224
239
  };
225
240
  //#endregion
226
241
  //#region src/v6/DataError.ts
227
- const DataErrorCode = { INVALID_DATA: "INVALID_DATA" };
228
242
  /**
229
243
  * Represents errors you may get that may've been caused by a specific piece of data.
230
244
  *
@@ -240,11 +254,10 @@ var DataError = class DataError extends CodeError {
240
254
  * @param message - A human-readable error message (e.g. The data provided is invalid).
241
255
  * @param options - Extra options to pass to super Error constructor.
242
256
  */
243
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
257
+ constructor(data, code, message = "The data provided is invalid", options) {
244
258
  super(code, message, options);
245
259
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
246
260
  this.name = new.target.name;
247
- this.code = code;
248
261
  this.data = data;
249
262
  Object.defineProperty(this, "message", { enumerable: true });
250
263
  Object.setPrototypeOf(this, new.target.prototype);
@@ -261,6 +274,21 @@ var DataError = class DataError extends CodeError {
261
274
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
262
275
  }
263
276
  /**
277
+ * Check a `DataError` against its error code
278
+ *
279
+ * 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.
280
+ *
281
+ * @template ErrorCode The type of the error code
282
+ *
283
+ * @param input - The input to check.
284
+ * @param code - The expected code of the resulting error.
285
+ *
286
+ * @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`.
287
+ */
288
+ static checkWithCode(input, code) {
289
+ return this.check(input) && input.code === code;
290
+ }
291
+ /**
264
292
  * 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.
265
293
  *
266
294
  * @param errorFunction - The function expected to throw the error.
@@ -405,5 +433,4 @@ I'll commit to you!
405
433
  //#endregion
406
434
  exports.CodeError = CodeError;
407
435
  exports.DataError = DataError;
408
- exports.DataErrorCode = DataErrorCode;
409
436
  exports.sayHello = sayHello;