@alextheman/utility 5.1.3 → 5.2.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.
@@ -76,47 +76,6 @@ const FILE_PATH_REGEX = String.raw`^(?<directory>.+)[\/\\](?<base>[^\/\\]+)$`;
76
76
  //#region src/root/constants/VERSION_NUMBER_REGEX.ts
77
77
  const VERSION_NUMBER_REGEX = "^(?:v)?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$";
78
78
 
79
- //#endregion
80
- //#region src/root/types/DataError.ts
81
- /**
82
- * Represents errors you may get that may've been caused by a specific piece of data.
83
- *
84
- * @category Types
85
- *
86
- * @template DataType - The type of the data that caused the error.
87
- */
88
- var DataError = class DataError extends Error {
89
- code;
90
- data;
91
- /**
92
- * @param data - The data that caused the error.
93
- * @param code - A standardised code (e.g. UNEXPECTED_DATA).
94
- * @param message - A human-readable error message (e.g. The data provided is invalid).
95
- * @param options - Extra options to pass to super Error constructor.
96
- */
97
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
98
- super(message, options);
99
- if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
100
- this.name = new.target.name;
101
- this.code = code;
102
- this.data = data;
103
- Object.defineProperty(this, "message", { enumerable: true });
104
- Object.setPrototypeOf(this, new.target.prototype);
105
- }
106
- /**
107
- * Checks whether the given input may have been caused by a DataError.
108
- *
109
- * @param input - The input to check.
110
- *
111
- * @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
- */
113
- static check(input) {
114
- if (input instanceof DataError) return true;
115
- const data = input;
116
- return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
117
- }
118
- };
119
-
120
79
  //#endregion
121
80
  //#region src/root/functions/arrayHelpers/fillArray.ts
122
81
  /**
@@ -419,6 +378,96 @@ const VersionType = {
419
378
  PATCH: "patch"
420
379
  };
421
380
 
381
+ //#endregion
382
+ //#region src/root/types/DataError.ts
383
+ /**
384
+ * Represents errors you may get that may've been caused by a specific piece of data.
385
+ *
386
+ * @category Types
387
+ *
388
+ * @template DataType - The type of the data that caused the error.
389
+ */
390
+ var DataError = class DataError extends Error {
391
+ code;
392
+ data;
393
+ /**
394
+ * @param data - The data that caused the error.
395
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
396
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
397
+ * @param options - Extra options to pass to super Error constructor.
398
+ */
399
+ constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
400
+ super(message, options);
401
+ if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
402
+ this.name = new.target.name;
403
+ this.code = code;
404
+ this.data = data;
405
+ Object.defineProperty(this, "message", { enumerable: true });
406
+ Object.setPrototypeOf(this, new.target.prototype);
407
+ }
408
+ static checkCaughtError(error, options) {
409
+ if (DataError.check(error)) {
410
+ 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.
411
+
412
+ Expected: ${options.expectedCode}
413
+ Received: ${error.code}
414
+ `, { cause: error });
415
+ return error;
416
+ }
417
+ throw error;
418
+ }
419
+ /**
420
+ * Checks whether the given input may have been caused by a DataError.
421
+ *
422
+ * @param input - The input to check.
423
+ *
424
+ * @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`.
425
+ */
426
+ static check(input) {
427
+ if (input instanceof DataError) return true;
428
+ const data = input;
429
+ return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
430
+ }
431
+ /**
432
+ * 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.
433
+ *
434
+ * @param errorFunction - The function expected to throw the error.
435
+ * @param options - Extra options to apply.
436
+ *
437
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
438
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
439
+ *
440
+ * @returns The `DataError` that was thrown by the `errorFunction`
441
+ */
442
+ static expectError(errorFunction, options) {
443
+ try {
444
+ errorFunction();
445
+ } catch (error) {
446
+ return DataError.checkCaughtError(error, options);
447
+ }
448
+ throw new Error("Expected a DataError to be thrown but none was thrown");
449
+ }
450
+ /**
451
+ * 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.
452
+ *
453
+ * @param errorFunction - The function expected to throw the error.
454
+ * @param options - Extra options to apply.
455
+ *
456
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
457
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
458
+ *
459
+ * @returns The `DataError` that was thrown by the `errorFunction`
460
+ */
461
+ static async expectErrorAsync(errorFunction, options) {
462
+ try {
463
+ await errorFunction();
464
+ } catch (error) {
465
+ return DataError.checkCaughtError(error, options);
466
+ }
467
+ throw new Error("Expected a DataError to be thrown but none was thrown");
468
+ }
469
+ };
470
+
422
471
  //#endregion
423
472
  //#region src/root/types/VersionNumber.ts
424
473
  /**
@@ -46,47 +46,6 @@ const FILE_PATH_REGEX = String.raw`^(?<directory>.+)[\/\\](?<base>[^\/\\]+)$`;
46
46
  //#region src/root/constants/VERSION_NUMBER_REGEX.ts
47
47
  const VERSION_NUMBER_REGEX = "^(?:v)?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$";
48
48
 
49
- //#endregion
50
- //#region src/root/types/DataError.ts
51
- /**
52
- * Represents errors you may get that may've been caused by a specific piece of data.
53
- *
54
- * @category Types
55
- *
56
- * @template DataType - The type of the data that caused the error.
57
- */
58
- var DataError = class DataError extends Error {
59
- code;
60
- data;
61
- /**
62
- * @param data - The data that caused the error.
63
- * @param code - A standardised code (e.g. UNEXPECTED_DATA).
64
- * @param message - A human-readable error message (e.g. The data provided is invalid).
65
- * @param options - Extra options to pass to super Error constructor.
66
- */
67
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
68
- super(message, options);
69
- if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
70
- this.name = new.target.name;
71
- this.code = code;
72
- this.data = data;
73
- Object.defineProperty(this, "message", { enumerable: true });
74
- Object.setPrototypeOf(this, new.target.prototype);
75
- }
76
- /**
77
- * Checks whether the given input may have been caused by a DataError.
78
- *
79
- * @param input - The input to check.
80
- *
81
- * @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`.
82
- */
83
- static check(input) {
84
- if (input instanceof DataError) return true;
85
- const data = input;
86
- return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
87
- }
88
- };
89
-
90
49
  //#endregion
