@alextheman/utility 5.12.0 → 5.13.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.
@@ -57,34 +57,42 @@ function paralleliseArrays(firstArray, secondArray) {
57
57
  return outputArray;
58
58
  }
59
59
  //#endregion
60
- //#region src/root/types/DataError.ts
60
+ //#region src/v6/CodeError.ts
61
61
  /**
62
- * Represents errors you may get that may've been caused by a specific piece of data.
62
+ * Represents errors that can be described using a standardised error code, and a human-readable error message.
63
63
  *
64
64
  * @category Types
65
65
  *
66
- * @template DataType - The type of the data that caused the error.
66
+ * @template ErrorCode The type of the standardised error code.
67
67
  */
68
- var DataError = class DataError extends Error {
68
+ var CodeError = class CodeError extends Error {
69
69
  code;
70
- data;
71
70
  /**
72
- * @param data - The data that caused the error.
73
71
  * @param code - A standardised code (e.g. UNEXPECTED_DATA).
74
72
  * @param message - A human-readable error message (e.g. The data provided is invalid).
75
73
  * @param options - Extra options to pass to super Error constructor.
76
74
  */
77
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
75
+ constructor(code, message = "Something went wrong.", options) {
78
76
  super(message, options);
79
77
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
80
78
  this.name = new.target.name;
81
79
  this.code = code;
82
- this.data = data;
83
80
  Object.defineProperty(this, "message", { enumerable: true });
84
81
  Object.setPrototypeOf(this, new.target.prototype);
85
82
  }
83
+ /**
84
+ * Checks whether the given input may have been caused by a CodeError.
85
+ *
86
+ * @param input - The input to check.
87
+ *
88
+ * @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`.
89
+ */
90
+ static check(input) {
91
+ if (input instanceof CodeError) return true;
92
+ return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string";
93
+ }
86
94
  static checkCaughtError(error, options) {
87
- if (DataError.check(error)) {
95
+ if (this.check(error)) {
88
96
  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.
89
97
 
90
98
  Expected: ${options.expectedCode}
@@ -95,6 +103,71 @@ var DataError = class DataError extends Error {
95
103
  throw error;
96
104
  }
97
105
  /**
106
+ * 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.
107
+ *
108
+ * @param errorFunction - The function expected to throw the error.
109
+ * @param options - Extra options to apply.
110
+ *
111
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
112
+ * @throws {Error} If no `CodeError` was thrown by the `errorFunction`
113
+ *
114
+ * @returns The `CodeError` that was thrown by the `errorFunction`
115
+ */
116
+ static expectError(errorFunction, options) {
117
+ try {
118
+ errorFunction();
119
+ } catch (error) {
120
+ return this.checkCaughtError(error, options);
121
+ }
122
+ throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
123
+ }
124
+ /**
125
+ * 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.
126
+ *
127
+ * @param errorFunction - The function expected to throw the error.
128
+ * @param options - Extra options to apply.
129
+ *
130
+ * @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
131
+ * @throws {Error} If no `CodeError` was thrown by the `errorFunction`
132
+ *
133
+ * @returns The `CodeError` that was thrown by the `errorFunction`
134
+ */
135
+ static async expectErrorAsync(errorFunction, options) {
136
+ try {
137
+ await errorFunction();
138
+ } catch (error) {
139
+ return this.checkCaughtError(error, options);
140
+ }
141
+ throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
142
+ }
143
+ };
144
+ //#endregion
145
+ //#region src/v6/DataError.ts
146
+ /**
147
+ * Represents errors you may get that may've been caused by a specific piece of data.
148
+ *
149
+ * @category Types
150
+ *
151
+ * @template DataType - The type of the data that caused the error.
152
+ */
153
+ var DataError = class DataError extends CodeError {
154
+ data;
155
+ /**
156
+ * @param data - The data that caused the error.
157
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
158
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
159
+ * @param options - Extra options to pass to super Error constructor.
160
+ */
161
+ constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
162
+ super(code, message, options);
163
+ if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
164
+ this.name = new.target.name;
165
+ this.code = code;
166
+ this.data = data;
167
+ Object.defineProperty(this, "message", { enumerable: true });
168
+ Object.setPrototypeOf(this, new.target.prototype);
169
+ }
170
+ /**
98
171
  * Checks whether the given input may have been caused by a DataError.
99
172
  *
100
173
  * @param input - The input to check.
@@ -103,8 +176,7 @@ var DataError = class DataError extends Error {
103
176
  */
104
177
  static check(input) {
105
178
  if (input instanceof DataError) return true;
106
- const data = input;
107
- return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
179
+ return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
108
180
  }
109
181
  /**
110
182
  * 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.
@@ -118,12 +190,7 @@ var DataError = class DataError extends Error {
118
190
  * @returns The `DataError` that was thrown by the `errorFunction`
119
191
  */
120
192
  static expectError(errorFunction, options) {
121
- try {
122
- errorFunction();
123
- } catch (error) {
124
- return DataError.checkCaughtError(error, options);
125
- }
126
- throw new Error("Expected a DataError to be thrown but none was thrown");
193
+ return super.expectError(errorFunction, options);
127
194
  }
128
195
  /**
129
196
  * 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.
@@ -137,15 +204,57 @@ var DataError = class DataError extends Error {
137
204
  * @returns The `DataError` that was thrown by the `errorFunction`
138
205
  */
139
206
  static async expectErrorAsync(errorFunction, options) {
140
- try {
141
- await errorFunction();
142
- } catch (error) {
143
- return DataError.checkCaughtError(error, options);
144
- }
145
- throw new Error("Expected a DataError to be thrown but none was thrown");
207
+ return await super.expectErrorAsync(errorFunction, options);
146
208
  }
147
209
  };
148
210
  //#endregion
211
+ //#region src/root/functions/parsers/zod/_parseZodSchema.ts
212
+ function _parseZodSchema(parsedResult, input, onError) {
213
+ if (!parsedResult.success) {
214
+ if (onError) {
215
+ if (onError instanceof Error) throw onError;
216
+ else if (typeof onError === "function") {
217
+ const evaluatedError = onError(parsedResult.error);
218
+ if (evaluatedError instanceof Error) throw evaluatedError;
219
+ }
220
+ }
221
+ const allErrorCodes = {};
222
+ for (const issue of parsedResult.error.issues) {
223
+ const code = issue.code.toUpperCase();
224
+ allErrorCodes[code] = (allErrorCodes[code] ?? 0) + 1;
225
+ }
226
+ throw new DataError({ input }, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
227
+ return secondCount - firstCount;
228
+ }).map(([code, count], _, allErrorCodes) => {
229
+ return allErrorCodes.length === 1 && count === 1 ? code : `${code}×${count}`;
230
+ }).join(","), `\n\n${z.prettifyError(parsedResult.error)}\n`);
231
+ }
232
+ return parsedResult.data;
233
+ }
234
+ //#endregion
235
+ //#region src/root/functions/parsers/zod/parseZodSchema.ts
236
+ /**
237
+ * An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
238
+ *
239
+ * NOTE: Use `parseZodSchemaAsync` if your schema includes an asynchronous function.
240
+ *
241
+ * @category Parsers
242
+ *
243
+ * @template SchemaType - The Zod schema type.
244
+ * @template ErrorType - The type of error to throw on invalid data.
245
+ *
246
+ * @param schema - The Zod schema to use in parsing.
247
+ * @param input - The data to parse.
248
+ * @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
249
+ *
250
+ * @throws {DataErrorCode} If the given data cannot be parsed according to the schema.
251
+ *
252
+ * @returns The parsed data from the Zod schema.
253
+ */
254
+ function parseZodSchema(schema, input, onError) {
255
+ return _parseZodSchema(schema.safeParse(input), input, onError);
256
+ }
257
+ //#endregion
149
258
  //#region src/root/functions/taggedTemplate/interpolate.ts
150
259
  /**
151
260
  * Returns the result of interpolating a template string when given the strings and interpolations separately.
@@ -236,166 +345,6 @@ function normaliseIndents(first, ...args) {
236
345
  return reduceLines(interpolate(strings, ...[...args]).split("\n"), options);
237
346
  }
238
347
  //#endregion
239
- //#region src/root/functions/miscellaneous/sayHello.ts
240
- /**
241
- * Returns a string representing the lyrics to the package's theme song, Commit To You
242
- *
243
- * [Pls listen!](https://www.youtube.com/watch?v=mH-Sg-8EnxM)
244
- *
245
- * @returns The lyrics string in markdown format.
246
- */
247
- function sayHello() {
248
- return normaliseIndents`
249
- # Commit To You
250
-
251
- ### Verse 1
252
-
253
- I know you've been checking me out,
254
- Shall we take it to the next level now?
255
- 'Cause I really wanna be there all for you,
256
- All for you!
257
- Come on now, let's make a fresh start!
258
- Pin my number, then you can take me out!
259
- Can't you see I really do care about you,
260
- About you!
261
-
262
- ### Pre-chorus 1
263
- Although our calendars are imperfect, at best,
264
- I'd like to organise time with you! (with you!).
265
- Just tell me when and I'll make it clear,
266
- All clear for you,
267
- All clear for you!
268
- (One, two, three, go!)
269
-
270
- ### Chorus
271
- I wanna be of utility, I'll help you on the run!
272
- I'll be the one here in the back, while you go have some fun!
273
- Looking out for you tonight, I'll be the one you can rely on!
274
- Watch you go and watch me pass by,
275
- I'll be here!
276
- I'll commit to you!
277
-
278
- ### Verse 2
279
- Though sometimes it won't be easy,
280
- You'll be here to bring out the best in me,
281
- And I'll hold myself to high standards for you!
282
- All for you!
283
- We'll grow as a pair, you and me,
284
- We'll build up a healthy dependency,
285
- You can build with me and I'll develop with you!
286
- I'm with you!
287
-
288
- ### Pre-chorus 2
289
- I'll be with you when you're up or you're down,
290
- We'll deal with all our problems together (together!)
291
- Just tell me what you want, I'll make it clear,
292
- All clear for you,
293
- All clear for you!
294
- (One, three, one, go!)
295
-
296
- ### Chorus
297
- I wanna be of utility, I'll help you on the run!
298
- (help you on the run!)
299
- I'll be the one here in the back, while you go have some fun!
300
- (you go have some fun!)
301
- Looking out for you tonight, I'll be the one you can rely on!
302
- Watch you go and watch me pass by,
303
- I'll be here!
304
- I'll commit to you!
305
-
306
- ### Bridge
307
- Looking into our stack!
308
- I'll commit to you!
309
- We've got a lot to unpack!
310
- I'll commit to you!
311
- The environment that we're in!
312
- I'll commit to you!
313
- Delicate as a string!
314
- I'll commit to you!
315
-
316
- But I think you're my type!
317
- I'll commit to you!
318
- Oh, this feels all so right!
319
- I'll commit to you!
320
- Nothing stopping us now!
321
- I'll commit to you!
322
- Let's show them what we're about!
323
- Two, three, four, go!
324
-
325
- ### Final Chorus
326
- I wanna be of utility, I'll help you on the run!
327
- (help you on the run!)
328
- I'll be the one here in the back, while you go have some fun!
329
- (you go have some fun!)
330
- Looking out for you tonight, I'll be the one you can rely on!
331
- Watch you go and watch me pass by,
332
- I'll be here!
333
- I'll commit to you!
334
-
335
- I wanna be of utility, I'll help you on the run!
336
- (I'll commit to you!)
337
- I'll be the one here in the back, while you go have some fun!
338
- (I'll commit to you!)
339
- Looking out for you tonight, I'll be the one you can rely on!
340
- (I'll commit to you!)
341
- Watch you go and watch me pass by,
342
- (I'll commit to you!)
343
- I'll be here!
344
-
345
- ### Outro
346
- I'll commit to you!
347
- I'll commit to you!
348
- I'll commit to you!
349
- `;
350
- }
351
- //#endregion
352
- //#region src/root/functions/parsers/zod/_parseZodSchema.ts
353
- function _parseZodSchema(parsedResult, input, onError) {
354
- if (!parsedResult.success) {
355
- if (onError) {
356
- if (onError instanceof Error) throw onError;
357
- else if (typeof onError === "function") {
358
- const evaluatedError = onError(parsedResult.error);
359
- if (evaluatedError instanceof Error) throw evaluatedError;
360
- }
361
- }
362
- const allErrorCodes = {};
363
- for (const issue of parsedResult.error.issues) {
364
- const code = issue.code.toUpperCase();
365
- allErrorCodes[code] = (allErrorCodes[code] ?? 0) + 1;
366
- }
367
- throw new DataError({ input }, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
368
- return secondCount - firstCount;
369
- }).map(([code, count], _, allErrorCodes) => {
370
- return allErrorCodes.length === 1 && count === 1 ? code : `${code}×${count}`;
371
- }).join(","), `\n\n${z.prettifyError(parsedResult.error)}\n`);
372
- }
373
- return parsedResult.data;
374
- }
375
- //#endregion
376
- //#region src/root/functions/parsers/zod/parseZodSchema.ts
377
- /**
378
- * An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
379
- *
380
- * NOTE: Use `parseZodSchemaAsync` if your schema includes an asynchronous function.
381
- *
382
- * @category Parsers
383
- *
384
- * @template SchemaType - The Zod schema type.
385
- * @template ErrorType - The type of error to throw on invalid data.
386
- *
387
- * @param schema - The Zod schema to use in parsing.
388
- * @param input - The data to parse.
389
- * @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
390
- *
391
- * @throws {DataError} If the given data cannot be parsed according to the schema.
392
- *
393
- * @returns The parsed data from the Zod schema.
394
- */
395
- function parseZodSchema(schema, input, onError) {
396
- return _parseZodSchema(schema.safeParse(input), input, onError);
397
- }
398
- //#endregion
399
348
  //#region src/internal/getDependenciesFromGroup.ts
