@alextheman/utility 5.1.3 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +84 -43
- package/dist/index.d.cts +35 -27
- package/dist/index.d.ts +35 -27
- package/dist/index.js +85 -43
- package/dist/internal/index.cjs +49 -0
- package/dist/internal/index.d.cts +41 -13
- package/dist/internal/index.d.ts +41 -13
- package/dist/internal/index.js +49 -0
- package/dist/node/index.cjs +90 -41
- package/dist/node/index.js +90 -41
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -164,6 +164,17 @@ var DataError = class DataError extends Error {
|
|
|
164
164
|
Object.defineProperty(this, "message", { enumerable: true });
|
|
165
165
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
166
166
|
}
|
|
167
|
+
static checkCaughtError(error, options) {
|
|
168
|
+
if (DataError.check(error)) {
|
|
169
|
+
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.
|
|
170
|
+
|
|
171
|
+
Expected: ${options.expectedCode}
|
|
172
|
+
Received: ${error.code}
|
|
173
|
+
`, { cause: error });
|
|
174
|
+
return error;
|
|
175
|
+
}
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
167
178
|
/**
|
|
168
179
|
* Checks whether the given input may have been caused by a DataError.
|
|
169
180
|
*
|
|
@@ -176,6 +187,44 @@ var DataError = class DataError extends Error {
|
|
|
176
187
|
const data = input;
|
|
177
188
|
return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
|
|
178
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* 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.
|
|
192
|
+
*
|
|
193
|
+
* @param errorFunction - The function expected to throw the error.
|
|
194
|
+
* @param options - Extra options to apply.
|
|
195
|
+
*
|
|
196
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
197
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
198
|
+
*
|
|
199
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
200
|
+
*/
|
|
201
|
+
static expectError(errorFunction, options) {
|
|
202
|
+
try {
|
|
203
|
+
errorFunction();
|
|
204
|
+
} catch (error) {
|
|
205
|
+
return DataError.checkCaughtError(error, options);
|
|
206
|
+
}
|
|
207
|
+
throw new Error("Expected a DataError to be thrown but none was thrown");
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* 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.
|
|
211
|
+
*
|
|
212
|
+
* @param errorFunction - The function expected to throw the error.
|
|
213
|
+
* @param options - Extra options to apply.
|
|
214
|
+
*
|
|
215
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
216
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
217
|
+
*
|
|
218
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
219
|
+
*/
|
|
220
|
+
static async expectErrorAsync(errorFunction, options) {
|
|
221
|
+
try {
|
|
222
|
+
await errorFunction();
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return DataError.checkCaughtError(error, options);
|
|
225
|
+
}
|
|
226
|
+
throw new Error("Expected a DataError to be thrown but none was thrown");
|
|
227
|
+
}
|
|
179
228
|
};
|
|
180
229
|
|
|
181
230
|
//#endregion
|
|
@@ -430,19 +479,27 @@ function randomiseArray(array) {
|
|
|
430
479
|
* @param stop - The number to stop at (exclusive).
|
|
431
480
|
* @param step - The step size between numbers, defaulting to 1.
|
|
432
481
|
*
|
|
433
|
-
* @throws {
|
|
434
|
-
* @throws {
|
|
482
|
+
* @throws {DataError} If `step` is `0`.
|
|
483
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
435
484
|
*
|
|
436
485
|
* @returns An array of numbers satisfying the range provided.
|
|
437
486
|
*/
|
|
438
487
|
function range(start, stop, step = 1) {
|
|
439
488
|
const numbers = [];
|
|
440
|
-
if (step === 0) throw new
|
|
489
|
+
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
441
490
|
else if (step > 0) {
|
|
442
|
-
if (start > stop) throw new
|
|
491
|
+
if (start > stop) throw new DataError({
|
|
492
|
+
start,
|
|
493
|
+
stop,
|
|
494
|
+
step
|
|
495
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
443
496
|
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
444
497
|
} else if (step < 0) {
|
|
445
|
-
if (start < stop) throw new
|
|
498
|
+
if (start < stop) throw new DataError({
|
|
499
|
+
start,
|
|
500
|
+
stop,
|
|
501
|
+
step
|
|
502
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
446
503
|
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
447
504
|
}
|
|
448
505
|
return numbers;
|
|
@@ -613,25 +670,6 @@ function isMonthlyMultiple(firstDate, secondDate) {
|
|
|
613
670
|
return firstDate.getDate() === secondDate.getDate();
|
|
614
671
|
}
|
|
615
672
|
|
|
616
|
-
//#endregion
|
|
617
|
-
//#region src/internal/getDependenciesFromGroup.ts
|
|
618
|
-
/**
|
|
619
|
-
* Get the dependencies from a given dependency group in `package.json`.
|
|
620
|
-
*
|
|
621
|
-
* @category Miscellaneous
|
|
622
|
-
*
|
|
623
|
-
* @param packageInfo - The data coming from `package.json`.
|
|
624
|
-
* @param dependencyGroup - The group to get dependency information about (can be `dependencies` or `devDependencies`).
|
|
625
|
-
*
|
|
626
|
-
* @returns A record consisting of the package names and version ranges from the given dependency group.
|
|
627
|
-
*/
|
|
628
|
-
function getDependenciesFromGroup(packageInfo, dependencyGroup) {
|
|
629
|
-
return {
|
|
630
|
-
dependencies: parseZodSchema(zod.default.record(zod.default.string(), zod.default.string()), packageInfo.dependencies ?? {}),
|
|
631
|
-
devDependencies: parseZodSchema(zod.default.record(zod.default.string(), zod.default.string()), packageInfo.devDependencies ?? {})
|
|
632
|
-
}[dependencyGroup];
|
|
633
|
-
}
|
|
634
|
-
|
|
635
673
|
//#endregion
|
|
636
674
|
//#region src/root/functions/miscellaneous/convertFileToBase64.ts
|
|
637
675
|
/**
|
|
@@ -641,7 +679,7 @@ function getDependenciesFromGroup(packageInfo, dependencyGroup) {
|
|
|
641
679
|
*
|
|
642
680
|
* @param file - The file to convert.
|
|
643
681
|
*
|
|
644
|
-
* @throws {Error} If the file reader gives an error.
|
|
682
|
+
* @throws {Error | DataError} If the file reader gives an error.
|
|
645
683
|
*
|
|
646
684
|
* @returns A promise that resolves to the encoded base 64 string.
|
|
647
685
|
*/
|
|
@@ -651,7 +689,7 @@ function convertFileToBase64(file) {
|
|
|
651
689
|
reader.readAsDataURL(file);
|
|
652
690
|
reader.onload = () => {
|
|
653
691
|
if (reader.result === null) {
|
|
654
|
-
reject(
|
|
692
|
+
reject(new DataError({ result: reader.result }, "FILE_CONVERSION_ERROR", "Could not convert the given file."));
|
|
655
693
|
return;
|
|
656
694
|
}
|
|
657
695
|
resolve(reader.result);
|
|
@@ -667,6 +705,9 @@ function convertFileToBase64(file) {
|
|
|
667
705
|
function getNullableResolutionStrategy(key, strategy) {
|
|
668
706
|
return (typeof strategy === "object" ? strategy[key] : strategy) ?? "empty";
|
|
669
707
|
}
|
|
708
|
+
function isPrimitive(item) {
|
|
709
|
+
return typeof item === "string" || typeof item === "number" || typeof item === "boolean";
|
|
710
|
+
}
|
|
670
711
|
/**
|
|
671
712
|
* Creates FormData from a given object, resolving non-string types as appropriate.
|
|
672
713
|
*
|
|
@@ -693,7 +734,7 @@ function createFormData(data, options = {
|
|
|
693
734
|
formData.append(String(key), JSON.stringify(value));
|
|
694
735
|
break;
|
|
695
736
|
case "omit": break;
|
|
696
|
-
default: throw
|
|
737
|
+
default: throw resolutionStrategy;
|
|
697
738
|
}
|
|
698
739
|
}
|
|
699
740
|
function resolveNullables(key, value, options) {
|
|
@@ -716,10 +757,10 @@ function createFormData(data, options = {
|
|
|
716
757
|
if (Array.isArray(value)) {
|
|
717
758
|
if (value.some((item) => {
|
|
718
759
|
return item instanceof Blob;
|
|
719
|
-
}) && (options.arrayResolution === "stringify" || typeof options.arrayResolution === "object" && options.arrayResolution[key] === "stringify")) throw new
|
|
760
|
+
}) && (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.");
|
|
720
761
|
if (options.arrayResolution === "multiple" || typeof options.arrayResolution === "object" && options.arrayResolution[key] === "multiple") {
|
|
721
762
|
for (const item of value) {
|
|
722
|
-
if ((
|
|
763
|
+
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.");
|
|
723
764
|
if (item instanceof Blob) formData.append(String(key), item);
|
|
724
765
|
else formData.append(String(key), String(item));
|
|
725
766
|
}
|
|
@@ -1461,12 +1502,12 @@ async function encryptWithKey(publicKey, plaintextValue) {
|
|
|
1461
1502
|
*
|
|
1462
1503
|
* @param stringToAppendTo - The string to append a semicolon to.
|
|
1463
1504
|
*
|
|
1464
|
-
* @throws {
|
|
1505
|
+
* @throws {DataError} If the string contains multiple lines.
|
|
1465
1506
|
*
|
|
1466
1507
|
* @returns A string with the semicolon appended.
|
|
1467
1508
|
*/
|
|
1468
1509
|
function appendSemicolon(stringToAppendTo) {
|
|
1469
|
-
if (stringToAppendTo.includes("\n")) throw new
|
|
1510
|
+
if (stringToAppendTo.includes("\n")) throw new DataError({ stringToAppendTo }, "MULTIPLE_LINE_ERROR", "Cannot append semicolon to multi-line string.");
|
|
1470
1511
|
const stringWithNoTrailingWhitespace = stringToAppendTo.trimEnd();
|
|
1471
1512
|
if (stringWithNoTrailingWhitespace === "") return "";
|
|
1472
1513
|
return stringWithNoTrailingWhitespace[stringWithNoTrailingWhitespace.length - 1] === ";" ? stringWithNoTrailingWhitespace : `${stringWithNoTrailingWhitespace};`;
|
|
@@ -1518,34 +1559,35 @@ function camelToKebab(string, options = { preserveConsecutiveCapitals: true }) {
|
|
|
1518
1559
|
*
|
|
1519
1560
|
* @category String Helpers
|
|
1520
1561
|
*
|
|
1521
|
-
* @param
|
|
1562
|
+
* @param input - The string to convert.
|
|
1522
1563
|
* @param options - Options to apply to the conversion.
|
|
1523
1564
|
*
|
|
1524
1565
|
* @returns The string converted to camelCase.
|
|
1525
1566
|
*/
|
|
1526
|
-
function kebabToCamel(
|
|
1527
|
-
if (
|
|
1528
|
-
if (
|
|
1567
|
+
function kebabToCamel(input, options) {
|
|
1568
|
+
if (input !== input.toLowerCase()) throw new DataError({ input }, "UPPERCASE_INPUT", "Kebab-case must be purely lowercase.");
|
|
1569
|
+
if (input.startsWith("-") || input.endsWith("-")) throw new DataError({ input }, "TRAILING_DASHES", "Dashes at the start and/or end are not allowed.");
|
|
1570
|
+
if (input.includes("--")) throw new DataError({ input }, "CONSECUTIVE_DASHES", "Consecutive dashes are not allowed.");
|
|
1529
1571
|
let outputString = "";
|
|
1530
1572
|
let skip = false;
|
|
1531
|
-
for (const stringIndex in [...
|
|
1573
|
+
for (const stringIndex in [...input]) {
|
|
1532
1574
|
if (skip) {
|
|
1533
1575
|
skip = false;
|
|
1534
1576
|
continue;
|
|
1535
1577
|
}
|
|
1536
1578
|
const index = parseIntStrict(stringIndex);
|
|
1537
1579
|
if (index === 0 && options?.startWithUpper) {
|
|
1538
|
-
outputString +=
|
|
1580
|
+
outputString += input[index].toUpperCase();
|
|
1539
1581
|
continue;
|
|
1540
1582
|
}
|
|
1541
|
-
if (index ===
|
|
1542
|
-
outputString +=
|
|
1583
|
+
if (index === input.length - 1) {
|
|
1584
|
+
outputString += input[index];
|
|
1543
1585
|
break;
|
|
1544
1586
|
}
|
|
1545
|
-
if (
|
|
1546
|
-
outputString +=
|
|
1587
|
+
if (input[index] === "-" && /^[a-zA-Z]+$/.test(input[index + 1])) {
|
|
1588
|
+
outputString += input[index + 1].toUpperCase();
|
|
1547
1589
|
skip = true;
|
|
1548
|
-
} else outputString +=
|
|
1590
|
+
} else outputString += input[index];
|
|
1549
1591
|
}
|
|
1550
1592
|
return outputString;
|
|
1551
1593
|
}
|
|
@@ -1586,7 +1628,6 @@ exports.deepFreeze = deepFreeze;
|
|
|
1586
1628
|
exports.encryptWithKey = encryptWithKey;
|
|
1587
1629
|
exports.fillArray = fillArray;
|
|
1588
1630
|
exports.formatDateAndTime = formatDateAndTime;
|
|
1589
|
-
exports.getDependenciesFromGroup = getDependenciesFromGroup;
|
|
1590
1631
|
exports.getRandomNumber = getRandomNumber;
|
|
1591
1632
|
exports.getRecordKeys = getRecordKeys;
|
|
1592
1633
|
exports.getStringsAndInterpolations = getStringsAndInterpolations;
|
package/dist/index.d.cts
CHANGED
|
@@ -87,8 +87,8 @@ declare function randomiseArray<ItemType>(array: ItemType[]): ItemType[];
|
|
|
87
87
|
* @param stop - The number to stop at (exclusive).
|
|
88
88
|
* @param step - The step size between numbers, defaulting to 1.
|
|
89
89
|
*
|
|
90
|
-
* @throws {
|
|
91
|
-
* @throws {
|
|
90
|
+
* @throws {DataError} If `step` is `0`.
|
|
91
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
92
92
|
*
|
|
93
93
|
* @returns An array of numbers satisfying the range provided.
|
|
94
94
|
*/
|
|
@@ -190,26 +190,6 @@ declare function isMonthlyMultiple(firstDate: Date, secondDate: Date): boolean;
|
|
|
190
190
|
*/
|
|
191
191
|
declare function isSameDate(firstDate: Date, secondDate: Date): boolean;
|
|
192
192
|
//#endregion
|
|
193
|
-
//#region src/internal/DependencyGroup.d.ts
|
|
194
|
-
declare const DependencyGroup: {
|
|
195
|
-
readonly DEPENDENCIES: "dependencies";
|
|
196
|
-
readonly DEV_DEPENDENCIES: "devDependencies";
|
|
197
|
-
};
|
|
198
|
-
type DependencyGroup = CreateEnumType<typeof DependencyGroup>;
|
|
199
|
-
//#endregion
|
|
200
|
-
//#region src/internal/getDependenciesFromGroup.d.ts
|
|
201
|
-
/**
|
|
202
|
-
* Get the dependencies from a given dependency group in `package.json`.
|
|
203
|
-
*
|
|
204
|
-
* @category Miscellaneous
|
|
205
|
-
*
|
|
206
|
-
* @param packageInfo - The data coming from `package.json`.
|
|
207
|
-
* @param dependencyGroup - The group to get dependency information about (can be `dependencies` or `devDependencies`).
|
|
208
|
-
*
|
|
209
|
-
* @returns A record consisting of the package names and version ranges from the given dependency group.
|
|
210
|
-
*/
|
|
211
|
-
declare function getDependenciesFromGroup(packageInfo: Record<string, unknown>, dependencyGroup: DependencyGroup): Record<string, string>;
|
|
212
|
-
//#endregion
|
|
213
193
|
//#region src/root/functions/miscellaneous/convertFileToBase64.d.ts
|
|
214
194
|
/**
|
|
215
195
|
* Asynchronously converts a file to a base 64 string
|
|
@@ -218,7 +198,7 @@ declare function getDependenciesFromGroup(packageInfo: Record<string, unknown>,
|
|
|
218
198
|
*
|
|
219
199
|
* @param file - The file to convert.
|
|
220
200
|
*
|
|
221
|
-
* @throws {Error} If the file reader gives an error.
|
|
201
|
+
* @throws {Error | DataError} If the file reader gives an error.
|
|
222
202
|
*
|
|
223
203
|
* @returns A promise that resolves to the encoded base 64 string.
|
|
224
204
|
*/
|
|
@@ -259,6 +239,9 @@ declare class APIError extends Error {
|
|
|
259
239
|
type RecordKey = string | number | symbol;
|
|
260
240
|
//#endregion
|
|
261
241
|
//#region src/root/types/DataError.d.ts
|
|
242
|
+
interface ExpectErrorOptions {
|
|
243
|
+
expectedCode?: string;
|
|
244
|
+
}
|
|
262
245
|
/**
|
|
263
246
|
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
264
247
|
*
|
|
@@ -276,6 +259,7 @@ declare class DataError<DataType extends Record<RecordKey, unknown> = Record<Rec
|
|
|
276
259
|
* @param options - Extra options to pass to super Error constructor.
|
|
277
260
|
*/
|
|
278
261
|
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
262
|
+
private static checkCaughtError;
|
|
279
263
|
/**
|
|
280
264
|
* Checks whether the given input may have been caused by a DataError.
|
|
281
265
|
*
|
|
@@ -284,6 +268,30 @@ declare class DataError<DataType extends Record<RecordKey, unknown> = Record<Rec
|
|
|
284
268
|
* @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`.
|
|
285
269
|
*/
|
|
286
270
|
static check<DataType extends Record<RecordKey, unknown> = Record<RecordKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
271
|
+
/**
|
|
272
|
+
* 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.
|
|
273
|
+
*
|
|
274
|
+
* @param errorFunction - The function expected to throw the error.
|
|
275
|
+
* @param options - Extra options to apply.
|
|
276
|
+
*
|
|
277
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
278
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
279
|
+
*
|
|
280
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
281
|
+
*/
|
|
282
|
+
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions): DataError;
|
|
283
|
+
/**
|
|
284
|
+
* 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.
|
|
285
|
+
*
|
|
286
|
+
* @param errorFunction - The function expected to throw the error.
|
|
287
|
+
* @param options - Extra options to apply.
|
|
288
|
+
*
|
|
289
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
290
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
291
|
+
*
|
|
292
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
293
|
+
*/
|
|
294
|
+
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions): Promise<DataError>;
|
|
287
295
|
}
|
|
288
296
|
//#endregion
|
|
289
297
|
//#region src/root/types/VersionNumber.d.ts
|
|
@@ -841,7 +849,7 @@ declare function encryptWithKey(publicKey: string, plaintextValue: string): Prom
|
|
|
841
849
|
*
|
|
842
850
|
* @param stringToAppendTo - The string to append a semicolon to.
|
|
843
851
|
*
|
|
844
|
-
* @throws {
|
|
852
|
+
* @throws {DataError} If the string contains multiple lines.
|
|
845
853
|
*
|
|
846
854
|
* @returns A string with the semicolon appended.
|
|
847
855
|
*/
|
|
@@ -884,12 +892,12 @@ interface KebabToCamelOptions {
|
|
|
884
892
|
*
|
|
885
893
|
* @category String Helpers
|
|
886
894
|
*
|
|
887
|
-
* @param
|
|
895
|
+
* @param input - The string to convert.
|
|
888
896
|
* @param options - Options to apply to the conversion.
|
|
889
897
|
*
|
|
890
898
|
* @returns The string converted to camelCase.
|
|
891
899
|
*/
|
|
892
|
-
declare function kebabToCamel(
|
|
900
|
+
declare function kebabToCamel(input: string, options?: KebabToCamelOptions): string;
|
|
893
901
|
//#endregion
|
|
894
902
|
//#region src/root/functions/stringHelpers/truncate.d.ts
|
|
895
903
|
/**
|
|
@@ -1072,4 +1080,4 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
1072
1080
|
*/
|
|
1073
1081
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
1074
1082
|
//#endregion
|
|
1075
|
-
export { APIError, ArrayElement, CallReturnType, CamelToKebabOptions, CreateEnumType, CreateFormDataOptions, CreateFormDataOptionsNullableResolution, CreateFormDataOptionsUndefinedOrNullResolution, DataError, DisallowUndefined, Env, FILE_PATH_REGEX, FormDataArrayResolutionStrategy, FormDataNullableResolutionStrategy, HTTPErrorCode, IgnoreCase, IsTypeArgumentString, KebabToCamelOptions, NAMESPACE_EXPORT_REGEX, NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, OptionalOnCondition, ParallelTuple, RecordKey, RemoveUndefined, StringListToArrayOptions, VERSION_NUMBER_REGEX, VersionNumber, FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, encryptWithKey, fillArray, formatDateAndTime,
|
|
1083
|
+
export { APIError, ArrayElement, CallReturnType, CamelToKebabOptions, CreateEnumType, CreateFormDataOptions, CreateFormDataOptionsNullableResolution, CreateFormDataOptionsUndefinedOrNullResolution, DataError, DisallowUndefined, Env, FILE_PATH_REGEX, FormDataArrayResolutionStrategy, FormDataNullableResolutionStrategy, HTTPErrorCode, IgnoreCase, IsTypeArgumentString, KebabToCamelOptions, NAMESPACE_EXPORT_REGEX, NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, OptionalOnCondition, ParallelTuple, RecordKey, RemoveUndefined, StringListToArrayOptions, VERSION_NUMBER_REGEX, VersionNumber, FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, encryptWithKey, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, truncate, wait, zodVersionNumber };
|
package/dist/index.d.ts
CHANGED
|
@@ -87,8 +87,8 @@ declare function randomiseArray<ItemType>(array: ItemType[]): ItemType[];
|
|
|
87
87
|
* @param stop - The number to stop at (exclusive).
|
|
88
88
|
* @param step - The step size between numbers, defaulting to 1.
|
|
89
89
|
*
|
|
90
|
-
* @throws {
|
|
91
|
-
* @throws {
|
|
90
|
+
* @throws {DataError} If `step` is `0`.
|
|
91
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
92
92
|
*
|
|
93
93
|
* @returns An array of numbers satisfying the range provided.
|
|
94
94
|
*/
|
|
@@ -190,26 +190,6 @@ declare function isMonthlyMultiple(firstDate: Date, secondDate: Date): boolean;
|
|
|
190
190
|
*/
|
|
191
191
|
declare function isSameDate(firstDate: Date, secondDate: Date): boolean;
|
|
192
192
|
//#endregion
|
|
193
|
-
//#region src/internal/DependencyGroup.d.ts
|
|
194
|
-
declare const DependencyGroup: {
|
|
195
|
-
readonly DEPENDENCIES: "dependencies";
|
|
196
|
-
readonly DEV_DEPENDENCIES: "devDependencies";
|
|
197
|
-
};
|
|
198
|
-
type DependencyGroup = CreateEnumType<typeof DependencyGroup>;
|
|
199
|
-
//#endregion
|
|
200
|
-
//#region src/internal/getDependenciesFromGroup.d.ts
|
|
201
|
-
/**
|
|
202
|
-
* Get the dependencies from a given dependency group in `package.json`.
|
|
203
|
-
*
|
|
204
|
-
* @category Miscellaneous
|
|
205
|
-
*
|
|
206
|
-
* @param packageInfo - The data coming from `package.json`.
|
|
207
|
-
* @param dependencyGroup - The group to get dependency information about (can be `dependencies` or `devDependencies`).
|
|
208
|
-
*
|
|
209
|
-
* @returns A record consisting of the package names and version ranges from the given dependency group.
|
|
210
|
-
*/
|
|
211
|
-
declare function getDependenciesFromGroup(packageInfo: Record<string, unknown>, dependencyGroup: DependencyGroup): Record<string, string>;
|
|
212
|
-
//#endregion
|
|
213
193
|
//#region src/root/functions/miscellaneous/convertFileToBase64.d.ts
|
|
214
194
|
/**
|
|
215
195
|
* Asynchronously converts a file to a base 64 string
|
|
@@ -218,7 +198,7 @@ declare function getDependenciesFromGroup(packageInfo: Record<string, unknown>,
|
|
|
218
198
|
*
|
|
219
199
|
* @param file - The file to convert.
|
|
220
200
|
*
|
|
221
|
-
* @throws {Error} If the file reader gives an error.
|
|
201
|
+
* @throws {Error | DataError} If the file reader gives an error.
|
|
222
202
|
*
|
|
223
203
|
* @returns A promise that resolves to the encoded base 64 string.
|
|
224
204
|
*/
|
|
@@ -259,6 +239,9 @@ declare class APIError extends Error {
|
|
|
259
239
|
type RecordKey = string | number | symbol;
|
|
260
240
|
//#endregion
|
|
261
241
|
//#region src/root/types/DataError.d.ts
|
|
242
|
+
interface ExpectErrorOptions {
|
|
243
|
+
expectedCode?: string;
|
|
244
|
+
}
|
|
262
245
|
/**
|
|
263
246
|
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
264
247
|
*
|
|
@@ -276,6 +259,7 @@ declare class DataError<DataType extends Record<RecordKey, unknown> = Record<Rec
|
|
|
276
259
|
* @param options - Extra options to pass to super Error constructor.
|
|
277
260
|
*/
|
|
278
261
|
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
262
|
+
private static checkCaughtError;
|
|
279
263
|
/**
|
|
280
264
|
* Checks whether the given input may have been caused by a DataError.
|
|
281
265
|
*
|
|
@@ -284,6 +268,30 @@ declare class DataError<DataType extends Record<RecordKey, unknown> = Record<Rec
|
|
|
284
268
|
* @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`.
|
|
285
269
|
*/
|
|
286
270
|
static check<DataType extends Record<RecordKey, unknown> = Record<RecordKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
271
|
+
/**
|
|
272
|
+
* 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.
|
|
273
|
+
*
|
|
274
|
+
* @param errorFunction - The function expected to throw the error.
|
|
275
|
+
* @param options - Extra options to apply.
|
|
276
|
+
*
|
|
277
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
278
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
279
|
+
*
|
|
280
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
281
|
+
*/
|
|
282
|
+
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions): DataError;
|
|
283
|
+
/**
|
|
284
|
+
* 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.
|
|
285
|
+
*
|
|
286
|
+
* @param errorFunction - The function expected to throw the error.
|
|
287
|
+
* @param options - Extra options to apply.
|
|
288
|
+
*
|
|
289
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
290
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
291
|
+
*
|
|
292
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
293
|
+
*/
|
|
294
|
+
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions): Promise<DataError>;
|
|
287
295
|
}
|
|
288
296
|
//#endregion
|
|
289
297
|
//#region src/root/types/VersionNumber.d.ts
|
|
@@ -841,7 +849,7 @@ declare function encryptWithKey(publicKey: string, plaintextValue: string): Prom
|
|
|
841
849
|
*
|
|
842
850
|
* @param stringToAppendTo - The string to append a semicolon to.
|
|
843
851
|
*
|
|
844
|
-
* @throws {
|
|
852
|
+
* @throws {DataError} If the string contains multiple lines.
|
|
845
853
|
*
|
|
846
854
|
* @returns A string with the semicolon appended.
|
|
847
855
|
*/
|
|
@@ -884,12 +892,12 @@ interface KebabToCamelOptions {
|
|
|
884
892
|
*
|
|
885
893
|
* @category String Helpers
|
|
886
894
|
*
|
|
887
|
-
* @param
|
|
895
|
+
* @param input - The string to convert.
|
|
888
896
|
* @param options - Options to apply to the conversion.
|
|
889
897
|
*
|
|
890
898
|
* @returns The string converted to camelCase.
|
|
891
899
|
*/
|
|
892
|
-
declare function kebabToCamel(
|
|
900
|
+
declare function kebabToCamel(input: string, options?: KebabToCamelOptions): string;
|
|
893
901
|
//#endregion
|
|
894
902
|
//#region src/root/functions/stringHelpers/truncate.d.ts
|
|
895
903
|
/**
|
|
@@ -1072,4 +1080,4 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
1072
1080
|
*/
|
|
1073
1081
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
1074
1082
|
//#endregion
|
|
1075
|
-
export { APIError, type ArrayElement, type CallReturnType, CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, KebabToCamelOptions, NAMESPACE_EXPORT_REGEX, type NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, type OptionalOnCondition, ParallelTuple, type RecordKey, type RemoveUndefined, type StringListToArrayOptions, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, encryptWithKey, fillArray, formatDateAndTime,
|
|
1083
|
+
export { APIError, type ArrayElement, type CallReturnType, CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, KebabToCamelOptions, NAMESPACE_EXPORT_REGEX, type NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, type OptionalOnCondition, ParallelTuple, type RecordKey, type RemoveUndefined, type StringListToArrayOptions, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, encryptWithKey, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, truncate, wait, zodVersionNumber };
|