91
50
  //#region src/root/functions/arrayHelpers/fillArray.ts
92
51
  /**
@@ -389,6 +348,96 @@ const VersionType = {
389
348
  PATCH: "patch"
390
349
  };
391
350
 
351
+ //#endregion
352
+ //#region src/root/types/DataError.ts
353
+ /**
354
+ * Represents errors you may get that may've been caused by a specific piece of data.
355
+ *
356
+ * @category Types
357
+ *
358
+ * @template DataType - The type of the data that caused the error.
359
+ */
360
+ var DataError = class DataError extends Error {
361
+ code;
362
+ data;
363
+ /**
364
+ * @param data - The data that caused the error.
365
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
366
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
367
+ * @param options - Extra options to pass to super Error constructor.
368
+ */
369
+ constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
370
+ super(message, options);
371
+ if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
372
+ this.name = new.target.name;
373
+ this.code = code;
374
+ this.data = data;
375
+ Object.defineProperty(this, "message", { enumerable: true });
376
+ Object.setPrototypeOf(this, new.target.prototype);
377
+ }
378
+ static checkCaughtError(error, options) {
379
+ if (DataError.check(error)) {
380
+ 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.
381
+
382
+ Expected: ${options.expectedCode}
383
+ Received: ${error.code}
384
+ `, { cause: error });
385
+ return error;
386
+ }
387
+ throw error;
388
+ }
389
+ /**
390
+ * Checks whether the given input may have been caused by a DataError.
391
+ *
392
+ * @param input - The input to check.
393
+ *
394
+ * @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`.
395
+ */
396
+ static check(input) {
397
+ if (input instanceof DataError) return true;
398
+ const data = input;
399
+ return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
400
+ }
401
+ /**
402
+ * 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.
403
+ *
404
+ * @param errorFunction - The function expected to throw the error.
405
+ * @param options - Extra options to apply.
406
+ *
407
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
408
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
409
+ *
410
+ * @returns The `DataError` that was thrown by the `errorFunction`
411
+ */
412
+ static expectError(errorFunction, options) {
413
+ try {
414
+ errorFunction();
415
+ } catch (error) {
416
+ return DataError.checkCaughtError(error, options);
417
+ }
418
+ throw new Error("Expected a DataError to be thrown but none was thrown");
419
+ }
420
+ /**
421
+ * 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.
422
+ *
423
+ * @param errorFunction - The function expected to throw the error.
424
+ * @param options - Extra options to apply.
425
+ *
426
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
427
+ * @throws {Error} If no `DataError` was thrown by the `errorFunction`
428
+ *
429
+ * @returns The `DataError` that was thrown by the `errorFunction`
430
+ */
431
+ static async expectErrorAsync(errorFunction, options) {
432
+ try {
433
+ await errorFunction();
434
+ } catch (error) {
435
+ return DataError.checkCaughtError(error, options);
436
+ }
437
+ throw new Error("Expected a DataError to be thrown but none was thrown");
438
+ }
439
+ };
440
+
392
441
  //#endregion
393
442
  //#region src/root/types/VersionNumber.ts
394
443
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alextheman/utility",
3
- "version": "5.1.3",
3
+ "version": "5.2.0",
4
4
  "description": "Helpful utility functions.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,14 +36,14 @@
36
36
  "zod": "^4.3.6"
37
37
  },
38
38
  "devDependencies": {
39
- "@alextheman/eslint-plugin": "^5.7.1",
40
- "@types/node": "^25.2.3",
41
- "alex-c-line": "^1.26.2",
39
+ "@alextheman/eslint-plugin": "^5.8.2",
40
+ "@types/node": "^25.3.0",
41
+ "alex-c-line": "^1.28.0",
42
42
  "dotenv-cli": "^11.0.0",
43
- "eslint": "^10.0.0",
43
+ "eslint": "^10.0.1",
44
44
  "globals": "^17.3.0",
45
45
  "husky": "^9.1.7",
46
- "jsdom": "^28.0.0",
46
+ "jsdom": "^28.1.0",
47
47
  "prettier": "^3.8.1",
48
48
  "tempy": "^3.2.0",
49
49
  "tsdown": "^0.20.3",