@alextheman/utility 5.13.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.
- package/dist/index.cjs +122 -30
- package/dist/index.d.cts +75 -75
- package/dist/index.d.ts +75 -75
- package/dist/index.js +122 -31
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -180,7 +180,7 @@ var CodeError = class CodeError extends Error {
|
|
|
180
180
|
*
|
|
181
181
|
* @template DataType - The type of the data that caused the error.
|
|
182
182
|
*/
|
|
183
|
-
var DataError = class DataError extends CodeError {
|
|
183
|
+
var DataError$1 = class DataError$1 extends CodeError {
|
|
184
184
|
data;
|
|
185
185
|
/**
|
|
186
186
|
* @param data - The data that caused the error.
|
|
@@ -205,7 +205,7 @@ var DataError = class DataError extends CodeError {
|
|
|
205
205
|
* @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`.
|
|
206
206
|
*/
|
|
207
207
|
static check(input) {
|
|
208
|
-
if (input instanceof DataError) return true;
|
|
208
|
+
if (input instanceof DataError$1) return true;
|
|
209
209
|
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
210
210
|
}
|
|
211
211
|
/**
|
|
@@ -254,18 +254,18 @@ var DataError = class DataError extends CodeError {
|
|
|
254
254
|
function parseIntStrict(string, radix) {
|
|
255
255
|
const trimmedString = string.trim();
|
|
256
256
|
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
257
|
-
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError(radix ? {
|
|
257
|
+
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError$1(radix ? {
|
|
258
258
|
string,
|
|
259
259
|
radix
|
|
260
260
|
} : { string }, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
261
261
|
if (radix && radix < 10 && [...trimmedString.replace(/^[+-]/, "")].some((character) => {
|
|
262
262
|
return parseInt(character) >= radix;
|
|
263
|
-
})) throw new DataError({
|
|
263
|
+
})) throw new DataError$1({
|
|
264
264
|
string,
|
|
265
265
|
radix
|
|
266
266
|
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
267
267
|
const parseIntResult = parseInt(trimmedString, radix);
|
|
268
|
-
if (isNaN(parseIntResult)) throw new DataError({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
268
|
+
if (isNaN(parseIntResult)) throw new DataError$1({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
269
269
|
return parseIntResult;
|
|
270
270
|
}
|
|
271
271
|
//#endregion
|
|
@@ -328,16 +328,16 @@ function randomiseArray(array) {
|
|
|
328
328
|
*/
|
|
329
329
|
function range(start, stop, step = 1) {
|
|
330
330
|
const numbers = [];
|
|
331
|
-
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
331
|
+
if (step === 0) throw new DataError$1({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
332
332
|
else if (step > 0) {
|
|
333
|
-
if (start > stop) throw new DataError({
|
|
333
|
+
if (start > stop) throw new DataError$1({
|
|
334
334
|
start,
|
|
335
335
|
stop,
|
|
336
336
|
step
|
|
337
337
|
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
338
338
|
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
339
339
|
} else if (step < 0) {
|
|
340
|
-
if (start < stop) throw new DataError({
|
|
340
|
+
if (start < stop) throw new DataError$1({
|
|
341
341
|
start,
|
|
342
342
|
stop,
|
|
343
343
|
step
|
|
@@ -537,7 +537,7 @@ function convertFileToBase64(file) {
|
|
|
537
537
|
reader.readAsDataURL(file);
|
|
538
538
|
reader.onload = () => {
|
|
539
539
|
if (reader.result === null) {
|
|
540
|
-
reject(new DataError({ result: reader.result }, "FILE_CONVERSION_ERROR", "Could not convert the given file."));
|
|
540
|
+
reject(new DataError$1({ result: reader.result }, "FILE_CONVERSION_ERROR", "Could not convert the given file."));
|
|
541
541
|
return;
|
|
542
542
|
}
|
|
543
543
|
resolve(reader.result);
|
|
@@ -604,10 +604,10 @@ function createFormData(data, options = {
|
|
|
604
604
|
if (Array.isArray(value)) {
|
|
605
605
|
if (value.some((item) => {
|
|
606
606
|
return item instanceof Blob;
|
|
607
|
-
}) && (options.arrayResolution === "stringify" || typeof options.arrayResolution === "object" && options.arrayResolution[key] === "stringify")) throw new DataError({ value }, "CANNOT_STRINGIFY_BLOB", "Files/blobs cannot be stringified. Please change the resolution option for this item to \"multiple\" instead.");
|
|
607
|
+
}) && (options.arrayResolution === "stringify" || typeof options.arrayResolution === "object" && options.arrayResolution[key] === "stringify")) throw new DataError$1({ value }, "CANNOT_STRINGIFY_BLOB", "Files/blobs cannot be stringified. Please change the resolution option for this item to \"multiple\" instead.");
|
|
608
608
|
if (options.arrayResolution === "multiple" || typeof options.arrayResolution === "object" && options.arrayResolution[key] === "multiple") {
|
|
609
609
|
for (const item of value) {
|
|
610
|
-
if (!isPrimitive(item) && !(item instanceof Blob)) throw new DataError({ item }, "NON_PRIMITIVE_ARRAY_ITEMS_FOUND", "Cannot directly add non-primitive data to FormData. Please change the resolution option for this item to \"stringify\" instead.");
|
|
610
|
+
if (!isPrimitive(item) && !(item instanceof Blob)) throw new DataError$1({ item }, "NON_PRIMITIVE_ARRAY_ITEMS_FOUND", "Cannot directly add non-primitive data to FormData. Please change the resolution option for this item to \"stringify\" instead.");
|
|
611
611
|
if (item instanceof Blob) formData.append(String(key), item);
|
|
612
612
|
else formData.append(String(key), String(item));
|
|
613
613
|
}
|
|
@@ -763,8 +763,8 @@ function stringifyDotenv(contents, options) {
|
|
|
763
763
|
const { quoteStyle = "double" } = options ?? {};
|
|
764
764
|
let result = "";
|
|
765
765
|
for (const key in contentsCopy) {
|
|
766
|
-
if (/[ \t\r\n]/.test(key)) throw new DataError({ [key]: contentsCopy[key] }, "INVALID_KEY", "Environment variables are not allowed to have whitespace.");
|
|
767
|
-
if (quoteStyle === "none") if (/[ \t\r\n]/.test(contentsCopy[key]) || contentsCopy[key].includes("#") || contentsCopy[key].includes("=") || contentsCopy[key].includes("\\")) throw new DataError({ [key]: contentsCopy[key] }, "INCOMPATIBLE_QUOTE_STYLE", "Cannot use `{ quoteStyle: \"none\" }` when value has whitespace, #, =, or \\");
|
|
766
|
+
if (/[ \t\r\n]/.test(key)) throw new DataError$1({ [key]: contentsCopy[key] }, "INVALID_KEY", "Environment variables are not allowed to have whitespace.");
|
|
767
|
+
if (quoteStyle === "none") if (/[ \t\r\n]/.test(contentsCopy[key]) || contentsCopy[key].includes("#") || contentsCopy[key].includes("=") || contentsCopy[key].includes("\\")) throw new DataError$1({ [key]: contentsCopy[key] }, "INCOMPATIBLE_QUOTE_STYLE", "Cannot use `{ quoteStyle: \"none\" }` when value has whitespace, #, =, or \\");
|
|
768
768
|
else {
|
|
769
769
|
result += `${key}=${contentsCopy[key]}\n`;
|
|
770
770
|
continue;
|
|
@@ -889,7 +889,7 @@ function removeUndefinedFromObject(object) {
|
|
|
889
889
|
*/
|
|
890
890
|
function parseBoolean(inputString) {
|
|
891
891
|
const normalisedString = inputString.toLowerCase();
|
|
892
|
-
if (!["true", "false"].includes(normalisedString)) throw new DataError({ inputString }, "INVALID_BOOLEAN_STRING", "The provided boolean string must be one of `true | false`");
|
|
892
|
+
if (!["true", "false"].includes(normalisedString)) throw new DataError$1({ inputString }, "INVALID_BOOLEAN_STRING", "The provided boolean string must be one of `true | false`");
|
|
893
893
|
return normalisedString === "true";
|
|
894
894
|
}
|
|
895
895
|
//#endregion
|
|
@@ -908,7 +908,7 @@ function _parseZodSchema(parsedResult, input, onError) {
|
|
|
908
908
|
const code = issue.code.toUpperCase();
|
|
909
909
|
allErrorCodes[code] = (allErrorCodes[code] ?? 0) + 1;
|
|
910
910
|
}
|
|
911
|
-
throw new DataError({ input }, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
|
|
911
|
+
throw new DataError$1({ input }, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
|
|
912
912
|
return secondCount - firstCount;
|
|
913
913
|
}).map(([code, count], _, allErrorCodes) => {
|
|
914
914
|
return allErrorCodes.length === 1 && count === 1 ? code : `${code}×${count}`;
|
|
@@ -963,7 +963,7 @@ const Env = {
|
|
|
963
963
|
* @returns The specified environment if allowed.
|
|
964
964
|
*/
|
|
965
965
|
function parseEnv(input) {
|
|
966
|
-
return parseZodSchema(zod.z.enum(Env), input, new DataError({ input }, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
966
|
+
return parseZodSchema(zod.z.enum(Env), input, new DataError$1({ input }, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
967
967
|
}
|
|
968
968
|
//#endregion
|
|
969
969
|
//#region src/root/functions/parsers/parseFormData.ts
|
|
@@ -1001,8 +1001,8 @@ function parseFormData(formData, dataParser) {
|
|
|
1001
1001
|
* @returns The UUID again if successful.
|
|
1002
1002
|
*/
|
|
1003
1003
|
function parseUUID(input) {
|
|
1004
|
-
if (!(typeof input === "string")) throw new DataError({ input }, "INVALID_TYPE", "Invalid type - expected string.");
|
|
1005
|
-
if (!UUID_REGEX.test(input)) throw new DataError({ input }, "INVALID_UUID", "The provided input does not match the expected shape for a UUID.");
|
|
1004
|
+
if (!(typeof input === "string")) throw new DataError$1({ input }, "INVALID_TYPE", "Invalid type - expected string.");
|
|
1005
|
+
if (!UUID_REGEX.test(input)) throw new DataError$1({ input }, "INVALID_UUID", "The provided input does not match the expected shape for a UUID.");
|
|
1006
1006
|
return input;
|
|
1007
1007
|
}
|
|
1008
1008
|
//#endregion
|
|
@@ -1029,7 +1029,7 @@ const VersionType = {
|
|
|
1029
1029
|
* @returns The given version type if allowed.
|
|
1030
1030
|
*/
|
|
1031
1031
|
function parseVersionType(input) {
|
|
1032
|
-
return parseZodSchema(zod.default.enum(VersionType), input, new DataError({ input }, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
1032
|
+
return parseZodSchema(zod.default.enum(VersionType), input, new DataError$1({ input }, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
1033
1033
|
}
|
|
1034
1034
|
//#endregion
|
|
1035
1035
|
//#region src/root/functions/parsers/zod/parseZodSchemaAsync.ts
|
|
@@ -1116,7 +1116,7 @@ function deepFreeze(object) {
|
|
|
1116
1116
|
* @returns A string with the semicolon appended.
|
|
1117
1117
|
*/
|
|
1118
1118
|
function appendSemicolon(stringToAppendTo) {
|
|
1119
|
-
if (stringToAppendTo.includes("\n")) throw new DataError({ stringToAppendTo }, "MULTIPLE_LINE_ERROR", "Cannot append semicolon to multi-line string.");
|
|
1119
|
+
if (stringToAppendTo.includes("\n")) throw new DataError$1({ stringToAppendTo }, "MULTIPLE_LINE_ERROR", "Cannot append semicolon to multi-line string.");
|
|
1120
1120
|
const stringWithNoTrailingWhitespace = stringToAppendTo.trimEnd();
|
|
1121
1121
|
if (stringWithNoTrailingWhitespace === "") return "";
|
|
1122
1122
|
return stringWithNoTrailingWhitespace[stringWithNoTrailingWhitespace.length - 1] === ";" ? stringWithNoTrailingWhitespace : `${stringWithNoTrailingWhitespace};`;
|
|
@@ -1184,9 +1184,9 @@ function escapeRegexPattern(regexPattern) {
|
|
|
1184
1184
|
* @returns The string converted to camelCase.
|
|
1185
1185
|
*/
|
|
1186
1186
|
function kebabToCamel(input, options) {
|
|
1187
|
-
if (input !== input.toLowerCase()) throw new DataError({ input }, "UPPERCASE_INPUT", "Kebab-case must be purely lowercase.");
|
|
1188
|
-
if (input.startsWith("-") || input.endsWith("-")) throw new DataError({ input }, "TRAILING_DASHES", "Dashes at the start and/or end are not allowed.");
|
|
1189
|
-
if (input.includes("--")) throw new DataError({ input }, "CONSECUTIVE_DASHES", "Consecutive dashes are not allowed.");
|
|
1187
|
+
if (input !== input.toLowerCase()) throw new DataError$1({ input }, "UPPERCASE_INPUT", "Kebab-case must be purely lowercase.");
|
|
1188
|
+
if (input.startsWith("-") || input.endsWith("-")) throw new DataError$1({ input }, "TRAILING_DASHES", "Dashes at the start and/or end are not allowed.");
|
|
1189
|
+
if (input.includes("--")) throw new DataError$1({ input }, "CONSECUTIVE_DASHES", "Consecutive dashes are not allowed.");
|
|
1190
1190
|
let outputString = "";
|
|
1191
1191
|
let skip = false;
|
|
1192
1192
|
for (const stringIndex in [...input]) {
|
|
@@ -1299,7 +1299,7 @@ function createTemplateStringsArray(strings) {
|
|
|
1299
1299
|
* ```
|
|
1300
1300
|
*/
|
|
1301
1301
|
function getStringsAndInterpolations(strings, ...interpolations) {
|
|
1302
|
-
if (strings.length !== interpolations.length + 1) throw new DataError({
|
|
1302
|
+
if (strings.length !== interpolations.length + 1) throw new DataError$1({
|
|
1303
1303
|
stringsLength: strings.length,
|
|
1304
1304
|
interpolationsLength: interpolations.length,
|
|
1305
1305
|
strings,
|
|
@@ -1465,6 +1465,97 @@ function normaliseIndents(first, ...args) {
|
|
|
1465
1465
|
*/
|
|
1466
1466
|
const normalizeIndents = normaliseIndents;
|
|
1467
1467
|
//#endregion
|
|
1468
|
+
//#region src/root/deprecated/DataError.ts
|
|
1469
|
+
/**
|
|
1470
|
+
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
1471
|
+
*
|
|
1472
|
+
* @category Types
|
|
1473
|
+
*
|
|
1474
|
+
* @deprecated Please use `DataError` from `@alextheman/utility/v6` instead.
|
|
1475
|
+
*
|
|
1476
|
+
* @template DataType - The type of the data that caused the error.
|
|
1477
|
+
*/
|
|
1478
|
+
var DataError = class DataError extends Error {
|
|
1479
|
+
code;
|
|
1480
|
+
data;
|
|
1481
|
+
/**
|
|
1482
|
+
* @param data - The data that caused the error.
|
|
1483
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
1484
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
1485
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
1486
|
+
*/
|
|
1487
|
+
constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
|
|
1488
|
+
super(message, options);
|
|
1489
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
1490
|
+
this.name = new.target.name;
|
|
1491
|
+
this.code = code;
|
|
1492
|
+
this.data = data;
|
|
1493
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
1494
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
1495
|
+
}
|
|
1496
|
+
static checkCaughtError(error, options) {
|
|
1497
|
+
if (DataError.check(error)) {
|
|
1498
|
+
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.
|
|
1499
|
+
|
|
1500
|
+
Expected: ${options.expectedCode}
|
|
1501
|
+
Received: ${error.code}
|
|
1502
|
+
`, { cause: error });
|
|
1503
|
+
return error;
|
|
1504
|
+
}
|
|
1505
|
+
throw error;
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
1509
|
+
*
|
|
1510
|
+
* @param input - The input to check.
|
|
1511
|
+
*
|
|
1512
|
+
* @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`.
|
|
1513
|
+
*/
|
|
1514
|
+
static check(input) {
|
|
1515
|
+
if (input instanceof DataError) return true;
|
|
1516
|
+
const data = input;
|
|
1517
|
+
return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* 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.
|
|
1521
|
+
*
|
|
1522
|
+
* @param errorFunction - The function expected to throw the error.
|
|
1523
|
+
* @param options - Extra options to apply.
|
|
1524
|
+
*
|
|
1525
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1526
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1527
|
+
*
|
|
1528
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1529
|
+
*/
|
|
1530
|
+
static expectError(errorFunction, options) {
|
|
1531
|
+
try {
|
|
1532
|
+
errorFunction();
|
|
1533
|
+
} catch (error) {
|
|
1534
|
+
return DataError.checkCaughtError(error, options);
|
|
1535
|
+
}
|
|
1536
|
+
throw new Error("Expected a DataError to be thrown but none was thrown");
|
|
1537
|
+
}
|
|
1538
|
+
/**
|
|
1539
|
+
* 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.
|
|
1540
|
+
*
|
|
1541
|
+
* @param errorFunction - The function expected to throw the error.
|
|
1542
|
+
* @param options - Extra options to apply.
|
|
1543
|
+
*
|
|
1544
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1545
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1546
|
+
*
|
|
1547
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1548
|
+
*/
|
|
1549
|
+
static async expectErrorAsync(errorFunction, options) {
|
|
1550
|
+
try {
|
|
1551
|
+
await errorFunction();
|
|
1552
|
+
} catch (error) {
|
|
1553
|
+
return DataError.checkCaughtError(error, options);
|
|
1554
|
+
}
|
|
1555
|
+
throw new Error("Expected a DataError to be thrown but none was thrown");
|
|
1556
|
+
}
|
|
1557
|
+
};
|
|
1558
|
+
//#endregion
|
|
1468
1559
|
//#region src/root/types/APIError.ts
|
|
1469
1560
|
const httpErrorCodeLookup = {
|
|
1470
1561
|
400: "BAD_REQUEST",
|
|
@@ -1531,7 +1622,7 @@ var VersionNumber = class VersionNumber {
|
|
|
1531
1622
|
this.minor = input.minor;
|
|
1532
1623
|
this.patch = input.patch;
|
|
1533
1624
|
} else if (typeof input === "string") {
|
|
1534
|
-
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.`);
|
|
1625
|
+
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError$1({ 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.`);
|
|
1535
1626
|
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
1536
1627
|
return parseIntStrict(number);
|
|
1537
1628
|
});
|
|
@@ -1539,16 +1630,16 @@ var VersionNumber = class VersionNumber {
|
|
|
1539
1630
|
this.minor = minor;
|
|
1540
1631
|
this.patch = patch;
|
|
1541
1632
|
} else if (Array.isArray(input)) {
|
|
1542
|
-
if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1633
|
+
if (input.length !== 3) throw new DataError$1({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1543
1634
|
const [major, minor, patch] = input.map((number) => {
|
|
1544
1635
|
const parsedInteger = parseIntStrict(number?.toString());
|
|
1545
|
-
if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1636
|
+
if (parsedInteger < 0) throw new DataError$1({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1546
1637
|
return parsedInteger;
|
|
1547
1638
|
});
|
|
1548
1639
|
this.major = major;
|
|
1549
1640
|
this.minor = minor;
|
|
1550
1641
|
this.patch = patch;
|
|
1551
|
-
} else throw new DataError({ input }, "INVALID_INPUT", normaliseIndents`
|
|
1642
|
+
} else throw new DataError$1({ input }, "INVALID_INPUT", normaliseIndents`
|
|
1552
1643
|
The provided input can not be parsed into a valid version number.
|
|
1553
1644
|
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
1554
1645
|
`);
|
|
@@ -1626,7 +1717,7 @@ var VersionNumber = class VersionNumber {
|
|
|
1626
1717
|
try {
|
|
1627
1718
|
return new VersionNumber(calculatedRawVersion);
|
|
1628
1719
|
} catch (error) {
|
|
1629
|
-
if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
|
|
1720
|
+
if (DataError$1.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError$1({
|
|
1630
1721
|
currentVersion: this.toString(),
|
|
1631
1722
|
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
1632
1723
|
incrementAmount
|
|
@@ -1642,7 +1733,7 @@ var VersionNumber = class VersionNumber {
|
|
|
1642
1733
|
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1643
1734
|
*/
|
|
1644
1735
|
[Symbol.toPrimitive](hint) {
|
|
1645
|
-
if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1736
|
+
if (hint === "number") throw new DataError$1({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1646
1737
|
return this.toString();
|
|
1647
1738
|
}
|
|
1648
1739
|
/**
|
|
@@ -1676,6 +1767,7 @@ const zodVersionNumber = zod.default.union([
|
|
|
1676
1767
|
});
|
|
1677
1768
|
//#endregion
|
|
1678
1769
|
exports.APIError = APIError;
|
|
1770
|
+
exports.DataError = DataError;
|
|
1679
1771
|
exports.Env = Env;
|
|
1680
1772
|
exports.FILE_PATH_PATTERN = FILE_PATH_PATTERN;
|
|
1681
1773
|
exports.FILE_PATH_REGEX = FILE_PATH_REGEX;
|
package/dist/index.d.cts
CHANGED
|
@@ -16,6 +16,74 @@ declare const UUID_REGEX: RegExp;
|
|
|
16
16
|
declare const VERSION_NUMBER_PATTERN: string;
|
|
17
17
|
declare const VERSION_NUMBER_REGEX: RegExp;
|
|
18
18
|
//#endregion
|
|
19
|
+
//#region src/root/deprecated/DataError.d.ts
|
|
20
|
+
interface ExpectErrorOptions$2 {
|
|
21
|
+
expectedCode?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
25
|
+
*
|
|
26
|
+
* @category Types
|
|
27
|
+
*
|
|
28
|
+
* @deprecated Please use `DataError` from `@alextheman/utility/v6` instead.
|
|
29
|
+
*
|
|
30
|
+
* @template DataType - The type of the data that caused the error.
|
|
31
|
+
*/
|
|
32
|
+
declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Error {
|
|
33
|
+
code: string;
|
|
34
|
+
data: DataType;
|
|
35
|
+
/**
|
|
36
|
+
* @param data - The data that caused the error.
|
|
37
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
38
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
39
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
40
|
+
*/
|
|
41
|
+
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
42
|
+
private static checkCaughtError;
|
|
43
|
+
/**
|
|
44
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
45
|
+
*
|
|
46
|
+
* @param input - The input to check.
|
|
47
|
+
*
|
|
48
|
+
* @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`.
|
|
49
|
+
*/
|
|
50
|
+
static check<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
51
|
+
/**
|
|
52
|
+
* 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.
|
|
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 `DataError`.
|
|
58
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
59
|
+
*
|
|
60
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
61
|
+
*/
|
|
62
|
+
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions$2): DataError;
|
|
63
|
+
/**
|
|
64
|
+
* 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.
|
|
65
|
+
*
|
|
66
|
+
* @param errorFunction - The function expected to throw the error.
|
|
67
|
+
* @param options - Extra options to apply.
|
|
68
|
+
*
|
|
69
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
70
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
71
|
+
*
|
|
72
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
73
|
+
*/
|
|
74
|
+
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$2): Promise<DataError>;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/root/deprecated/RecordKey.d.ts
|
|
78
|
+
/**
|
|
79
|
+
* Represents the native Record's possible key type.
|
|
80
|
+
*
|
|
81
|
+
* @category Types
|
|
82
|
+
*
|
|
83
|
+
* @deprecated Please use the native `PropertyKey` type instead.
|
|
84
|
+
*/
|
|
85
|
+
type RecordKey = string | number | symbol;
|
|
86
|
+
//#endregion
|
|
19
87
|
//#region src/root/functions/arrayHelpers/fillArray.d.ts
|
|
20
88
|
/**
|
|
21
89
|
* Creates a new array where each element is the resolved result of the provided asynchronous callback.
|
|
@@ -739,7 +807,7 @@ type VersionType = CreateEnumType<typeof VersionType>;
|
|
|
739
807
|
declare function parseVersionType(input: unknown): VersionType;
|
|
740
808
|
//#endregion
|
|
741
809
|
//#region src/v6/CodeError.d.ts
|
|
742
|
-
interface ExpectErrorOptions$
|
|
810
|
+
interface ExpectErrorOptions$1<ErrorCode extends string = string> {
|
|
743
811
|
expectedCode?: ErrorCode;
|
|
744
812
|
}
|
|
745
813
|
/**
|
|
@@ -765,7 +833,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
765
833
|
* @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`.
|
|
766
834
|
*/
|
|
767
835
|
static check(input: unknown): input is CodeError<string>;
|
|
768
|
-
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$
|
|
836
|
+
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
|
|
769
837
|
/**
|
|
770
838
|
* 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.
|
|
771
839
|
*
|
|
@@ -777,7 +845,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
777
845
|
*
|
|
778
846
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
779
847
|
*/
|
|
780
|
-
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$
|
|
848
|
+
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
|
|
781
849
|
/**
|
|
782
850
|
* 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.
|
|
783
851
|
*
|
|
@@ -789,12 +857,12 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
789
857
|
*
|
|
790
858
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
791
859
|
*/
|
|
792
|
-
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$
|
|
860
|
+
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
|
|
793
861
|
}
|
|
794
862
|
//#endregion
|
|
795
863
|
//#region src/v6/DataError.d.ts
|
|
796
864
|
type DefaultDataErrorCode = "INVALID_DATA";
|
|
797
|
-
interface ExpectErrorOptions
|
|
865
|
+
interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
|
|
798
866
|
expectedCode?: ErrorCode | DefaultDataErrorCode;
|
|
799
867
|
}
|
|
800
868
|
declare const DataErrorCode: {
|
|
@@ -836,7 +904,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
836
904
|
*
|
|
837
905
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
838
906
|
*/
|
|
839
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions
|
|
907
|
+
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
840
908
|
/**
|
|
841
909
|
* 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.
|
|
842
910
|
*
|
|
@@ -848,7 +916,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
848
916
|
*
|
|
849
917
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
850
918
|
*/
|
|
851
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions
|
|
919
|
+
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
852
920
|
}
|
|
853
921
|
//#endregion
|
|
854
922
|
//#region src/root/functions/parsers/zod/parseZodSchema.d.ts
|
|
@@ -1196,72 +1264,4 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
1196
1264
|
*/
|
|
1197
1265
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
1198
1266
|
//#endregion
|
|
1199
|
-
//#region src/root/deprecated/DataError.d.ts
|
|
1200
|
-
interface ExpectErrorOptions {
|
|
1201
|
-
expectedCode?: string;
|
|
1202
|
-
}
|
|
1203
|
-
/**
|
|
1204
|
-
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
1205
|
-
*
|
|
1206
|
-
* @category Types
|
|
1207
|
-
*
|
|
1208
|
-
* @deprecated Please use `DataError` from `@alextheman/utility/v6` instead.
|
|
1209
|
-
*
|
|
1210
|
-
* @template DataType - The type of the data that caused the error.
|
|
1211
|
-
*/
|
|
1212
|
-
declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Error {
|
|
1213
|
-
code: string;
|
|
1214
|
-
data: DataType;
|
|
1215
|
-
/**
|
|
1216
|
-
* @param data - The data that caused the error.
|
|
1217
|
-
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
1218
|
-
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
1219
|
-
* @param options - Extra options to pass to super Error constructor.
|
|
1220
|
-
*/
|
|
1221
|
-
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
1222
|
-
private static checkCaughtError;
|
|
1223
|
-
/**
|
|
1224
|
-
* Checks whether the given input may have been caused by a DataError.
|
|
1225
|
-
*
|
|
1226
|
-
* @param input - The input to check.
|
|
1227
|
-
*
|
|
1228
|
-
* @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`.
|
|
1229
|
-
*/
|
|
1230
|
-
static check<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
1231
|
-
/**
|
|
1232
|
-
* 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.
|
|
1233
|
-
*
|
|
1234
|
-
* @param errorFunction - The function expected to throw the error.
|
|
1235
|
-
* @param options - Extra options to apply.
|
|
1236
|
-
*
|
|
1237
|
-
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1238
|
-
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1239
|
-
*
|
|
1240
|
-
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1241
|
-
*/
|
|
1242
|
-
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions): DataError;
|
|
1243
|
-
/**
|
|
1244
|
-
* 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.
|
|
1245
|
-
*
|
|
1246
|
-
* @param errorFunction - The function expected to throw the error.
|
|
1247
|
-
* @param options - Extra options to apply.
|
|
1248
|
-
*
|
|
1249
|
-
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1250
|
-
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1251
|
-
*
|
|
1252
|
-
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1253
|
-
*/
|
|
1254
|
-
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions): Promise<DataError>;
|
|
1255
|
-
}
|
|
1256
|
-
//#endregion
|
|
1257
|
-
//#region src/root/deprecated/RecordKey.d.ts
|
|
1258
|
-
/**
|
|
1259
|
-
* Represents the native Record's possible key type.
|
|
1260
|
-
*
|
|
1261
|
-
* @category Types
|
|
1262
|
-
*
|
|
1263
|
-
* @deprecated Please use the native `PropertyKey` type instead.
|
|
1264
|
-
*/
|
|
1265
|
-
type RecordKey = string | number | symbol;
|
|
1266
|
-
//#endregion
|
|
1267
1267
|
export { APIError, type ArrayElement, type CallReturnType, CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, KebabToCamelOptions, type NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, ParallelTuple, RecordKey, RemoveUndefined, type StringListToArrayOptions, ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|
package/dist/index.d.ts
CHANGED
|
@@ -16,6 +16,74 @@ declare const UUID_REGEX: RegExp;
|
|
|
16
16
|
declare const VERSION_NUMBER_PATTERN: string;
|
|
17
17
|
declare const VERSION_NUMBER_REGEX: RegExp;
|
|
18
18
|
//#endregion
|
|
19
|
+
//#region src/root/deprecated/DataError.d.ts
|
|
20
|
+
interface ExpectErrorOptions$2 {
|
|
21
|
+
expectedCode?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
25
|
+
*
|
|
26
|
+
* @category Types
|
|
27
|
+
*
|
|
28
|
+
* @deprecated Please use `DataError` from `@alextheman/utility/v6` instead.
|
|
29
|
+
*
|
|
30
|
+
* @template DataType - The type of the data that caused the error.
|
|
31
|
+
*/
|
|
32
|
+
declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Error {
|
|
33
|
+
code: string;
|
|
34
|
+
data: DataType;
|
|
35
|
+
/**
|
|
36
|
+
* @param data - The data that caused the error.
|
|
37
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
38
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
39
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
40
|
+
*/
|
|
41
|
+
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
42
|
+
private static checkCaughtError;
|
|
43
|
+
/**
|
|
44
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
45
|
+
*
|
|
46
|
+
* @param input - The input to check.
|
|
47
|
+
*
|
|
48
|
+
* @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`.
|
|
49
|
+
*/
|
|
50
|
+
static check<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
51
|
+
/**
|
|
52
|
+
* 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.
|
|
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 `DataError`.
|
|
58
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
59
|
+
*
|
|
60
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
61
|
+
*/
|
|
62
|
+
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions$2): DataError;
|
|
63
|
+
/**
|
|
64
|
+
* 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.
|
|
65
|
+
*
|
|
66
|
+
* @param errorFunction - The function expected to throw the error.
|
|
67
|
+
* @param options - Extra options to apply.
|
|
68
|
+
*
|
|
69
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
70
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
71
|
+
*
|
|
72
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
73
|
+
*/
|
|
74
|
+
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$2): Promise<DataError>;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/root/deprecated/RecordKey.d.ts
|
|
78
|
+
/**
|
|
79
|
+
* Represents the native Record's possible key type.
|
|
80
|
+
*
|
|
81
|
+
* @category Types
|
|
82
|
+
*
|
|
83
|
+
* @deprecated Please use the native `PropertyKey` type instead.
|
|
84
|
+
*/
|
|
85
|
+
type RecordKey = string | number | symbol;
|
|
86
|
+
//#endregion
|
|
19
87
|
//#region src/root/functions/arrayHelpers/fillArray.d.ts
|
|
20
88
|
/**
|
|
21
89
|
* Creates a new array where each element is the resolved result of the provided asynchronous callback.
|
|
@@ -739,7 +807,7 @@ type VersionType = CreateEnumType<typeof VersionType>;
|
|
|
739
807
|
declare function parseVersionType(input: unknown): VersionType;
|
|
740
808
|
//#endregion
|
|
741
809
|
//#region src/v6/CodeError.d.ts
|
|
742
|
-
interface ExpectErrorOptions$
|
|
810
|
+
interface ExpectErrorOptions$1<ErrorCode extends string = string> {
|
|
743
811
|
expectedCode?: ErrorCode;
|
|
744
812
|
}
|
|
745
813
|
/**
|
|
@@ -765,7 +833,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
765
833
|
* @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`.
|
|
766
834
|
*/
|
|
767
835
|
static check(input: unknown): input is CodeError<string>;
|
|
768
|
-
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$
|
|
836
|
+
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
|
|
769
837
|
/**
|
|
770
838
|
* 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.
|
|
771
839
|
*
|
|
@@ -777,7 +845,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
777
845
|
*
|
|
778
846
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
779
847
|
*/
|
|
780
|
-
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$
|
|
848
|
+
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
|
|
781
849
|
/**
|
|
782
850
|
* 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.
|
|
783
851
|
*
|
|
@@ -789,12 +857,12 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
789
857
|
*
|
|
790
858
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
791
859
|
*/
|
|
792
|
-
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$
|
|
860
|
+
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
|
|
793
861
|
}
|
|
794
862
|
//#endregion
|
|
795
863
|
//#region src/v6/DataError.d.ts
|
|
796
864
|
type DefaultDataErrorCode = "INVALID_DATA";
|
|
797
|
-
interface ExpectErrorOptions
|
|
865
|
+
interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
|
|
798
866
|
expectedCode?: ErrorCode | DefaultDataErrorCode;
|
|
799
867
|
}
|
|
800
868
|
declare const DataErrorCode: {
|
|
@@ -836,7 +904,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
836
904
|
*
|
|
837
905
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
838
906
|
*/
|
|
839
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions
|
|
907
|
+
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
840
908
|
/**
|
|
841
909
|
* 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.
|
|
842
910
|
*
|
|
@@ -848,7 +916,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
848
916
|
*
|
|
849
917
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
850
918
|
*/
|
|
851
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions
|
|
919
|
+
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
852
920
|
}
|
|
853
921
|
//#endregion
|
|
854
922
|
//#region src/root/functions/parsers/zod/parseZodSchema.d.ts
|
|
@@ -1196,72 +1264,4 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
1196
1264
|
*/
|
|
1197
1265
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
1198
1266
|
//#endregion
|
|
1199
|
-
//#region src/root/deprecated/DataError.d.ts
|
|
1200
|
-
interface ExpectErrorOptions {
|
|
1201
|
-
expectedCode?: string;
|
|
1202
|
-
}
|
|
1203
|
-
/**
|
|
1204
|
-
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
1205
|
-
*
|
|
1206
|
-
* @category Types
|
|
1207
|
-
*
|
|
1208
|
-
* @deprecated Please use `DataError` from `@alextheman/utility/v6` instead.
|
|
1209
|
-
*
|
|
1210
|
-
* @template DataType - The type of the data that caused the error.
|
|
1211
|
-
*/
|
|
1212
|
-
declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Error {
|
|
1213
|
-
code: string;
|
|
1214
|
-
data: DataType;
|
|
1215
|
-
/**
|
|
1216
|
-
* @param data - The data that caused the error.
|
|
1217
|
-
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
1218
|
-
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
1219
|
-
* @param options - Extra options to pass to super Error constructor.
|
|
1220
|
-
*/
|
|
1221
|
-
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
1222
|
-
private static checkCaughtError;
|
|
1223
|
-
/**
|
|
1224
|
-
* Checks whether the given input may have been caused by a DataError.
|
|
1225
|
-
*
|
|
1226
|
-
* @param input - The input to check.
|
|
1227
|
-
*
|
|
1228
|
-
* @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`.
|
|
1229
|
-
*/
|
|
1230
|
-
static check<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
1231
|
-
/**
|
|
1232
|
-
* 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.
|
|
1233
|
-
*
|
|
1234
|
-
* @param errorFunction - The function expected to throw the error.
|
|
1235
|
-
* @param options - Extra options to apply.
|
|
1236
|
-
*
|
|
1237
|
-
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1238
|
-
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1239
|
-
*
|
|
1240
|
-
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1241
|
-
*/
|
|
1242
|
-
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions): DataError;
|
|
1243
|
-
/**
|
|
1244
|
-
* 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.
|
|
1245
|
-
*
|
|
1246
|
-
* @param errorFunction - The function expected to throw the error.
|
|
1247
|
-
* @param options - Extra options to apply.
|
|
1248
|
-
*
|
|
1249
|
-
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1250
|
-
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1251
|
-
*
|
|
1252
|
-
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1253
|
-
*/
|
|
1254
|
-
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions): Promise<DataError>;
|
|
1255
|
-
}
|
|
1256
|
-
//#endregion
|
|
1257
|
-
//#region src/root/deprecated/RecordKey.d.ts
|
|
1258
|
-
/**
|
|
1259
|
-
* Represents the native Record's possible key type.
|
|
1260
|
-
*
|
|
1261
|
-
* @category Types
|
|
1262
|
-
*
|
|
1263
|
-
* @deprecated Please use the native `PropertyKey` type instead.
|
|
1264
|
-
*/
|
|
1265
|
-
type RecordKey = string | number | symbol;
|
|
1266
|
-
//#endregion
|
|
1267
1267
|
export { APIError, type ArrayElement, type CallReturnType, CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, KebabToCamelOptions, type NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, ParallelTuple, RecordKey, RemoveUndefined, type StringListToArrayOptions, ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|
package/dist/index.js
CHANGED
|
@@ -156,7 +156,7 @@ var CodeError = class CodeError extends Error {
|
|
|
156
156
|
*
|
|
157
157
|
* @template DataType - The type of the data that caused the error.
|
|
158
158
|
*/
|
|
159
|
-
var DataError = class DataError extends CodeError {
|
|
159
|
+
var DataError$1 = class DataError$1 extends CodeError {
|
|
160
160
|
data;
|
|
161
161
|
/**
|
|
162
162
|
* @param data - The data that caused the error.
|
|
@@ -181,7 +181,7 @@ var DataError = class DataError extends CodeError {
|
|
|
181
181
|
* @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`.
|
|
182
182
|
*/
|
|
183
183
|
static check(input) {
|
|
184
|
-
if (input instanceof DataError) return true;
|
|
184
|
+
if (input instanceof DataError$1) return true;
|
|
185
185
|
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
186
186
|
}
|
|
187
187
|
/**
|
|
@@ -230,18 +230,18 @@ var DataError = class DataError extends CodeError {
|
|
|
230
230
|
function parseIntStrict(string, radix) {
|
|
231
231
|
const trimmedString = string.trim();
|
|
232
232
|
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
233
|
-
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError(radix ? {
|
|
233
|
+
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError$1(radix ? {
|
|
234
234
|
string,
|
|
235
235
|
radix
|
|
236
236
|
} : { string }, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
237
237
|
if (radix && radix < 10 && [...trimmedString.replace(/^[+-]/, "")].some((character) => {
|
|
238
238
|
return parseInt(character) >= radix;
|
|
239
|
-
})) throw new DataError({
|
|
239
|
+
})) throw new DataError$1({
|
|
240
240
|
string,
|
|
241
241
|
radix
|
|
242
242
|
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
243
243
|
const parseIntResult = parseInt(trimmedString, radix);
|
|
244
|
-
if (isNaN(parseIntResult)) throw new DataError({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
244
|
+
if (isNaN(parseIntResult)) throw new DataError$1({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
245
245
|
return parseIntResult;
|
|
246
246
|
}
|
|
247
247
|
//#endregion
|
|
@@ -304,16 +304,16 @@ function randomiseArray(array) {
|
|
|
304
304
|
*/
|
|
305
305
|
function range(start, stop, step = 1) {
|
|
306
306
|
const numbers = [];
|
|
307
|
-
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
307
|
+
if (step === 0) throw new DataError$1({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
308
308
|
else if (step > 0) {
|
|
309
|
-
if (start > stop) throw new DataError({
|
|
309
|
+
if (start > stop) throw new DataError$1({
|
|
310
310
|
start,
|
|
311
311
|
stop,
|
|
312
312
|
step
|
|
313
313
|
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
314
314
|
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
315
315
|
} else if (step < 0) {
|
|
316
|
-
if (start < stop) throw new DataError({
|
|
316
|
+
if (start < stop) throw new DataError$1({
|
|
317
317
|
start,
|
|
318
318
|
stop,
|
|
319
319
|
step
|
|
@@ -513,7 +513,7 @@ function convertFileToBase64(file) {
|
|
|
513
513
|
reader.readAsDataURL(file);
|
|
514
514
|
reader.onload = () => {
|
|
515
515
|
if (reader.result === null) {
|
|
516
|
-
reject(new DataError({ result: reader.result }, "FILE_CONVERSION_ERROR", "Could not convert the given file."));
|
|
516
|
+
reject(new DataError$1({ result: reader.result }, "FILE_CONVERSION_ERROR", "Could not convert the given file."));
|
|
517
517
|
return;
|
|
518
518
|
}
|
|
519
519
|
resolve(reader.result);
|
|
@@ -580,10 +580,10 @@ function createFormData(data, options = {
|
|
|
580
580
|
if (Array.isArray(value)) {
|
|
581
581
|
if (value.some((item) => {
|
|
582
582
|
return item instanceof Blob;
|
|
583
|
-
}) && (options.arrayResolution === "stringify" || typeof options.arrayResolution === "object" && options.arrayResolution[key] === "stringify")) throw new DataError({ value }, "CANNOT_STRINGIFY_BLOB", "Files/blobs cannot be stringified. Please change the resolution option for this item to \"multiple\" instead.");
|
|
583
|
+
}) && (options.arrayResolution === "stringify" || typeof options.arrayResolution === "object" && options.arrayResolution[key] === "stringify")) throw new DataError$1({ value }, "CANNOT_STRINGIFY_BLOB", "Files/blobs cannot be stringified. Please change the resolution option for this item to \"multiple\" instead.");
|
|
584
584
|
if (options.arrayResolution === "multiple" || typeof options.arrayResolution === "object" && options.arrayResolution[key] === "multiple") {
|
|
585
585
|
for (const item of value) {
|
|
586
|
-
if (!isPrimitive(item) && !(item instanceof Blob)) throw new DataError({ item }, "NON_PRIMITIVE_ARRAY_ITEMS_FOUND", "Cannot directly add non-primitive data to FormData. Please change the resolution option for this item to \"stringify\" instead.");
|
|
586
|
+
if (!isPrimitive(item) && !(item instanceof Blob)) throw new DataError$1({ item }, "NON_PRIMITIVE_ARRAY_ITEMS_FOUND", "Cannot directly add non-primitive data to FormData. Please change the resolution option for this item to \"stringify\" instead.");
|
|
587
587
|
if (item instanceof Blob) formData.append(String(key), item);
|
|
588
588
|
else formData.append(String(key), String(item));
|
|
589
589
|
}
|
|
@@ -739,8 +739,8 @@ function stringifyDotenv(contents, options) {
|
|
|
739
739
|
const { quoteStyle = "double" } = options ?? {};
|
|
740
740
|
let result = "";
|
|
741
741
|
for (const key in contentsCopy) {
|
|
742
|
-
if (/[ \t\r\n]/.test(key)) throw new DataError({ [key]: contentsCopy[key] }, "INVALID_KEY", "Environment variables are not allowed to have whitespace.");
|
|
743
|
-
if (quoteStyle === "none") if (/[ \t\r\n]/.test(contentsCopy[key]) || contentsCopy[key].includes("#") || contentsCopy[key].includes("=") || contentsCopy[key].includes("\\")) throw new DataError({ [key]: contentsCopy[key] }, "INCOMPATIBLE_QUOTE_STYLE", "Cannot use `{ quoteStyle: \"none\" }` when value has whitespace, #, =, or \\");
|
|
742
|
+
if (/[ \t\r\n]/.test(key)) throw new DataError$1({ [key]: contentsCopy[key] }, "INVALID_KEY", "Environment variables are not allowed to have whitespace.");
|
|
743
|
+
if (quoteStyle === "none") if (/[ \t\r\n]/.test(contentsCopy[key]) || contentsCopy[key].includes("#") || contentsCopy[key].includes("=") || contentsCopy[key].includes("\\")) throw new DataError$1({ [key]: contentsCopy[key] }, "INCOMPATIBLE_QUOTE_STYLE", "Cannot use `{ quoteStyle: \"none\" }` when value has whitespace, #, =, or \\");
|
|
744
744
|
else {
|
|
745
745
|
result += `${key}=${contentsCopy[key]}\n`;
|
|
746
746
|
continue;
|
|
@@ -865,7 +865,7 @@ function removeUndefinedFromObject(object) {
|
|
|
865
865
|
*/
|
|
866
866
|
function parseBoolean(inputString) {
|
|
867
867
|
const normalisedString = inputString.toLowerCase();
|
|
868
|
-
if (!["true", "false"].includes(normalisedString)) throw new DataError({ inputString }, "INVALID_BOOLEAN_STRING", "The provided boolean string must be one of `true | false`");
|
|
868
|
+
if (!["true", "false"].includes(normalisedString)) throw new DataError$1({ inputString }, "INVALID_BOOLEAN_STRING", "The provided boolean string must be one of `true | false`");
|
|
869
869
|
return normalisedString === "true";
|
|
870
870
|
}
|
|
871
871
|
//#endregion
|
|
@@ -884,7 +884,7 @@ function _parseZodSchema(parsedResult, input, onError) {
|
|
|
884
884
|
const code = issue.code.toUpperCase();
|
|
885
885
|
allErrorCodes[code] = (allErrorCodes[code] ?? 0) + 1;
|
|
886
886
|
}
|
|
887
|
-
throw new DataError({ input }, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
|
|
887
|
+
throw new DataError$1({ input }, Object.entries(allErrorCodes).toSorted(([_, firstCount], [__, secondCount]) => {
|
|
888
888
|
return secondCount - firstCount;
|
|
889
889
|
}).map(([code, count], _, allErrorCodes) => {
|
|
890
890
|
return allErrorCodes.length === 1 && count === 1 ? code : `${code}×${count}`;
|
|
@@ -939,7 +939,7 @@ const Env = {
|
|
|
939
939
|
* @returns The specified environment if allowed.
|
|
940
940
|
*/
|
|
941
941
|
function parseEnv(input) {
|
|
942
|
-
return parseZodSchema(z.enum(Env), input, new DataError({ input }, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
942
|
+
return parseZodSchema(z.enum(Env), input, new DataError$1({ input }, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
943
943
|
}
|
|
944
944
|
//#endregion
|
|
945
945
|
//#region src/root/functions/parsers/parseFormData.ts
|
|
@@ -977,8 +977,8 @@ function parseFormData(formData, dataParser) {
|
|
|
977
977
|
* @returns The UUID again if successful.
|
|
978
978
|
*/
|
|
979
979
|
function parseUUID(input) {
|
|
980
|
-
if (!(typeof input === "string")) throw new DataError({ input }, "INVALID_TYPE", "Invalid type - expected string.");
|
|
981
|
-
if (!UUID_REGEX.test(input)) throw new DataError({ input }, "INVALID_UUID", "The provided input does not match the expected shape for a UUID.");
|
|
980
|
+
if (!(typeof input === "string")) throw new DataError$1({ input }, "INVALID_TYPE", "Invalid type - expected string.");
|
|
981
|
+
if (!UUID_REGEX.test(input)) throw new DataError$1({ input }, "INVALID_UUID", "The provided input does not match the expected shape for a UUID.");
|
|
982
982
|
return input;
|
|
983
983
|
}
|
|
984
984
|
//#endregion
|
|
@@ -1005,7 +1005,7 @@ const VersionType = {
|
|
|
1005
1005
|
* @returns The given version type if allowed.
|
|
1006
1006
|
*/
|
|
1007
1007
|
function parseVersionType(input) {
|
|
1008
|
-
return parseZodSchema(z$1.enum(VersionType), input, new DataError({ input }, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
1008
|
+
return parseZodSchema(z$1.enum(VersionType), input, new DataError$1({ input }, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
1009
1009
|
}
|
|
1010
1010
|
//#endregion
|
|
1011
1011
|
//#region src/root/functions/parsers/zod/parseZodSchemaAsync.ts
|
|
@@ -1092,7 +1092,7 @@ function deepFreeze(object) {
|
|
|
1092
1092
|
* @returns A string with the semicolon appended.
|
|
1093
1093
|
*/
|
|
1094
1094
|
function appendSemicolon(stringToAppendTo) {
|
|
1095
|
-
if (stringToAppendTo.includes("\n")) throw new DataError({ stringToAppendTo }, "MULTIPLE_LINE_ERROR", "Cannot append semicolon to multi-line string.");
|
|
1095
|
+
if (stringToAppendTo.includes("\n")) throw new DataError$1({ stringToAppendTo }, "MULTIPLE_LINE_ERROR", "Cannot append semicolon to multi-line string.");
|
|
1096
1096
|
const stringWithNoTrailingWhitespace = stringToAppendTo.trimEnd();
|
|
1097
1097
|
if (stringWithNoTrailingWhitespace === "") return "";
|
|
1098
1098
|
return stringWithNoTrailingWhitespace[stringWithNoTrailingWhitespace.length - 1] === ";" ? stringWithNoTrailingWhitespace : `${stringWithNoTrailingWhitespace};`;
|
|
@@ -1160,9 +1160,9 @@ function escapeRegexPattern(regexPattern) {
|
|
|
1160
1160
|
* @returns The string converted to camelCase.
|
|
1161
1161
|
*/
|
|
1162
1162
|
function kebabToCamel(input, options) {
|
|
1163
|
-
if (input !== input.toLowerCase()) throw new DataError({ input }, "UPPERCASE_INPUT", "Kebab-case must be purely lowercase.");
|
|
1164
|
-
if (input.startsWith("-") || input.endsWith("-")) throw new DataError({ input }, "TRAILING_DASHES", "Dashes at the start and/or end are not allowed.");
|
|
1165
|
-
if (input.includes("--")) throw new DataError({ input }, "CONSECUTIVE_DASHES", "Consecutive dashes are not allowed.");
|
|
1163
|
+
if (input !== input.toLowerCase()) throw new DataError$1({ input }, "UPPERCASE_INPUT", "Kebab-case must be purely lowercase.");
|
|
1164
|
+
if (input.startsWith("-") || input.endsWith("-")) throw new DataError$1({ input }, "TRAILING_DASHES", "Dashes at the start and/or end are not allowed.");
|
|
1165
|
+
if (input.includes("--")) throw new DataError$1({ input }, "CONSECUTIVE_DASHES", "Consecutive dashes are not allowed.");
|
|
1166
1166
|
let outputString = "";
|
|
1167
1167
|
let skip = false;
|
|
1168
1168
|
for (const stringIndex in [...input]) {
|
|
@@ -1275,7 +1275,7 @@ function createTemplateStringsArray(strings) {
|
|
|
1275
1275
|
* ```
|
|
1276
1276
|
*/
|
|
1277
1277
|
function getStringsAndInterpolations(strings, ...interpolations) {
|
|
1278
|
-
if (strings.length !== interpolations.length + 1) throw new DataError({
|
|
1278
|
+
if (strings.length !== interpolations.length + 1) throw new DataError$1({
|
|
1279
1279
|
stringsLength: strings.length,
|
|
1280
1280
|
interpolationsLength: interpolations.length,
|
|
1281
1281
|
strings,
|
|
@@ -1441,6 +1441,97 @@ function normaliseIndents(first, ...args) {
|
|
|
1441
1441
|
*/
|
|
1442
1442
|
const normalizeIndents = normaliseIndents;
|
|
1443
1443
|
//#endregion
|
|
1444
|
+
//#region src/root/deprecated/DataError.ts
|
|
1445
|
+
/**
|
|
1446
|
+
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
1447
|
+
*
|
|
1448
|
+
* @category Types
|
|
1449
|
+
*
|
|
1450
|
+
* @deprecated Please use `DataError` from `@alextheman/utility/v6` instead.
|
|
1451
|
+
*
|
|
1452
|
+
* @template DataType - The type of the data that caused the error.
|
|
1453
|
+
*/
|
|
1454
|
+
var DataError = class DataError extends Error {
|
|
1455
|
+
code;
|
|
1456
|
+
data;
|
|
1457
|
+
/**
|
|
1458
|
+
* @param data - The data that caused the error.
|
|
1459
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
1460
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
1461
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
1462
|
+
*/
|
|
1463
|
+
constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
|
|
1464
|
+
super(message, options);
|
|
1465
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
1466
|
+
this.name = new.target.name;
|
|
1467
|
+
this.code = code;
|
|
1468
|
+
this.data = data;
|
|
1469
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
1470
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
1471
|
+
}
|
|
1472
|
+
static checkCaughtError(error, options) {
|
|
1473
|
+
if (DataError.check(error)) {
|
|
1474
|
+
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.
|
|
1475
|
+
|
|
1476
|
+
Expected: ${options.expectedCode}
|
|
1477
|
+
Received: ${error.code}
|
|
1478
|
+
`, { cause: error });
|
|
1479
|
+
return error;
|
|
1480
|
+
}
|
|
1481
|
+
throw error;
|
|
1482
|
+
}
|
|
1483
|
+
/**
|
|
1484
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
1485
|
+
*
|
|
1486
|
+
* @param input - The input to check.
|
|
1487
|
+
*
|
|
1488
|
+
* @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`.
|
|
1489
|
+
*/
|
|
1490
|
+
static check(input) {
|
|
1491
|
+
if (input instanceof DataError) return true;
|
|
1492
|
+
const data = input;
|
|
1493
|
+
return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
|
|
1494
|
+
}
|
|
1495
|
+
/**
|
|
1496
|
+
* 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.
|
|
1497
|
+
*
|
|
1498
|
+
* @param errorFunction - The function expected to throw the error.
|
|
1499
|
+
* @param options - Extra options to apply.
|
|
1500
|
+
*
|
|
1501
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1502
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1503
|
+
*
|
|
1504
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1505
|
+
*/
|
|
1506
|
+
static expectError(errorFunction, options) {
|
|
1507
|
+
try {
|
|
1508
|
+
errorFunction();
|
|
1509
|
+
} catch (error) {
|
|
1510
|
+
return DataError.checkCaughtError(error, options);
|
|
1511
|
+
}
|
|
1512
|
+
throw new Error("Expected a DataError to be thrown but none was thrown");
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* 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.
|
|
1516
|
+
*
|
|
1517
|
+
* @param errorFunction - The function expected to throw the error.
|
|
1518
|
+
* @param options - Extra options to apply.
|
|
1519
|
+
*
|
|
1520
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1521
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1522
|
+
*
|
|
1523
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1524
|
+
*/
|
|
1525
|
+
static async expectErrorAsync(errorFunction, options) {
|
|
1526
|
+
try {
|
|
1527
|
+
await errorFunction();
|
|
1528
|
+
} catch (error) {
|
|
1529
|
+
return DataError.checkCaughtError(error, options);
|
|
1530
|
+
}
|
|
1531
|
+
throw new Error("Expected a DataError to be thrown but none was thrown");
|
|
1532
|
+
}
|
|
1533
|
+
};
|
|
1534
|
+
//#endregion
|
|
1444
1535
|
//#region src/root/types/APIError.ts
|
|
1445
1536
|
const httpErrorCodeLookup = {
|
|
1446
1537
|
400: "BAD_REQUEST",
|
|
@@ -1507,7 +1598,7 @@ var VersionNumber = class VersionNumber {
|
|
|
1507
1598
|
this.minor = input.minor;
|
|
1508
1599
|
this.patch = input.patch;
|
|
1509
1600
|
} else if (typeof input === "string") {
|
|
1510
|
-
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.`);
|
|
1601
|
+
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError$1({ 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.`);
|
|
1511
1602
|
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
1512
1603
|
return parseIntStrict(number);
|
|
1513
1604
|
});
|
|
@@ -1515,16 +1606,16 @@ var VersionNumber = class VersionNumber {
|
|
|
1515
1606
|
this.minor = minor;
|
|
1516
1607
|
this.patch = patch;
|
|
1517
1608
|
} else if (Array.isArray(input)) {
|
|
1518
|
-
if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1609
|
+
if (input.length !== 3) throw new DataError$1({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1519
1610
|
const [major, minor, patch] = input.map((number) => {
|
|
1520
1611
|
const parsedInteger = parseIntStrict(number?.toString());
|
|
1521
|
-
if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1612
|
+
if (parsedInteger < 0) throw new DataError$1({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1522
1613
|
return parsedInteger;
|
|
1523
1614
|
});
|
|
1524
1615
|
this.major = major;
|
|
1525
1616
|
this.minor = minor;
|
|
1526
1617
|
this.patch = patch;
|
|
1527
|
-
} else throw new DataError({ input }, "INVALID_INPUT", normaliseIndents`
|
|
1618
|
+
} else throw new DataError$1({ input }, "INVALID_INPUT", normaliseIndents`
|
|
1528
1619
|
The provided input can not be parsed into a valid version number.
|
|
1529
1620
|
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
1530
1621
|
`);
|
|
@@ -1602,7 +1693,7 @@ var VersionNumber = class VersionNumber {
|
|
|
1602
1693
|
try {
|
|
1603
1694
|
return new VersionNumber(calculatedRawVersion);
|
|
1604
1695
|
} catch (error) {
|
|
1605
|
-
if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
|
|
1696
|
+
if (DataError$1.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError$1({
|
|
1606
1697
|
currentVersion: this.toString(),
|
|
1607
1698
|
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
1608
1699
|
incrementAmount
|
|
@@ -1618,7 +1709,7 @@ var VersionNumber = class VersionNumber {
|
|
|
1618
1709
|
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1619
1710
|
*/
|
|
1620
1711
|
[Symbol.toPrimitive](hint) {
|
|
1621
|
-
if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1712
|
+
if (hint === "number") throw new DataError$1({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1622
1713
|
return this.toString();
|
|
1623
1714
|
}
|
|
1624
1715
|
/**
|
|
@@ -1651,4 +1742,4 @@ const zodVersionNumber = z$1.union([
|
|
|
1651
1742
|
return new VersionNumber(rawVersionNumber);
|
|
1652
1743
|
});
|
|
1653
1744
|
//#endregion
|
|
1654
|
-
export { APIError, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, ONE_DAY_IN_MILLISECONDS, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, VersionType, addDaysToDate, appendSemicolon, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|
|
1745
|
+
export { APIError, DataError, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, ONE_DAY_IN_MILLISECONDS, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, VersionType, addDaysToDate, appendSemicolon, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|