@alextheman/utility 5.11.3 → 5.12.0

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.
@@ -0,0 +1,410 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/root/functions/arrayHelpers/fillArray.ts
3
+ /**
4
+ * Creates a new array where each element is the result of the provided callback.
5
+ *
6
+ * If the callback returns at least one Promise, the entire result will be wrapped
7
+ * in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
8
+ *
9
+ * @category Array Helpers
10
+ *
11
+ * @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
12
+ *
13
+ * @param callback - A function invoked with the current index. May return a value or a Promise.
14
+ * @param length - The desired length of the resulting array.
15
+ *
16
+ * @returns An array of the callback results, or a Promise resolving to one if the callback is async.
17
+ */
18
+ function fillArray(callback, length = 1) {
19
+ const outputArray = new Array(length).fill(null).map((_, index) => {
20
+ return callback(index);
21
+ });
22
+ if (outputArray.some((item) => {
23
+ return item instanceof Promise;
24
+ })) return Promise.all(outputArray);
25
+ return outputArray;
26
+ }
27
+ //#endregion
28
+ //#region src/root/functions/arrayHelpers/paralleliseArrays.ts
29
+ /**
30
+ * Creates a new array of tuples, each containing the item at the given index from both arrays.
31
+ *
32
+ * If `secondArray` is shorter than `firstArray`, the second position in the tuple
33
+ * will be `undefined`. Iteration always uses the length of the first array.
34
+ *
35
+ * @category Array Helpers
36
+ *
37
+ * @template FirstArrayItem
38
+ * @template SecondArrayItem
39
+ *
40
+ * @param firstArray - The first array. Each item in this will take up the first tuple spot.
41
+ * @param secondArray - The second array. Each item in this will take up the second tuple spot.
42
+ *
43
+ * @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
44
+ */
45
+ function paralleliseArrays(firstArray, secondArray) {
46
+ const outputArray = [];
47
+ for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
48
+ return outputArray;
49
+ }
50
+ //#endregion
51
+ //#region src/root/functions/taggedTemplate/interpolate.ts
52
+ /**
53
+ * Returns the result of interpolating a template string when given the strings and interpolations separately.
54
+ *
55
+ * You can pass a template string directly by doing:
56
+ *
57
+ * ```
58
+ * interpolate`Template string here`;
59
+ * ```
60
+ *
61
+ * In this case, it will be functionally the same as if you just wrote the template string by itself.
62
+ *
63
+ * @category Tagged Template
64
+ *
65
+ * @template InterpolationsType - The type of the interpolations.
66
+ *
67
+ * @param strings - The strings from the template to process.
68
+ * @param interpolations - An array of all interpolations from the template.
69
+ *
70
+ * @returns A new string with the strings and interpolations from the template applied.
71
+ */
72
+ function interpolate(strings, ...interpolations) {
73
+ let result = "";
74
+ for (const [string, interpolation = ""] of paralleliseArrays(strings, interpolations)) result += string + interpolation;
75
+ return result;
76
+ }
77
+ //#endregion
78
+ //#region src/root/functions/taggedTemplate/normaliseIndents.ts
79
+ function calculateTabSize(line, whitespaceLength) {
80
+ const potentialWhitespacePart = line.slice(0, whitespaceLength);
81
+ const trimmedString = line.trimStart();
82
+ if (potentialWhitespacePart.trim() !== "") return 0;
83
+ const tabSize = line.length - (trimmedString.length + whitespaceLength);
84
+ return tabSize < 0 ? 0 : tabSize;
85
+ }
86
+ function getWhitespaceLength(lines) {
87
+ const [firstNonEmptyLine] = lines.filter((line) => {
88
+ return line.trim() !== "";
89
+ });
90
+ return firstNonEmptyLine.length - firstNonEmptyLine.trimStart().length;
91
+ }
92
+ function reduceLines(lines, { preserveTabs = true }) {
93
+ const slicedLines = lines.slice(1);
94
+ const isFirstLineEmpty = lines[0].trim() === "";
95
+ const whitespaceLength = getWhitespaceLength(isFirstLineEmpty ? lines : slicedLines);
96
+ return (isFirstLineEmpty ? slicedLines : lines).map((line) => {
97
+ const tabSize = calculateTabSize(line, whitespaceLength);
98
+ return (preserveTabs ? fillArray(() => {
99
+ return " ";
100
+ }, tabSize).join("") : "") + line.trimStart();
101
+ }).join("\n");
102
+ }
103
+ /**
104
+ * Applies any options if provided, then removes any extraneous indents from a multi-line template string.
105
+ *
106
+ * You can pass a template string directly by doing:
107
+ *
108
+ * ```typescript
109
+ * normaliseIndents`Template string here
110
+ * with a new line
111
+ * and another new line`;
112
+ * ```
113
+ *
114
+ * You may also pass the options first, then invoke the resulting function with a template string:
115
+ *
116
+ * ```typescript
117
+ * normaliseIndents({ preserveTabs: false })`Template string here
118
+ * with a new line
119
+ * and another new line`;
120
+ * ```
121
+ *
122
+ * @category Tagged Template
123
+ *
124
+ * @param first - The strings from the template to process, or the options to apply.
125
+ * @param args - An array of all interpolations from the template.
126
+ *
127
+ * @returns An additional function to invoke, or a new string with the strings and interpolations from the template applied, and extraneous indents removed.
128
+ */
129
+ function normaliseIndents(first, ...args) {
130
+ if (typeof first === "object" && first !== null && !Array.isArray(first)) {
131
+ const options = first;
132
+ return (strings, ...interpolations) => {
133
+ return normaliseIndents(strings, ...interpolations, options);
134
+ };
135
+ }
136
+ const strings = first;
137
+ const options = typeof args[args.length - 1] === "object" && !Array.isArray(args[args.length - 1]) ? args.pop() : {};
138
+ return reduceLines(interpolate(strings, ...[...args]).split("\n"), options);
139
+ }
140
+ //#endregion
141
+ //#region src/root/functions/miscellaneous/sayHello.ts
142
+ /**
143
+ * Returns a string representing the lyrics to the package's theme song, Commit To You
144
+ *
145
+ * [Pls listen!](https://www.youtube.com/watch?v=mH-Sg-8EnxM)
146
+ *
147
+ * @returns The lyrics string in markdown format.
148
+ */
149
+ function sayHello() {
150
+ return normaliseIndents`
151
+ # Commit To You
152
+
153
+ ### Verse 1
154
+
155
+ I know you've been checking me out,
156
+ Shall we take it to the next level now?
157
+ 'Cause I really wanna be there all for you,
158
+ All for you!
159
+ Come on now, let's make a fresh start!
160
+ Pin my number, then you can take me out!
161
+ Can't you see I really do care about you,
162
+ About you!
163
+
164
+ ### Pre-chorus 1
165
+ Although our calendars are imperfect, at best,
166
+ I'd like to organise time with you! (with you!).
167
+ Just tell me when and I'll make it clear,
168
+ All clear for you,
169
+ All clear for you!
170
+ (One, two, three, go!)
171
+
172
+ ### Chorus
173
+ I wanna be of utility, I'll help you on the run!
174
+ I'll be the one here in the back, while you go have some fun!
175
+ Looking out for you tonight, I'll be the one you can rely on!
176
+ Watch you go and watch me pass by,
177
+ I'll be here!
178
+ I'll commit to you!
179
+
180
+ ### Verse 2
181
+ Though sometimes it won't be easy,
182
+ You'll be here to bring out the best in me,
183
+ And I'll hold myself to high standards for you!
184
+ All for you!
185
+ We'll grow as a pair, you and me,
186
+ We'll build up a healthy dependency,
187
+ You can build with me and I'll develop with you!
188
+ I'm with you!
189
+
190
+ ### Pre-chorus 2
191
+ I'll be with you when you're up or you're down,
192
+ We'll deal with all our problems together (together!)
193
+ Just tell me what you want, I'll make it clear,
194
+ All clear for you,
195
+ All clear for you!
196
+ (One, three, one, go!)
197
+
198
+ ### Chorus
199
+ I wanna be of utility, I'll help you on the run!
200
+ (help you on the run!)
201
+ I'll be the one here in the back, while you go have some fun!
202
+ (you go have some fun!)
203
+ Looking out for you tonight, I'll be the one you can rely on!
204
+ Watch you go and watch me pass by,
205
+ I'll be here!
206
+ I'll commit to you!
207
+
208
+ ### Bridge
209
+ Looking into our stack!
210
+ I'll commit to you!
211
+ We've got a lot to unpack!
212
+ I'll commit to you!
213
+ The environment that we're in!
214
+ I'll commit to you!
215
+ Delicate as a string!
216
+ I'll commit to you!
217
+
218
+ But I think you're my type!
219
+ I'll commit to you!
220
+ Oh, this feels all so right!
221
+ I'll commit to you!
222
+ Nothing stopping us now!
223
+ I'll commit to you!
224
+ Let's show them what we're about!
225
+ Two, three, four, go!
226
+
227
+ ### Final Chorus
228
+ I wanna be of utility, I'll help you on the run!
229
+ (help you on the run!)
230
+ I'll be the one here in the back, while you go have some fun!
231
+ (you go have some fun!)
232
+ Looking out for you tonight, I'll be the one you can rely on!
233
+ Watch you go and watch me pass by,
234
+ I'll be here!
235
+ I'll commit to you!
236
+
237
+ I wanna be of utility, I'll help you on the run!
238
+ (I'll commit to you!)
239
+ I'll be the one here in the back, while you go have some fun!
240
+ (I'll commit to you!)
241
+ Looking out for you tonight, I'll be the one you can rely on!
242
+ (I'll commit to you!)
243
+ Watch you go and watch me pass by,
244
+ (I'll commit to you!)
245
+ I'll be here!
246
+
247
+ ### Outro
248
+ I'll commit to you!
249
+ I'll commit to you!
250
+ I'll commit to you!
251
+ `;
252
+ }
253
+ //#endregion
254
+ //#region src/v6/CodeError.ts
255
+ /**
256
+ * Represents errors that can be described using a standardised error code, and a human-readable error message.
257
+ *
258
+ * @category Types
259
+ *
260
+ * @template ErrorCode The type of the standardised error code.
261
+ */
262
+ var CodeError = class CodeError extends Error {
263
+ code;
264
+ /**
265
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
266
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
267
+ * @param options - Extra options to pass to super Error constructor.
268
+ */
269
+ constructor(code, message = "Something went wrong.", options) {
270
+ super(message, options);
271
+ if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
272
+ this.name = new.target.name;
273
+ this.code = code;
274
+ Object.defineProperty(this, "message", { enumerable: true });
275
+ Object.setPrototypeOf(this, new.target.prototype);
276
+ }
277
+ /**
278
+ * Checks whether the given input may have been caused by a CodeError.
279
+ *
280
+ * @param input - The input to check.
281
+ *
282
+ * @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`.
283
+ */
284
+ static check(input) {
285
+ if (input instanceof CodeError) return true;
286
+ return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string";
287
+ }
288
+ static checkCaughtError(error, options) {
289
+ if (this.check(error)) {
290
+ if (options?.expectedCode && error.code !== options.expectedCode) throw new Error(normaliseIndents`The error code on the thrown error does not match the expected error code.
291
+
292
+ Expected: ${options.expectedCode}
293
+ Received: ${error.code}
294
+ `, { cause: error });
295
+ return error;
296
+ }
297
+ throw error;
298
+ }
299
+ /**
300
+ * 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.
301
+ *
302
+ * @param errorFunction - The function expected to throw the error.
303
+ * @param options - Extra options to apply.
304
+ *
305
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
306
+ * @throws {Error} If no `CodeError` was thrown by the `errorFunction`
307
+ *
308
+ * @returns The `CodeError` that was thrown by the `errorFunction`
309
+ */
310
+ static expectError(errorFunction, options) {
311
+ try {
312
+ errorFunction();
313
+ } catch (error) {
314
+ return this.checkCaughtError(error, options);
315
+ }
316
+ throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
317
+ }
318
+ /**
319
+ * 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.
320
+ *
321
+ * @param errorFunction - The function expected to throw the error.
322
+ * @param options - Extra options to apply.
323
+ *
324
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
325
+ * @throws {Error} If no `CodeError` was thrown by the `errorFunction`
326
+ *
327
+ * @returns The `CodeError` that was thrown by the `errorFunction`
328
+ */
329
+ static async expectErrorAsync(errorFunction, options) {
330
+ try {
331
+ await errorFunction();
332
+ } catch (error) {
333
+ return this.checkCaughtError(error, options);
334
+ }
335
+ throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
336
+ }
337
+ };
338
+ //#endregion
339
+ //#region src/v6/DataError.ts
340
+ const DataErrorCode = { INVALID_DATA: "INVALID_DATA" };
341
+ /**
342
+ * Represents errors you may get that may've been caused by a specific piece of data.
343
+ *
344
+ * @category Types
345
+ *
346
+ * @template DataType - The type of the data that caused the error.
347
+ */
348
+ var DataError = class DataError extends CodeError {
349
+ data;
350
+ /**
351
+ * @param data - The data that caused the error.
352
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
353
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
354
+ * @param options - Extra options to pass to super Error constructor.
355
+ */
356
+ constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
357
+ super(code, message, options);
358
+ if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
359
+ this.name = new.target.name;
360
+ this.code = code;
361
+ this.data = data;
362
+ Object.defineProperty(this, "message", { enumerable: true });
363
+ Object.setPrototypeOf(this, new.target.prototype);
364
+ }
365
+ /**
366
+ * Checks whether the given input may have been caused by a DataError.
367
+ *
368
+ * @param input - The input to check.
369
+ *
370
+ * @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`.
371
+ */
372
+ static check(input) {
373
+ if (input instanceof DataError) return true;
374
+ const data = input;
375
+ return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
376
+ }
377
+ /**
378
+ * 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.
379
+ *
380
+ * @param errorFunction - The function expected to throw the error.
381
+ * @param options - Extra options to apply.
382
+ *
383
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
384
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
385
+ *
386
+ * @returns The `DataError` that was thrown by the `errorFunction`
387
+ */
388
+ static expectError(errorFunction, options) {
389
+ return super.expectError(errorFunction, options);
390
+ }
391
+ /**
392
+ * 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.
393
+ *
394
+ * @param errorFunction - The function expected to throw the error.
395
+ * @param options - Extra options to apply.
396
+ *
397
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
398
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
399
+ *
400
+ * @returns The `DataError` that was thrown by the `errorFunction`
401
+ */
402
+ static async expectErrorAsync(errorFunction, options) {
403
+ return await super.expectErrorAsync(errorFunction, options);
404
+ }
405
+ };
406
+ //#endregion
407
+ exports.CodeError = CodeError;
408
+ exports.DataError = DataError;
409
+ exports.DataErrorCode = DataErrorCode;
410
+ exports.sayHello = sayHello;
@@ -0,0 +1,137 @@
1
+ //#region src/root/functions/miscellaneous/sayHello.d.ts
2
+ /**
3
+ * Returns a string representing the lyrics to the package's theme song, Commit To You
4
+ *
5
+ * [Pls listen!](https://www.youtube.com/watch?v=mH-Sg-8EnxM)
6
+ *
7
+ * @returns The lyrics string in markdown format.
8
+ */
9
+ declare function sayHello(): string;
10
+ //#endregion
11
+ //#region src/v6/CodeError.d.ts
12
+ interface ExpectErrorOptions$1<ErrorCode extends string = string> {
13
+ expectedCode?: ErrorCode;
14
+ }
15
+ /**
16
+ * Represents errors that can be described using a standardised error code, and a human-readable error message.
17
+ *
18
+ * @category Types
19
+ *
20
+ * @template ErrorCode The type of the standardised error code.
21
+ */
22
+ declare class CodeError<ErrorCode extends string = string> extends Error {
23
+ code: ErrorCode;
24
+ /**
25
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
26
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
27
+ * @param options - Extra options to pass to super Error constructor.
28
+ */
29
+ constructor(code: ErrorCode, message?: string, options?: ErrorOptions);
30
+ /**
31
+ * Checks whether the given input may have been caused by a CodeError.
32
+ *
33
+ * @param input - The input to check.
34
+ *
35
+ * @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`.
36
+ */
37
+ static check(input: unknown): input is CodeError<string>;
38
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
39
+ /**
40
+ * 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.
41
+ *
42
+ * @param errorFunction - The function expected to throw the error.
43
+ * @param options - Extra options to apply.
44
+ *
45
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
46
+ * @throws {Error} If no `CodeError` was thrown by the `errorFunction`
47
+ *
48
+ * @returns The `CodeError` that was thrown by the `errorFunction`
49
+ */
50
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
51
+ /**
52
+ * 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.
53
+ *
54
+ * @param errorFunction - The function expected to throw the error.
55
+ * @param options - Extra options to apply.
56
+ *
57
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
58
+ * @throws {Error} If no `CodeError` was thrown by the `errorFunction`
59
+ *
60
+ * @returns The `CodeError` that was thrown by the `errorFunction`
61
+ */
62
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
63
+ }
64
+ //#endregion
65
+ //#region src/root/types/CreateEnumType.d.ts
66
+ /**
67
+ * Get the value types from a const object so the object can behave similarly to an enum.
68
+ *
69
+ * @category Types
70
+ *
71
+ * @template ObjectType - The type of the object to get the value types for.
72
+ */
73
+ type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
74
+ //#endregion
75
+ //#region src/root/types/IsTypeArgumentString.d.ts
76
+ type IsTypeArgumentString<Argument extends string> = Argument;
77
+ //#endregion
78
+ //#region src/v6/DataError.d.ts
79
+ type DefaultDataErrorCode = "INVALID_DATA";
80
+ interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
81
+ expectedCode?: ErrorCode | DefaultDataErrorCode;
82
+ }
83
+ declare const DataErrorCode: {
84
+ readonly INVALID_DATA: "INVALID_DATA";
85
+ };
86
+ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
87
+ /**
88
+ * Represents errors you may get that may've been caused by a specific piece of data.
89
+ *
90
+ * @category Types
91
+ *
92
+ * @template DataType - The type of the data that caused the error.
93
+ */
94
+ declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
95
+ data: DataType;
96
+ /**
97
+ * @param data - The data that caused the error.
98
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
99
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
100
+ * @param options - Extra options to pass to super Error constructor.
101
+ */
102
+ constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
103
+ /**
104
+ * Checks whether the given input may have been caused by a DataError.
105
+ *
106
+ * @param input - The input to check.
107
+ *
108
+ * @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`.
109
+ */
110
+ static check(input: unknown): input is DataError;
111
+ /**
112
+ * 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.
113
+ *
114
+ * @param errorFunction - The function expected to throw the error.
115
+ * @param options - Extra options to apply.
116
+ *
117
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
118
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
119
+ *
120
+ * @returns The `DataError` that was thrown by the `errorFunction`
121
+ */
122
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
123
+ /**
124
+ * 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.
125
+ *
126
+ * @param errorFunction - The function expected to throw the error.
127
+ * @param options - Extra options to apply.
128
+ *
129
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
130
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
131
+ *
132
+ * @returns The `DataError` that was thrown by the `errorFunction`
133
+ */
134
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
135
+ }
136
+ //#endregion
137
+ export { CodeError, DataError, DataErrorCode, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };
@@ -0,0 +1,137 @@
1
+ //#region src/root/functions/miscellaneous/sayHello.d.ts
2
+ /**
3
+ * Returns a string representing the lyrics to the package's theme song, Commit To You
4
+ *
5
+ * [Pls listen!](https://www.youtube.com/watch?v=mH-Sg-8EnxM)
6
+ *
7
+ * @returns The lyrics string in markdown format.
8
+ */
9
+ declare function sayHello(): string;
10
+ //#endregion
11
+ //#region src/v6/CodeError.d.ts
12
+ interface ExpectErrorOptions$1<ErrorCode extends string = string> {
13
+ expectedCode?: ErrorCode;
14
+ }
15
+ /**
16
+ * Represents errors that can be described using a standardised error code, and a human-readable error message.
17
+ *
18
+ * @category Types
19
+ *
20
+ * @template ErrorCode The type of the standardised error code.
21
+ */
22
+ declare class CodeError<ErrorCode extends string = string> extends Error {
23
+ code: ErrorCode;
24
+ /**
25
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
26
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
27
+ * @param options - Extra options to pass to super Error constructor.
28
+ */
29
+ constructor(code: ErrorCode, message?: string, options?: ErrorOptions);
30
+ /**
31
+ * Checks whether the given input may have been caused by a CodeError.
32
+ *
33
+ * @param input - The input to check.
34
+ *
35
+ * @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`.
36
+ */
37
+ static check(input: unknown): input is CodeError<string>;
38
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
39
+ /**
40
+ * 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.
41
+ *
42
+ * @param errorFunction - The function expected to throw the error.
43
+ * @param options - Extra options to apply.
44
+ *
45
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
46
+ * @throws {Error} If no `CodeError` was thrown by the `errorFunction`
47
+ *
48
+ * @returns The `CodeError` that was thrown by the `errorFunction`
49
+ */
50
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
51
+ /**
52
+ * 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.
53
+ *
54
+ * @param errorFunction - The function expected to throw the error.
55
+ * @param options - Extra options to apply.
56
+ *
57
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
58
+ * @throws {Error} If no `CodeError` was thrown by the `errorFunction`
59
+ *
60
+ * @returns The `CodeError` that was thrown by the `errorFunction`
61
+ */
62
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
63
+ }
64
+ //#endregion
65
+ //#region src/root/types/CreateEnumType.d.ts
66
+ /**
67
+ * Get the value types from a const object so the object can behave similarly to an enum.
68
+ *
69
+ * @category Types
70
+ *
71
+ * @template ObjectType - The type of the object to get the value types for.
72
+ */
73
+ type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
74
+ //#endregion
75
+ //#region src/root/types/IsTypeArgumentString.d.ts
76
+ type IsTypeArgumentString<Argument extends string> = Argument;
77
+ //#endregion
78
+ //#region src/v6/DataError.d.ts
79
+ type DefaultDataErrorCode = "INVALID_DATA";
80
+ interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
81
+ expectedCode?: ErrorCode | DefaultDataErrorCode;
82
+ }
83
+ declare const DataErrorCode: {
84
+ readonly INVALID_DATA: "INVALID_DATA";
85
+ };
86
+ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
87
+ /**
88
+ * Represents errors you may get that may've been caused by a specific piece of data.
89
+ *
90
+ * @category Types
91
+ *
92
+ * @template DataType - The type of the data that caused the error.
93
+ */
94
+ declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
95
+ data: DataType;
96
+ /**
97
+ * @param data - The data that caused the error.
98
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
99
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
100
+ * @param options - Extra options to pass to super Error constructor.
101
+ */
102
+ constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
103
+ /**
104
+ * Checks whether the given input may have been caused by a DataError.
105
+ *
106
+ * @param input - The input to check.
107
+ *
108
+ * @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`.
109
+ */
110
+ static check(input: unknown): input is DataError;
111
+ /**
112
+ * 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.
113
+ *
114
+ * @param errorFunction - The function expected to throw the error.
115
+ * @param options - Extra options to apply.
116
+ *
117
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
118
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
119
+ *
120
+ * @returns The `DataError` that was thrown by the `errorFunction`
121
+ */
122
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
123
+ /**
124
+ * 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.
125
+ *
126
+ * @param errorFunction - The function expected to throw the error.
127
+ * @param options - Extra options to apply.
128
+ *
129
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
130
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
131
+ *
132
+ * @returns The `DataError` that was thrown by the `errorFunction`
133
+ */
134
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
135
+ }
136
+ //#endregion
137
+ export { CodeError, DataError, DataErrorCode, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };