@alextheman/utility 5.13.1 → 5.15.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.
- package/dist/index.cjs +302 -237
- package/dist/index.d.cts +207 -160
- package/dist/index.d.ts +206 -159
- package/dist/index.js +302 -238
- package/dist/internal/index.cjs +342 -43
- package/dist/internal/index.d.cts +36 -18
- package/dist/internal/index.d.ts +36 -18
- package/dist/internal/index.js +342 -43
- package/dist/node/index.cjs +31 -2
- package/dist/node/index.js +31 -2
- package/dist/v6/index.cjs +31 -4
- package/dist/v6/index.d.cts +40 -32
- package/dist/v6/index.d.ts +40 -32
- package/dist/v6/index.js +32 -4
- package/package.json +7 -7
package/dist/internal/index.cjs
CHANGED
|
@@ -33,6 +33,10 @@ const DependencyGroup = {
|
|
|
33
33
|
DEV_DEPENDENCIES: "devDependencies"
|
|
34
34
|
};
|
|
35
35
|
//#endregion
|
|
36
|
+
//#region src/root/constants/VERSION_NUMBER_REGEX.ts
|
|
37
|
+
const VERSION_NUMBER_PATTERN = String.raw`^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
|
|
38
|
+
const VERSION_NUMBER_REGEX = RegExp(`^${VERSION_NUMBER_PATTERN}$`);
|
|
39
|
+
//#endregion
|
|
36
40
|
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
37
41
|
/**
|
|
38
42
|
* Creates a new array where each element is the result of the provided callback.
|
|
@@ -128,6 +132,21 @@ var CodeError = class CodeError extends Error {
|
|
|
128
132
|
throw error;
|
|
129
133
|
}
|
|
130
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
|
+
/**
|
|
131
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.
|
|
132
151
|
*
|
|
133
152
|
* @param errorFunction - The function expected to throw the error.
|
|
@@ -183,11 +202,10 @@ var DataError = class DataError extends CodeError {
|
|
|
183
202
|
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
184
203
|
* @param options - Extra options to pass to super Error constructor.
|
|
185
204
|
*/
|
|
186
|
-
constructor(data, code
|
|
205
|
+
constructor(data, code, message = "The data provided is invalid", options) {
|
|
187
206
|
super(code, message, options);
|
|
188
207
|
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
189
208
|
this.name = new.target.name;
|
|
190
|
-
this.code = code;
|
|
191
209
|
this.data = data;
|
|
192
210
|
Object.defineProperty(this, "message", { enumerable: true });
|
|
193
211
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -204,6 +222,21 @@ var DataError = class DataError extends CodeError {
|
|
|
204
222
|
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
205
223
|
}
|
|
206
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
|
+
/**
|
|
207
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.
|
|
208
241
|
*
|
|
209
242
|
* @param errorFunction - The function expected to throw the error.
|
|
@@ -233,53 +266,49 @@ var DataError = class DataError extends CodeError {
|
|
|
233
266
|
}
|
|
234
267
|
};
|
|
235
268
|
//#endregion
|
|
236
|
-
//#region src/root/functions/parsers/
|
|
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
|
|
269
|
+
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
261
270
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
* NOTE: Use `parseZodSchemaAsync` if your schema includes an asynchronous function.
|
|
271
|
+
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
265
272
|
*
|
|
266
273
|
* @category Parsers
|
|
267
274
|
*
|
|
268
|
-
* @
|
|
269
|
-
* @
|
|
275
|
+
* @param string - A string to convert into a number.
|
|
276
|
+
* @param radix - A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.
|
|
270
277
|
*
|
|
271
|
-
* @
|
|
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.
|
|
278
|
+
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
274
279
|
*
|
|
275
|
-
* @
|
|
276
|
-
*
|
|
277
|
-
* @returns The parsed data from the Zod schema.
|
|
280
|
+
* @returns The integer parsed from the input string.
|
|
278
281
|
*/
|
|
279
|
-
function
|
|
280
|
-
|
|
282
|
+
function parseIntStrict(string, radix) {
|
|
283
|
+
const trimmedString = string.trim();
|
|
284
|
+
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
285
|
+
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError(radix ? {
|
|
286
|
+
string,
|
|
287
|
+
radix
|
|
288
|
+
} : { string }, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
289
|
+
if (radix && radix < 10 && [...trimmedString.replace(/^[+-]/, "")].some((character) => {
|
|
290
|
+
return parseInt(character) >= radix;
|
|
291
|
+
})) throw new DataError({
|
|
292
|
+
string,
|
|
293
|
+
radix
|
|
294
|
+
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
295
|
+
const parseIntResult = parseInt(trimmedString, radix);
|
|
296
|
+
if (isNaN(parseIntResult)) throw new DataError({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
297
|
+
return parseIntResult;
|
|
281
298
|
}
|
|
282
299
|
//#endregion
|
|
300
|
+
//#region src/root/functions/parsers/parseVersionType.ts
|
|
301
|
+
/**
|
|
302
|
+
* Represents the three common software version types.
|
|
303
|
+
*
|
|
304
|
+
* @category Types
|
|
305
|
+
*/
|
|
306
|
+
const VersionType = {
|
|
307
|
+
MAJOR: "major",
|
|
308
|
+
MINOR: "minor",
|
|
309
|
+
PATCH: "patch"
|
|
310
|
+
};
|
|
311
|
+
//#endregion
|
|
283
312
|
//#region src/root/functions/taggedTemplate/interpolate.ts
|
|
284
313
|
/**
|
|
285
314
|
* Returns the result of interpolating a template string when given the strings and interpolations separately.
|
|
@@ -370,6 +399,276 @@ function normaliseIndents(first, ...args) {
|
|
|
370
399
|
return reduceLines(interpolate(strings, ...[...args]).split("\n"), options);
|
|
371
400
|
}
|
|
372
401
|
//#endregion
|
|
402
|
+
//#region src/root/types/VersionNumber.ts
|
|
403
|
+
/**
|
|
404
|
+
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
405
|
+
*
|
|
406
|
+
* @category Types
|
|
407
|
+
*/
|
|
408
|
+
var VersionNumber = class VersionNumber {
|
|
409
|
+
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
410
|
+
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
411
|
+
major = 0;
|
|
412
|
+
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
413
|
+
minor = 0;
|
|
414
|
+
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
415
|
+
patch = 0;
|
|
416
|
+
/**
|
|
417
|
+
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
418
|
+
*/
|
|
419
|
+
constructor(input) {
|
|
420
|
+
if (input instanceof VersionNumber) {
|
|
421
|
+
this.major = input.major;
|
|
422
|
+
this.minor = input.minor;
|
|
423
|
+
this.patch = input.patch;
|
|
424
|
+
} else if (typeof input === "string") {
|
|
425
|
+
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
426
|
+
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
427
|
+
return parseIntStrict(number);
|
|
428
|
+
});
|
|
429
|
+
this.major = major;
|
|
430
|
+
this.minor = minor;
|
|
431
|
+
this.patch = patch;
|
|
432
|
+
} else if (Array.isArray(input)) {
|
|
433
|
+
if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
434
|
+
const [major, minor, patch] = input.map((number) => {
|
|
435
|
+
const parsedInteger = parseIntStrict(number?.toString());
|
|
436
|
+
if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
437
|
+
return parsedInteger;
|
|
438
|
+
});
|
|
439
|
+
this.major = major;
|
|
440
|
+
this.minor = minor;
|
|
441
|
+
this.patch = patch;
|
|
442
|
+
} else throw new DataError({ input }, "INVALID_INPUT", normaliseIndents`
|
|
443
|
+
The provided input can not be parsed into a valid version number.
|
|
444
|
+
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
445
|
+
`);
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Gets the current version type of the current instance of `VersionNumber`.
|
|
449
|
+
*
|
|
450
|
+
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
451
|
+
*/
|
|
452
|
+
get type() {
|
|
453
|
+
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
454
|
+
if (this.patch === 0) return VersionType.MINOR;
|
|
455
|
+
return VersionType.PATCH;
|
|
456
|
+
}
|
|
457
|
+
static formatString(input, options) {
|
|
458
|
+
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
459
|
+
return input.startsWith("v") ? input : `v${input}`;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
463
|
+
*
|
|
464
|
+
* @param firstVersion - The first version number to compare.
|
|
465
|
+
* @param secondVersion - The second version number to compare.
|
|
466
|
+
*
|
|
467
|
+
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
468
|
+
*/
|
|
469
|
+
static isEqual(firstVersion, secondVersion) {
|
|
470
|
+
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Get a formatted string representation of the current version number
|
|
474
|
+
*
|
|
475
|
+
* @param options - Options to apply to the string formatting.
|
|
476
|
+
*
|
|
477
|
+
* @returns A formatted string representation of the current version number with the options applied.
|
|
478
|
+
*/
|
|
479
|
+
format(options) {
|
|
480
|
+
let baseOutput = `${this.major}`;
|
|
481
|
+
if (!options?.omitMinor) {
|
|
482
|
+
baseOutput += `.${this.minor}`;
|
|
483
|
+
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
484
|
+
}
|
|
485
|
+
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
489
|
+
*
|
|
490
|
+
* @param incrementType - The type of increment. Can be one of the following:
|
|
491
|
+
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
492
|
+
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
493
|
+
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
494
|
+
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
495
|
+
*
|
|
496
|
+
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
497
|
+
*/
|
|
498
|
+
increment(incrementType, incrementAmount = 1) {
|
|
499
|
+
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
500
|
+
const calculatedRawVersion = {
|
|
501
|
+
major: [
|
|
502
|
+
this.major + incrementBy,
|
|
503
|
+
0,
|
|
504
|
+
0
|
|
505
|
+
],
|
|
506
|
+
minor: [
|
|
507
|
+
this.major,
|
|
508
|
+
this.minor + incrementBy,
|
|
509
|
+
0
|
|
510
|
+
],
|
|
511
|
+
patch: [
|
|
512
|
+
this.major,
|
|
513
|
+
this.minor,
|
|
514
|
+
this.patch + incrementBy
|
|
515
|
+
]
|
|
516
|
+
}[incrementType];
|
|
517
|
+
try {
|
|
518
|
+
return new VersionNumber(calculatedRawVersion);
|
|
519
|
+
} catch (error) {
|
|
520
|
+
if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
|
|
521
|
+
currentVersion: this.toString(),
|
|
522
|
+
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
523
|
+
incrementAmount
|
|
524
|
+
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
525
|
+
else throw error;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
530
|
+
*
|
|
531
|
+
* @param hint - Not used as of now, but generally used to help with numeric coercion, I think (which we most likely do not need for version numbers).
|
|
532
|
+
*
|
|
533
|
+
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
534
|
+
*/
|
|
535
|
+
[Symbol.toPrimitive](hint) {
|
|
536
|
+
if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
537
|
+
return this.toString();
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
541
|
+
*
|
|
542
|
+
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
543
|
+
*/
|
|
544
|
+
toJSON() {
|
|
545
|
+
return this.toString();
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Get a string representation of the current version number.
|
|
549
|
+
*
|
|
550
|
+
* @returns A stringified representation of the current version number with the prefix.
|
|
551
|
+
*/
|
|
552
|
+
toString() {
|
|
553
|
+
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
554
|
+
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
const zodVersionNumber = zod.default.union([
|
|
558
|
+
zod.default.string(),
|
|
559
|
+
zod.default.tuple([
|
|
560
|
+
zod.default.number(),
|
|
561
|
+
zod.default.number(),
|
|
562
|
+
zod.default.number()
|
|
563
|
+
]),
|
|
564
|
+
zod.default.instanceof(VersionNumber)
|
|
565
|
+
]).transform((rawVersionNumber) => {
|
|
566
|
+
return new VersionNumber(rawVersionNumber);
|
|
567
|
+
});
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region src/root/zod/_parseZodSchema.ts
|
|
570
|
+
function _parseZodSchema(parsedResult, input, onError) {
|
|
571
|
+
if (!parsedResult.success) {
|
|
572
|
+
if (onError) {
|
|
573
|
+
if (onError instanceof Error) throw onError;
|
|
574
|
+
else if (typeof onError === "function") {
|
|
575
|
+
const evaluatedError = onError(parsedResult.error);
|
|
576
|
+
if (evaluatedError instanceof Error) throw evaluatedError;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
const allErrorCodes = {};
|
|
580
|
+
for (const issue of parsedResult.error.issues) {
|
|
581
|
+
const code = issue.code.toUpperCase();
|
|
582
|
+
allErrorCodes[code] = (allErrorCodes[code] ?? 0) + 1;
|
|
583
|
+
}
|
|
584
|
+
throw new DataError({ input }, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
|
|
585
|
+
return secondCount - firstCount;
|
|
586
|
+
}).map(([code, count], _, allErrorCodes) => {
|
|
587
|
+
return allErrorCodes.length === 1 && count === 1 ? code : `${code}×${count}`;
|
|
588
|
+
}).join(","), `\n\n${zod.default.prettifyError(parsedResult.error)}\n`);
|
|
589
|
+
}
|
|
590
|
+
return parsedResult.data;
|
|
591
|
+
}
|
|
592
|
+
//#endregion
|
|
593
|
+
//#region src/root/zod/parseZodSchema.ts
|
|
594
|
+
/**
|
|
595
|
+
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
596
|
+
*
|
|
597
|
+
* NOTE: Use `parseZodSchemaAsync` if your schema includes an asynchronous function.
|
|
598
|
+
*
|
|
599
|
+
* @category Parsers
|
|
600
|
+
*
|
|
601
|
+
* @deprecated Please use `az.with(schema).parse(input)` instead.
|
|
602
|
+
*
|
|
603
|
+
* @template SchemaType - The Zod schema type.
|
|
604
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
605
|
+
*
|
|
606
|
+
* @param schema - The Zod schema to use in parsing.
|
|
607
|
+
* @param input - The data to parse.
|
|
608
|
+
* @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.
|
|
609
|
+
*
|
|
610
|
+
* @throws {DataErrorCode} If the given data cannot be parsed according to the schema.
|
|
611
|
+
*
|
|
612
|
+
* @returns The parsed data from the Zod schema.
|
|
613
|
+
*/
|
|
614
|
+
function parseZodSchema(schema, input, onError) {
|
|
615
|
+
return _parseZodSchema(schema.safeParse(input), input, onError);
|
|
616
|
+
}
|
|
617
|
+
//#endregion
|
|
618
|
+
//#region src/root/zod/parseZodSchemaAsync.ts
|
|
619
|
+
/**
|
|
620
|
+
* An alternative function to zodSchema.parseAsync() that can be used to strictly parse asynchronous Zod schemas.
|
|
621
|
+
*
|
|
622
|
+
* @category Parsers
|
|
623
|
+
*
|
|
624
|
+
* @deprecated Please use `az.with(schema).parseAsync(input)` instead.
|
|
625
|
+
*
|
|
626
|
+
* @template SchemaType - The Zod schema type.
|
|
627
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
628
|
+
*
|
|
629
|
+
* @param schema - The Zod schema to use in parsing.
|
|
630
|
+
* @param input - The data to parse.
|
|
631
|
+
* @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.
|
|
632
|
+
*
|
|
633
|
+
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
634
|
+
*
|
|
635
|
+
* @returns The parsed data from the Zod schema.
|
|
636
|
+
*/
|
|
637
|
+
async function parseZodSchemaAsync(schema, input, onError) {
|
|
638
|
+
return _parseZodSchema(await schema.safeParseAsync(input), input, onError);
|
|
639
|
+
}
|
|
640
|
+
//#endregion
|
|
641
|
+
//#region src/root/zod/zodFieldWrapper.ts
|
|
642
|
+
function zodFieldWrapper(schema) {
|
|
643
|
+
return zod.default.string().trim().transform((value) => {
|
|
644
|
+
return value === "" ? null : value;
|
|
645
|
+
}).pipe(schema);
|
|
646
|
+
}
|
|
647
|
+
//#endregion
|
|
648
|
+
//#region src/root/zod/az.ts
|
|
649
|
+
const az = {
|
|
650
|
+
field: zodFieldWrapper,
|
|
651
|
+
fieldNumber: () => {
|
|
652
|
+
return zod.default.coerce.number();
|
|
653
|
+
},
|
|
654
|
+
versionNumber: () => {
|
|
655
|
+
return zodVersionNumber;
|
|
656
|
+
},
|
|
657
|
+
fieldDate: () => {
|
|
658
|
+
return zod.default.coerce.date();
|
|
659
|
+
},
|
|
660
|
+
with: (schema) => {
|
|
661
|
+
return {
|
|
662
|
+
parse: (input, error) => {
|
|
663
|
+
return parseZodSchema(schema, input, error);
|
|
664
|
+
},
|
|
665
|
+
parseAsync: async (input, error) => {
|
|
666
|
+
return await parseZodSchemaAsync(schema, input, error);
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
//#endregion
|
|
373
672
|
//#region src/internal/getDependenciesFromGroup.ts
|
|
374
673
|
/**
|
|
375
674
|
* Get the dependencies from a given dependency group in `package.json`.
|
|
@@ -383,8 +682,8 @@ function normaliseIndents(first, ...args) {
|
|
|
383
682
|
*/
|
|
384
683
|
function getDependenciesFromGroup(packageInfo, dependencyGroup) {
|
|
385
684
|
return {
|
|
386
|
-
dependencies:
|
|
387
|
-
devDependencies:
|
|
685
|
+
dependencies: az.with(zod.default.record(zod.default.string(), zod.default.string())).parse(packageInfo.dependencies ?? {}),
|
|
686
|
+
devDependencies: az.with(zod.default.record(zod.default.string(), zod.default.string())).parse(packageInfo.devDependencies ?? {})
|
|
388
687
|
}[dependencyGroup];
|
|
389
688
|
}
|
|
390
689
|
//#endregion
|
|
@@ -392,7 +691,7 @@ function getDependenciesFromGroup(packageInfo, dependencyGroup) {
|
|
|
392
691
|
async function getExpectedTgzName(packagePath, packageManager) {
|
|
393
692
|
const { stdout: rawPackedTgzData } = await (0, execa.execa)({ cwd: packagePath })`${packageManager} pack --json --dry-run`;
|
|
394
693
|
const packedTgzData = parseJsonFromStdout(rawPackedTgzData);
|
|
395
|
-
const parsedPackedTgzData =
|
|
694
|
+
const parsedPackedTgzData = az.with(packageManager === "pnpm" ? zod.default.object({ filename: zod.default.string() }) : zod.default.array(zod.default.object({ filename: zod.default.string() }))).parse(packedTgzData, new DataError(packedTgzData, "AMBIGUOUS_EXPECTED_FILE_NAME", "Could not figure out the expected filename."));
|
|
396
695
|
const [normalisedTgzMetadata] = Array.isArray(parsedPackedTgzData) ? parsedPackedTgzData : [parsedPackedTgzData];
|
|
397
696
|
const { filename: expectedTgzFileName } = normalisedTgzMetadata;
|
|
398
697
|
return expectedTgzFileName;
|
|
@@ -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
|
|
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<
|
|
54
|
-
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions
|
|
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
|
|
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
|
|
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 =
|
|
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
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|