400
349
  /**
401
350
  * Get the dependencies from a given dependency group in `package.json`.
@@ -471,7 +420,117 @@ function parseJsonFromStdout(stdout) {
471
420
  }
472
421
  //#endregion
473
422
  //#region src/internal/sayHello.ts
474
- var sayHello_default = sayHello;
423
+ /**
424
+ * Returns a string representing the lyrics to the package's theme song, Commit To You
425
+ *
426
+ * [Pls listen!](https://www.youtube.com/watch?v=mH-Sg-8EnxM)
427
+ *
428
+ * @returns The lyrics string in markdown format.
429
+ */
430
+ function sayHello() {
431
+ return `
432
+ # Commit To You
433
+
434
+ ### Verse 1
435
+
436
+ I know you've been checking me out,
437
+ Shall we take it to the next level now?
438
+ 'Cause I really wanna be there all for you,
439
+ All for you!
440
+ Come on now, let's make a fresh start!
441
+ Pin my number, then you can take me out!
442
+ Can't you see I really do care about you,
443
+ About you!
444
+
445
+ ### Pre-chorus 1
446
+ Although our calendars are imperfect, at best,
447
+ I'd like to organise time with you! (with you!).
448
+ Just tell me when and I'll make it clear,
449
+ All clear for you,
450
+ All clear for you!
451
+ (One, two, three, go!)
452
+
453
+ ### Chorus
454
+ I wanna be of utility, I'll help you on the run!
455
+ I'll be the one here in the back, while you go have some fun!
456
+ Looking out for you tonight, I'll be the one you can rely on!
457
+ Watch you go and watch me pass by,
458
+ I'll be here!
459
+ I'll commit to you!
460
+
461
+ ### Verse 2
462
+ Though sometimes it won't be easy,
463
+ You'll be here to bring out the best in me,
464
+ And I'll hold myself to high standards for you!
465
+ All for you!
466
+ We'll grow as a pair, you and me,
467
+ We'll build up a healthy dependency,
468
+ You can build with me and I'll develop with you!
469
+ I'm with you!
470
+
471
+ ### Pre-chorus 2
472
+ I'll be with you when you're up or you're down,
473
+ We'll deal with all our problems together (together!)
474
+ Just tell me what you want, I'll make it clear,
475
+ All clear for you,
476
+ All clear for you!
477
+ (One, three, one, go!)
478
+
479
+ ### Chorus
480
+ I wanna be of utility, I'll help you on the run!
481
+ (help you on the run!)
482
+ I'll be the one here in the back, while you go have some fun!
483
+ (you go have some fun!)
484
+ Looking out for you tonight, I'll be the one you can rely on!
485
+ Watch you go and watch me pass by,
486
+ I'll be here!
487
+ I'll commit to you!
488
+
489
+ ### Bridge
490
+ Looking into our stack!
491
+ I'll commit to you!
492
+ We've got a lot to unpack!
493
+ I'll commit to you!
494
+ The environment that we're in!
495
+ I'll commit to you!
496
+ Delicate as a string!
497
+ I'll commit to you!
498
+
499
+ But I think you're my type!
500
+ I'll commit to you!
501
+ Oh, this feels all so right!
502
+ I'll commit to you!
503
+ Nothing stopping us now!
504
+ I'll commit to you!
505
+ Let's show them what we're about!
506
+ Two, three, four, go!
507
+
508
+ ### Final Chorus
509
+ I wanna be of utility, I'll help you on the run!
510
+ (help you on the run!)
511
+ I'll be the one here in the back, while you go have some fun!
512
+ (you go have some fun!)
513
+ Looking out for you tonight, I'll be the one you can rely on!
514
+ Watch you go and watch me pass by,
515
+ I'll be here!
516
+ I'll commit to you!
517
+
518
+ I wanna be of utility, I'll help you on the run!
519
+ (I'll commit to you!)
520
+ I'll be the one here in the back, while you go have some fun!
521
+ (I'll commit to you!)
522
+ Looking out for you tonight, I'll be the one you can rely on!
523
+ (I'll commit to you!)
524
+ Watch you go and watch me pass by,
525
+ (I'll commit to you!)
526
+ I'll be here!
527
+
528
+ ### Outro
529
+ I'll commit to you!
530
+ I'll commit to you!
531
+ I'll commit to you!
532
+ `;
533
+ }
475
534
  //#endregion
476
535
  //#region src/internal/setupPackageEndToEnd.ts
477
536
  async function setupPackageEndToEnd(temporaryPath, packageManager, moduleType, options) {
@@ -491,4 +550,4 @@ async function setupPackageEndToEnd(temporaryPath, packageManager, moduleType, o
491
550
  return runCommandInTempDirectory;
492
551
  }
493
552
  //#endregion
494
- export { DependencyGroup, ModuleType, PackageManager, getDependenciesFromGroup, getExpectedTgzName, getPackageJsonContents, getPackageJsonPath, packageJsonNotFoundError, parseJsonFromStdout, sayHello_default as sayHello, setupPackageEndToEnd };
553
+ export { DependencyGroup, ModuleType, PackageManager, getDependenciesFromGroup, getExpectedTgzName, getPackageJsonContents, getPackageJsonPath, packageJsonNotFoundError, parseJsonFromStdout, sayHello, setupPackageEndToEnd };