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