@alextheman/utility 3.10.0 → 4.0.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/LICENSE +21 -0
- package/dist/index.cjs +123 -178
- package/dist/index.d.cts +13 -48
- package/dist/index.d.ts +13 -48
- package/dist/index.js +124 -175
- package/package.json +14 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License Copyright (c) 2025 AlexMan123456
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free
|
|
4
|
+
of charge, to any person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use, copy, modify, merge,
|
|
7
|
+
publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice
|
|
12
|
+
(including the next paragraph) shall be included in all copies or substantial
|
|
13
|
+
portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
16
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
18
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
19
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -88,27 +88,108 @@ function paralleliseArrays(firstArray, secondArray) {
|
|
|
88
88
|
}
|
|
89
89
|
var paralleliseArrays_default = paralleliseArrays;
|
|
90
90
|
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/types/APIError.ts
|
|
93
|
+
const httpErrorCodeLookup = {
|
|
94
|
+
400: "BAD_REQUEST",
|
|
95
|
+
401: "UNAUTHORISED",
|
|
96
|
+
403: "FORBIDDEN",
|
|
97
|
+
404: "NOT_FOUND",
|
|
98
|
+
418: "I_AM_A_TEAPOT",
|
|
99
|
+
500: "INTERNAL_SERVER_ERROR"
|
|
100
|
+
};
|
|
101
|
+
/** Represents common errors you may get from a HTTP API request. */
|
|
102
|
+
var APIError = class extends Error {
|
|
103
|
+
status;
|
|
104
|
+
/**
|
|
105
|
+
* @param status - A HTTP status code. Can be any number, but numbers between 400 and 600 are encouraged to fit with HTTP status code conventions.
|
|
106
|
+
* @param message - An error message to display alongside the status code.
|
|
107
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
108
|
+
*/
|
|
109
|
+
constructor(status = 500, message, options) {
|
|
110
|
+
super(message, options);
|
|
111
|
+
this.status = status;
|
|
112
|
+
if (message) this.message = message;
|
|
113
|
+
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
114
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
115
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Checks whether the given input may have been caused by an APIError.
|
|
119
|
+
*
|
|
120
|
+
* @param input - The input to check.
|
|
121
|
+
*
|
|
122
|
+
* @returns `true` if the input is an APIError, and `false` otherwise. The type of the input will also be narrowed down to APIError if `true`.
|
|
123
|
+
*/
|
|
124
|
+
static check(input) {
|
|
125
|
+
const data = input;
|
|
126
|
+
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var APIError_default = APIError;
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/types/DataError.ts
|
|
133
|
+
/** Represents errors you may get that may've been caused by a specific piece of data. */
|
|
134
|
+
var DataError = class extends Error {
|
|
135
|
+
data;
|
|
136
|
+
code;
|
|
137
|
+
/**
|
|
138
|
+
* @param data - The data that caused the error.
|
|
139
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
140
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
141
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
142
|
+
*/
|
|
143
|
+
constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
|
|
144
|
+
super(message, options);
|
|
145
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
146
|
+
this.name = new.target.name;
|
|
147
|
+
this.code = code;
|
|
148
|
+
this.data = data;
|
|
149
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
150
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
154
|
+
*
|
|
155
|
+
* @param input - The input to check.
|
|
156
|
+
*
|
|
157
|
+
* @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`.
|
|
158
|
+
*/
|
|
159
|
+
static check(input) {
|
|
160
|
+
const data = input;
|
|
161
|
+
return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
var DataError_default = DataError;
|
|
165
|
+
|
|
91
166
|
//#endregion
|
|
92
167
|
//#region src/functions/parsers/parseIntStrict.ts
|
|
93
|
-
const IntegerParsingError = /* @__PURE__ */ new TypeError("INTEGER_PARSING_ERROR");
|
|
94
168
|
/**
|
|
95
169
|
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
96
170
|
*
|
|
97
171
|
* @param string - A string to convert into a number.
|
|
98
172
|
* @param radix - A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.
|
|
99
173
|
*
|
|
100
|
-
* @throws {
|
|
174
|
+
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
101
175
|
*
|
|
102
176
|
* @returns The integer parsed from the input string.
|
|
103
177
|
*/
|
|
104
178
|
function parseIntStrict(string, radix) {
|
|
105
179
|
const trimmedString = string.trim();
|
|
106
|
-
|
|
180
|
+
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
181
|
+
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError_default(radix ? {
|
|
182
|
+
string,
|
|
183
|
+
radix
|
|
184
|
+
} : string, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
107
185
|
if (radix && radix < 10 && [...trimmedString].some((character) => {
|
|
108
186
|
return parseInt(character) >= radix;
|
|
109
|
-
})) throw
|
|
187
|
+
})) throw new DataError_default({
|
|
188
|
+
string,
|
|
189
|
+
radix
|
|
190
|
+
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
110
191
|
const parseIntResult = parseInt(trimmedString, radix);
|
|
111
|
-
if (isNaN(parseIntResult)) throw
|
|
192
|
+
if (isNaN(parseIntResult)) throw new DataError_default(string, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
112
193
|
return parseIntResult;
|
|
113
194
|
}
|
|
114
195
|
var parseIntStrict_default = parseIntStrict;
|
|
@@ -539,19 +620,42 @@ var omitProperties_default = omitProperties;
|
|
|
539
620
|
*
|
|
540
621
|
* @param inputString - The string to parse.
|
|
541
622
|
*
|
|
542
|
-
* @throws {
|
|
623
|
+
* @throws {DataError} If the string is not either `true` or `false` (case insensitive).
|
|
543
624
|
*
|
|
544
625
|
* @returns The string parsed as an actual boolean.
|
|
545
626
|
*/
|
|
546
627
|
function parseBoolean(inputString) {
|
|
547
628
|
const normalisedString = inputString.toLowerCase();
|
|
548
|
-
if (!["true", "false"].includes(normalisedString)) throw new
|
|
629
|
+
if (!["true", "false"].includes(normalisedString)) throw new DataError_default(inputString, "INVALID_BOOLEAN_STRING", "The provided boolean string must be one of `true | false`");
|
|
549
630
|
return normalisedString === "true";
|
|
550
631
|
}
|
|
551
|
-
/** @deprecated This function has been renamed to parseBoolean. */
|
|
552
|
-
const stringToBoolean = parseBoolean;
|
|
553
632
|
var parseBoolean_default = parseBoolean;
|
|
554
633
|
|
|
634
|
+
//#endregion
|
|
635
|
+
//#region src/functions/parsers/parseZodSchema.ts
|
|
636
|
+
/**
|
|
637
|
+
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
638
|
+
*
|
|
639
|
+
* @template Output - The Zod output type.
|
|
640
|
+
* @template Input - The Zod input type.
|
|
641
|
+
* @template Internals - The Zod internal types based on the output and input types.
|
|
642
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
643
|
+
*
|
|
644
|
+
* @param schema - The Zod schema to use in parsing.
|
|
645
|
+
* @param data - The data to parse.
|
|
646
|
+
* @param error - A custom error to throw on invalid data (defaults to `DataError`).
|
|
647
|
+
*
|
|
648
|
+
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
649
|
+
*
|
|
650
|
+
* @returns The parsed data from the Zod schema.
|
|
651
|
+
*/
|
|
652
|
+
function parseZodSchema(schema, data, error) {
|
|
653
|
+
const parsedResult = schema.safeParse(data);
|
|
654
|
+
if (!parsedResult.success) throw error ?? new DataError_default(data);
|
|
655
|
+
return parsedResult.data;
|
|
656
|
+
}
|
|
657
|
+
var parseZodSchema_default = parseZodSchema;
|
|
658
|
+
|
|
555
659
|
//#endregion
|
|
556
660
|
//#region src/functions/parsers/parseEnv.ts
|
|
557
661
|
const envSchema = zod.z.enum([
|
|
@@ -564,12 +668,12 @@ const envSchema = zod.z.enum([
|
|
|
564
668
|
*
|
|
565
669
|
* @param data - The data to parse.
|
|
566
670
|
*
|
|
567
|
-
* @throws {
|
|
671
|
+
* @throws {DataError} If the data does not match one of the environments allowed by the Env types ("test" | "development" | "production").
|
|
568
672
|
*
|
|
569
673
|
* @returns The specified environment if allowed.
|
|
570
674
|
*/
|
|
571
675
|
function parseEnv(data) {
|
|
572
|
-
return envSchema
|
|
676
|
+
return parseZodSchema_default(envSchema, data, new DataError_default(data, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
573
677
|
}
|
|
574
678
|
var parseEnv_default = parseEnv;
|
|
575
679
|
|
|
@@ -595,122 +699,6 @@ function parseFormData(formData, dataParser) {
|
|
|
595
699
|
}
|
|
596
700
|
var parseFormData_default = parseFormData;
|
|
597
701
|
|
|
598
|
-
//#endregion
|
|
599
|
-
//#region src/types/APIError.ts
|
|
600
|
-
const httpErrorCodeLookup = {
|
|
601
|
-
400: "BAD_REQUEST",
|
|
602
|
-
401: "UNAUTHORISED",
|
|
603
|
-
403: "FORBIDDEN",
|
|
604
|
-
404: "NOT_FOUND",
|
|
605
|
-
418: "I_AM_A_TEAPOT",
|
|
606
|
-
500: "INTERNAL_SERVER_ERROR"
|
|
607
|
-
};
|
|
608
|
-
/** Represents common errors you may get from a HTTP API request. */
|
|
609
|
-
var APIError = class extends Error {
|
|
610
|
-
status;
|
|
611
|
-
/**
|
|
612
|
-
* @param status - A HTTP status code. Can be any number, but numbers between 400 and 600 are encouraged to fit with HTTP status code conventions.
|
|
613
|
-
* @param message - An error message to display alongside the status code.
|
|
614
|
-
* @param options - Extra options to be passed to super Error constructor.
|
|
615
|
-
*/
|
|
616
|
-
constructor(status = 500, message, options) {
|
|
617
|
-
super(message, options);
|
|
618
|
-
this.status = status;
|
|
619
|
-
if (message) this.message = message;
|
|
620
|
-
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
621
|
-
Object.defineProperty(this, "message", { enumerable: true });
|
|
622
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
623
|
-
}
|
|
624
|
-
/**
|
|
625
|
-
* Checks whether the given input may have been caused by an APIError.
|
|
626
|
-
*
|
|
627
|
-
* @param input - The input to check.
|
|
628
|
-
*
|
|
629
|
-
* @returns `true` if the input is an APIError, and `false` otherwise. The type of the input will also be narrowed down to APIError if `true`.
|
|
630
|
-
*/
|
|
631
|
-
static check(input) {
|
|
632
|
-
const data = input;
|
|
633
|
-
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
634
|
-
}
|
|
635
|
-
};
|
|
636
|
-
var APIError_default = APIError;
|
|
637
|
-
|
|
638
|
-
//#endregion
|
|
639
|
-
//#region src/types/DataError.ts
|
|
640
|
-
/** Represents errors you may get that may've been caused by a specific piece of data. */
|
|
641
|
-
var DataError = class extends Error {
|
|
642
|
-
data;
|
|
643
|
-
code;
|
|
644
|
-
/**
|
|
645
|
-
* @param data - The data that caused the error.
|
|
646
|
-
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
647
|
-
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
648
|
-
* @param options - Extra options to pass to super Error constructor.
|
|
649
|
-
*/
|
|
650
|
-
constructor(data, message = "The data provided is invalid", code = "INVALID_DATA", options) {
|
|
651
|
-
super(message, options);
|
|
652
|
-
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
653
|
-
this.name = new.target.name;
|
|
654
|
-
this.code = code;
|
|
655
|
-
this.data = data;
|
|
656
|
-
Object.defineProperty(this, "message", { enumerable: true });
|
|
657
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
658
|
-
}
|
|
659
|
-
/**
|
|
660
|
-
* Checks whether the given input may have been caused by a DataError.
|
|
661
|
-
*
|
|
662
|
-
* @param input - The input to check.
|
|
663
|
-
*
|
|
664
|
-
* @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`.
|
|
665
|
-
*/
|
|
666
|
-
static check(input) {
|
|
667
|
-
const data = input;
|
|
668
|
-
return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
|
|
669
|
-
}
|
|
670
|
-
};
|
|
671
|
-
var DataError_default = DataError;
|
|
672
|
-
|
|
673
|
-
//#endregion
|
|
674
|
-
//#region src/types/Email.ts
|
|
675
|
-
const emailSchema = zod.default.email().brand();
|
|
676
|
-
/** @deprecated Please use `z.email().parse() from Zod instead.`*/
|
|
677
|
-
function parseEmail(data) {
|
|
678
|
-
return emailSchema.parse(data);
|
|
679
|
-
}
|
|
680
|
-
var Email_default = parseEmail;
|
|
681
|
-
|
|
682
|
-
//#endregion
|
|
683
|
-
//#region src/types/UUID.ts
|
|
684
|
-
const uuidSchema = zod.default.uuid().brand();
|
|
685
|
-
/** @deprecated Please use `z.uuid().parse() from Zod instead.`*/
|
|
686
|
-
function parseUUID(UUID) {
|
|
687
|
-
return uuidSchema.parse(UUID);
|
|
688
|
-
}
|
|
689
|
-
var UUID_default = parseUUID;
|
|
690
|
-
|
|
691
|
-
//#endregion
|
|
692
|
-
//#region src/functions/parsers/parseZodSchema.ts
|
|
693
|
-
/**
|
|
694
|
-
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
695
|
-
*
|
|
696
|
-
* @template Output - The Zod output type.
|
|
697
|
-
* @template Input - The Zod input type.
|
|
698
|
-
* @template Internals - The Zod internal types based on the output and input types.
|
|
699
|
-
*
|
|
700
|
-
* @param schema - The Zod schema to use in parsing.
|
|
701
|
-
* @param data - The data to parse.
|
|
702
|
-
*
|
|
703
|
-
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
704
|
-
*
|
|
705
|
-
* @returns The parsed data from the Zod schema.
|
|
706
|
-
*/
|
|
707
|
-
function parseZodSchema(schema, data) {
|
|
708
|
-
const parsedResult = schema.safeParse(data);
|
|
709
|
-
if (!parsedResult.success) throw new DataError_default(data);
|
|
710
|
-
return parsedResult.data;
|
|
711
|
-
}
|
|
712
|
-
var parseZodSchema_default = parseZodSchema;
|
|
713
|
-
|
|
714
702
|
//#endregion
|
|
715
703
|
//#region src/functions/parsers/parseVersionType.ts
|
|
716
704
|
const versionTypeSchema = zod.default.enum([
|
|
@@ -728,7 +716,7 @@ const versionTypeSchema = zod.default.enum([
|
|
|
728
716
|
* @returns The given version type if allowed.
|
|
729
717
|
*/
|
|
730
718
|
function parseVersionType(data) {
|
|
731
|
-
return parseZodSchema_default(versionTypeSchema, data);
|
|
719
|
+
return parseZodSchema_default(versionTypeSchema, data, new DataError_default(data, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
732
720
|
}
|
|
733
721
|
var parseVersionType_default = parseVersionType;
|
|
734
722
|
|
|
@@ -967,25 +955,25 @@ var interpolateObjects_default = interpolateObjects;
|
|
|
967
955
|
|
|
968
956
|
//#endregion
|
|
969
957
|
//#region src/functions/taggedTemplate/normaliseIndents.ts
|
|
970
|
-
function calculateTabSize
|
|
958
|
+
function calculateTabSize(line, whitespaceLength) {
|
|
971
959
|
const potentialWhitespacePart = line.slice(0, whitespaceLength);
|
|
972
960
|
const trimmedString = line.trimStart();
|
|
973
961
|
if (potentialWhitespacePart.trim() !== "") return 0;
|
|
974
962
|
const tabSize = line.length - (trimmedString.length + whitespaceLength);
|
|
975
963
|
return tabSize < 0 ? 0 : tabSize;
|
|
976
964
|
}
|
|
977
|
-
function getWhitespaceLength
|
|
965
|
+
function getWhitespaceLength(lines) {
|
|
978
966
|
const [firstNonEmptyLine] = lines.filter((line) => {
|
|
979
967
|
return line.trim() !== "";
|
|
980
968
|
});
|
|
981
969
|
return firstNonEmptyLine.length - firstNonEmptyLine.trimStart().length;
|
|
982
970
|
}
|
|
983
|
-
function reduceLines
|
|
971
|
+
function reduceLines(lines, { preserveTabs = true }) {
|
|
984
972
|
const slicedLines = lines.slice(1);
|
|
985
973
|
const isFirstLineEmpty = lines[0].trim() === "";
|
|
986
|
-
const whitespaceLength = getWhitespaceLength
|
|
974
|
+
const whitespaceLength = getWhitespaceLength(isFirstLineEmpty ? lines : slicedLines);
|
|
987
975
|
return (isFirstLineEmpty ? slicedLines : lines).map((line) => {
|
|
988
|
-
const tabSize = calculateTabSize
|
|
976
|
+
const tabSize = calculateTabSize(line, whitespaceLength);
|
|
989
977
|
return (preserveTabs ? fillArray_default(() => {
|
|
990
978
|
return " ";
|
|
991
979
|
}, tabSize).join("") : "") + line.trimStart();
|
|
@@ -1020,7 +1008,7 @@ function normaliseIndents(first, ...args) {
|
|
|
1020
1008
|
}
|
|
1021
1009
|
const strings = first;
|
|
1022
1010
|
const options = typeof args[args.length - 1] === "object" && !Array.isArray(args[args.length - 1]) ? args.pop() : {};
|
|
1023
|
-
return reduceLines
|
|
1011
|
+
return reduceLines(interpolate_default(strings, ...[...args]).split("\n"), options);
|
|
1024
1012
|
}
|
|
1025
1013
|
/**
|
|
1026
1014
|
* Applies any options if provided, then removes any extraneous indents from a multi-line template string.
|
|
@@ -1045,45 +1033,6 @@ function normaliseIndents(first, ...args) {
|
|
|
1045
1033
|
const normalizeIndents = normaliseIndents;
|
|
1046
1034
|
var normaliseIndents_default = normaliseIndents;
|
|
1047
1035
|
|
|
1048
|
-
//#endregion
|
|
1049
|
-
//#region src/functions/taggedTemplate/removeIndents.ts
|
|
1050
|
-
function calculateTabSize(line, whitespaceLength) {
|
|
1051
|
-
const potentialWhitespacePart = line.slice(0, whitespaceLength);
|
|
1052
|
-
const trimmedString = line.trimStart();
|
|
1053
|
-
if (potentialWhitespacePart.trim() !== "") return 0;
|
|
1054
|
-
const tabSize = line.length - (trimmedString.length + whitespaceLength);
|
|
1055
|
-
return tabSize < 0 ? 0 : tabSize;
|
|
1056
|
-
}
|
|
1057
|
-
function getWhitespaceLength(lines) {
|
|
1058
|
-
const [firstNonEmptyLine] = lines.filter((line) => {
|
|
1059
|
-
return line.trim() !== "";
|
|
1060
|
-
});
|
|
1061
|
-
return firstNonEmptyLine.length - firstNonEmptyLine.trimStart().length;
|
|
1062
|
-
}
|
|
1063
|
-
function reduceLines(lines, { preserveTabs = true }) {
|
|
1064
|
-
const slicedLines = lines.slice(1);
|
|
1065
|
-
const isFirstLineEmpty = lines[0].trim() === "";
|
|
1066
|
-
const whitespaceLength = getWhitespaceLength(isFirstLineEmpty ? lines : slicedLines);
|
|
1067
|
-
return (isFirstLineEmpty ? slicedLines : lines).map((line) => {
|
|
1068
|
-
const tabSize = calculateTabSize(line, whitespaceLength);
|
|
1069
|
-
return (preserveTabs ? fillArray_default(() => {
|
|
1070
|
-
return " ";
|
|
1071
|
-
}, tabSize).join("") : "") + line.trimStart();
|
|
1072
|
-
}).join("\n");
|
|
1073
|
-
}
|
|
1074
|
-
function removeIndents(first, ...args) {
|
|
1075
|
-
if (typeof first === "object" && first !== null && !Array.isArray(first)) {
|
|
1076
|
-
const options$1 = first;
|
|
1077
|
-
return (strings$1, ...interpolations) => {
|
|
1078
|
-
return removeIndents(strings$1, ...interpolations, options$1);
|
|
1079
|
-
};
|
|
1080
|
-
}
|
|
1081
|
-
const strings = first;
|
|
1082
|
-
const options = typeof args[args.length - 1] === "object" && !Array.isArray(args[args.length - 1]) ? args.pop() : {};
|
|
1083
|
-
return reduceLines(interpolate_default(strings, ...[...args]).split("\n"), options);
|
|
1084
|
-
}
|
|
1085
|
-
var removeIndents_default = removeIndents;
|
|
1086
|
-
|
|
1087
1036
|
//#endregion
|
|
1088
1037
|
//#region src/functions/versioning/parseVersion.ts
|
|
1089
1038
|
/**
|
|
@@ -1099,7 +1048,7 @@ var removeIndents_default = removeIndents;
|
|
|
1099
1048
|
* @returns The validated version number, prefixed with `v` if it was not already.
|
|
1100
1049
|
*/
|
|
1101
1050
|
function parseVersion(input, options) {
|
|
1102
|
-
if (!RegExp(VERSION_NUMBER_REGEX_default).test(input)) throw new DataError_default(input, `"${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
|
|
1051
|
+
if (!RegExp(VERSION_NUMBER_REGEX_default).test(input)) throw new DataError_default(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.`);
|
|
1103
1052
|
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
1104
1053
|
return input.startsWith("v") ? input : `v${input}`;
|
|
1105
1054
|
}
|
|
@@ -1199,19 +1148,15 @@ exports.normalizeIndents = normalizeIndents;
|
|
|
1199
1148
|
exports.omitProperties = omitProperties_default;
|
|
1200
1149
|
exports.paralleliseArrays = paralleliseArrays_default;
|
|
1201
1150
|
exports.parseBoolean = parseBoolean_default;
|
|
1202
|
-
exports.parseEmail = Email_default;
|
|
1203
1151
|
exports.parseEnv = parseEnv_default;
|
|
1204
1152
|
exports.parseFormData = parseFormData_default;
|
|
1205
1153
|
exports.parseIntStrict = parseIntStrict_default;
|
|
1206
|
-
exports.parseUUID = UUID_default;
|
|
1207
1154
|
exports.parseVersion = parseVersion_default;
|
|
1208
1155
|
exports.parseVersionType = parseVersionType_default;
|
|
1209
1156
|
exports.parseZodSchema = parseZodSchema_default;
|
|
1210
1157
|
exports.randomiseArray = randomiseArray_default;
|
|
1211
1158
|
exports.range = range_default;
|
|
1212
1159
|
exports.removeDuplicates = removeDuplicates_default;
|
|
1213
|
-
exports.removeIndents = removeIndents_default;
|
|
1214
1160
|
exports.stringListToArray = stringListToArray_default;
|
|
1215
|
-
exports.stringToBoolean = stringToBoolean;
|
|
1216
1161
|
exports.truncate = truncate_default;
|
|
1217
1162
|
exports.wait = wait_default;
|
package/dist/index.d.cts
CHANGED
|
@@ -20,7 +20,7 @@ declare const VERSION_NUMBER_REGEX: string;
|
|
|
20
20
|
*
|
|
21
21
|
* @returns A Promise resolving to an array of the callback results.
|
|
22
22
|
*/
|
|
23
|
-
declare function fillArray<ItemType
|
|
23
|
+
declare function fillArray<ItemType>(callback: (index: number) => Promise<ItemType>, length?: number): Promise<ItemType[]>;
|
|
24
24
|
/**
|
|
25
25
|
* Creates a new array where each element is the result of the provided synchronous callback.
|
|
26
26
|
*
|
|
@@ -34,7 +34,7 @@ declare function fillArray<ItemType$1>(callback: (index: number) => Promise<Item
|
|
|
34
34
|
*
|
|
35
35
|
* @returns An array of the callback results.
|
|
36
36
|
*/
|
|
37
|
-
declare function fillArray<ItemType
|
|
37
|
+
declare function fillArray<ItemType>(callback: (index: number) => ItemType, length?: number): ItemType[];
|
|
38
38
|
//#endregion
|
|
39
39
|
//#region src/functions/arrayHelpers/paralleliseArrays.d.ts
|
|
40
40
|
type ParallelTuple<A, B> = [A, B | undefined];
|
|
@@ -64,7 +64,7 @@ declare function paralleliseArrays<FirstArrayItem, SecondArrayItem>(firstArray:
|
|
|
64
64
|
*
|
|
65
65
|
* @returns A new array with the items randomised.
|
|
66
66
|
*/
|
|
67
|
-
declare function randomiseArray<ItemType
|
|
67
|
+
declare function randomiseArray<ItemType>(array: ItemType[]): ItemType[];
|
|
68
68
|
//#endregion
|
|
69
69
|
//#region src/functions/arrayHelpers/range.d.ts
|
|
70
70
|
/**
|
|
@@ -94,7 +94,7 @@ declare function range(start: number, stop: number, step?: number): number[];
|
|
|
94
94
|
*
|
|
95
95
|
* @returns A new array with a different reference in memory, with the duplicates removed.
|
|
96
96
|
*/
|
|
97
|
-
declare function removeDuplicates<ItemType
|
|
97
|
+
declare function removeDuplicates<ItemType>(array: ItemType[] | readonly ItemType[]): ItemType[];
|
|
98
98
|
//#endregion
|
|
99
99
|
//#region src/functions/date/addDaysToDate.d.ts
|
|
100
100
|
/**
|
|
@@ -207,11 +207,11 @@ declare class DataError extends Error {
|
|
|
207
207
|
code: string;
|
|
208
208
|
/**
|
|
209
209
|
* @param data - The data that caused the error.
|
|
210
|
-
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
211
210
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
211
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
212
212
|
* @param options - Extra options to pass to super Error constructor.
|
|
213
213
|
*/
|
|
214
|
-
constructor(data: unknown,
|
|
214
|
+
constructor(data: unknown, code?: string, message?: string, options?: ErrorOptions);
|
|
215
215
|
/**
|
|
216
216
|
* Checks whether the given input may have been caused by a DataError.
|
|
217
217
|
*
|
|
@@ -222,20 +222,6 @@ declare class DataError extends Error {
|
|
|
222
222
|
static check(input: unknown): input is DataError;
|
|
223
223
|
}
|
|
224
224
|
//#endregion
|
|
225
|
-
//#region src/types/Email.d.ts
|
|
226
|
-
declare const emailSchema: z.core.$ZodBranded<z.ZodEmail, "Email">;
|
|
227
|
-
/** @deprecated Please use the inferred type from `z.email() from Zod instead.` */
|
|
228
|
-
type Email = z.infer<typeof emailSchema>;
|
|
229
|
-
/** @deprecated Please use `z.email().parse() from Zod instead.`*/
|
|
230
|
-
declare function parseEmail(data: unknown): Email;
|
|
231
|
-
//#endregion
|
|
232
|
-
//#region src/types/UUID.d.ts
|
|
233
|
-
declare const uuidSchema: z.core.$ZodBranded<z.ZodUUID, "UUID">;
|
|
234
|
-
/** @deprecated Please use the inferred type from `z.uuid() from Zod instead.` */
|
|
235
|
-
type UUID = z.infer<typeof uuidSchema>;
|
|
236
|
-
/** @deprecated Please use `z.uuid().parse() from Zod instead.`*/
|
|
237
|
-
declare function parseUUID(UUID: unknown): UUID;
|
|
238
|
-
//#endregion
|
|
239
225
|
//#region src/types/ArrayElement.d.ts
|
|
240
226
|
/**
|
|
241
227
|
* Gets the individual element types from an array type.
|
|
@@ -244,10 +230,6 @@ declare function parseUUID(UUID: unknown): UUID;
|
|
|
244
230
|
*/
|
|
245
231
|
type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
|
|
246
232
|
//#endregion
|
|
247
|
-
//#region src/types/DeepReadonly.d.ts
|
|
248
|
-
/** @deprecated This type did not work with the deepFreeze function as intended and therefore should not be used. */
|
|
249
|
-
type DeepReadonly<T> = T extends ((...args: unknown[]) => unknown) ? T : T extends (infer ItemType)[] ? readonly DeepReadonly<ItemType>[] : T extends object ? { readonly [K in keyof T]: DeepReadonly<T[K]> } : T;
|
|
250
|
-
//#endregion
|
|
251
233
|
//#region src/types/DisallowUndefined.d.ts
|
|
252
234
|
/**
|
|
253
235
|
* Resolves to an error message type if the type argument could potentially be undefined.
|
|
@@ -406,13 +388,11 @@ declare function omitProperties<ObjectType extends Record<string, unknown> | Rea
|
|
|
406
388
|
*
|
|
407
389
|
* @param inputString - The string to parse.
|
|
408
390
|
*
|
|
409
|
-
* @throws {
|
|
391
|
+
* @throws {DataError} If the string is not either `true` or `false` (case insensitive).
|
|
410
392
|
*
|
|
411
393
|
* @returns The string parsed as an actual boolean.
|
|
412
394
|
*/
|
|
413
395
|
declare function parseBoolean(inputString: string): boolean;
|
|
414
|
-
/** @deprecated This function has been renamed to parseBoolean. */
|
|
415
|
-
declare const stringToBoolean: typeof parseBoolean;
|
|
416
396
|
//#endregion
|
|
417
397
|
//#region src/functions/parsers/parseEnv.d.ts
|
|
418
398
|
declare const envSchema: z$1.ZodEnum<{
|
|
@@ -427,7 +407,7 @@ type Env = z$1.infer<typeof envSchema>;
|
|
|
427
407
|
*
|
|
428
408
|
* @param data - The data to parse.
|
|
429
409
|
*
|
|
430
|
-
* @throws {
|
|
410
|
+
* @throws {DataError} If the data does not match one of the environments allowed by the Env types ("test" | "development" | "production").
|
|
431
411
|
*
|
|
432
412
|
* @returns The specified environment if allowed.
|
|
433
413
|
*/
|
|
@@ -461,7 +441,7 @@ declare function parseFormData(formData: FormData): Record<string, string | Blob
|
|
|
461
441
|
* @param string - A string to convert into a number.
|
|
462
442
|
* @param radix - A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.
|
|
463
443
|
*
|
|
464
|
-
* @throws {
|
|
444
|
+
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
465
445
|
*
|
|
466
446
|
* @returns The integer parsed from the input string.
|
|
467
447
|
*/
|
|
@@ -492,15 +472,17 @@ declare function parseVersionType(data: unknown): VersionType;
|
|
|
492
472
|
* @template Output - The Zod output type.
|
|
493
473
|
* @template Input - The Zod input type.
|
|
494
474
|
* @template Internals - The Zod internal types based on the output and input types.
|
|
475
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
495
476
|
*
|
|
496
477
|
* @param schema - The Zod schema to use in parsing.
|
|
497
478
|
* @param data - The data to parse.
|
|
479
|
+
* @param error - A custom error to throw on invalid data (defaults to `DataError`).
|
|
498
480
|
*
|
|
499
481
|
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
500
482
|
*
|
|
501
483
|
* @returns The parsed data from the Zod schema.
|
|
502
484
|
*/
|
|
503
|
-
declare function parseZodSchema<Output, Input, Internals extends core.$ZodTypeInternals<Output, Input
|
|
485
|
+
declare function parseZodSchema<Output, Input, Internals extends core.$ZodTypeInternals<Output, Input>, ErrorType extends Error>(schema: ZodType<Output, Input, Internals>, data: unknown, error?: ErrorType): core.output<ZodType<Output, Input, Internals>>;
|
|
504
486
|
//#endregion
|
|
505
487
|
//#region src/functions/recursive/deepCopy.d.ts
|
|
506
488
|
/**
|
|
@@ -702,23 +684,6 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
702
684
|
*/
|
|
703
685
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
704
686
|
//#endregion
|
|
705
|
-
//#region src/functions/taggedTemplate/removeIndents.d.ts
|
|
706
|
-
interface RemoveIndentsOptions {
|
|
707
|
-
preserveTabs?: boolean;
|
|
708
|
-
}
|
|
709
|
-
type RemoveIndentsFunction = (strings: TemplateStringsArray, ...interpolations: unknown[]) => string;
|
|
710
|
-
/**
|
|
711
|
-
* @param options
|
|
712
|
-
* @deprecated This function has been renamed to normaliseIndents
|
|
713
|
-
*/
|
|
714
|
-
declare function removeIndents(options: RemoveIndentsOptions): RemoveIndentsFunction;
|
|
715
|
-
/**
|
|
716
|
-
* @param strings
|
|
717
|
-
* @param interpolations
|
|
718
|
-
* @deprecated This function has been renamed to normaliseIndents
|
|
719
|
-
*/
|
|
720
|
-
declare function removeIndents(strings: TemplateStringsArray, ...interpolations: unknown[]): string;
|
|
721
|
-
//#endregion
|
|
722
687
|
//#region src/functions/versioning/determineVersionType.d.ts
|
|
723
688
|
/**
|
|
724
689
|
* Determines whether the given version is a major, minor, or patch version.
|
|
@@ -777,4 +742,4 @@ interface ParseVersionOptions {
|
|
|
777
742
|
*/
|
|
778
743
|
declare function parseVersion(input: string, options?: ParseVersionOptions): string;
|
|
779
744
|
//#endregion
|
|
780
|
-
export { APIError, ArrayElement, CreateFormDataOptions, CreateFormDataOptionsNullableResolution, CreateFormDataOptionsUndefinedOrNullResolution, DataError,
|
|
745
|
+
export { APIError, ArrayElement, CreateFormDataOptions, CreateFormDataOptionsNullableResolution, CreateFormDataOptionsUndefinedOrNullResolution, DataError, DisallowUndefined, Env, FormDataArrayResolutionStrategy, FormDataNullableResolutionStrategy, HTTPErrorCode, IgnoreCase, IncrementVersionOptions, KebabToCamelOptions, NAMESPACE_EXPORT_REGEX, NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, OptionalOnCondition, ParseVersionOptions, RecordKey, StringListToArrayOptions, VERSION_NUMBER_REGEX, VersionType, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, determineVersionType, fillArray, formatDateAndTime, getIndividualVersionNumbers, getRandomNumber, getRecordKeys, httpErrorCodeLookup, incrementVersion, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, kebabToCamel, normaliseImportPath, normaliseIndents, normalizeImportPath, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseVersion, parseVersionType, parseZodSchema, randomiseArray, range, removeDuplicates, stringListToArray, truncate, wait };
|
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ declare const VERSION_NUMBER_REGEX: string;
|
|
|
20
20
|
*
|
|
21
21
|
* @returns A Promise resolving to an array of the callback results.
|
|
22
22
|
*/
|
|
23
|
-
declare function fillArray<ItemType
|
|
23
|
+
declare function fillArray<ItemType>(callback: (index: number) => Promise<ItemType>, length?: number): Promise<ItemType[]>;
|
|
24
24
|
/**
|
|
25
25
|
* Creates a new array where each element is the result of the provided synchronous callback.
|
|
26
26
|
*
|
|
@@ -34,7 +34,7 @@ declare function fillArray<ItemType$1>(callback: (index: number) => Promise<Item
|
|
|
34
34
|
*
|
|
35
35
|
* @returns An array of the callback results.
|
|
36
36
|
*/
|
|
37
|
-
declare function fillArray<ItemType
|
|
37
|
+
declare function fillArray<ItemType>(callback: (index: number) => ItemType, length?: number): ItemType[];
|
|
38
38
|
//#endregion
|
|
39
39
|
//#region src/functions/arrayHelpers/paralleliseArrays.d.ts
|
|
40
40
|
type ParallelTuple<A, B> = [A, B | undefined];
|
|
@@ -64,7 +64,7 @@ declare function paralleliseArrays<FirstArrayItem, SecondArrayItem>(firstArray:
|
|
|
64
64
|
*
|
|
65
65
|
* @returns A new array with the items randomised.
|
|
66
66
|
*/
|
|
67
|
-
declare function randomiseArray<ItemType
|
|
67
|
+
declare function randomiseArray<ItemType>(array: ItemType[]): ItemType[];
|
|
68
68
|
//#endregion
|
|
69
69
|
//#region src/functions/arrayHelpers/range.d.ts
|
|
70
70
|
/**
|
|
@@ -94,7 +94,7 @@ declare function range(start: number, stop: number, step?: number): number[];
|
|
|
94
94
|
*
|
|
95
95
|
* @returns A new array with a different reference in memory, with the duplicates removed.
|
|
96
96
|
*/
|
|
97
|
-
declare function removeDuplicates<ItemType
|
|
97
|
+
declare function removeDuplicates<ItemType>(array: ItemType[] | readonly ItemType[]): ItemType[];
|
|
98
98
|
//#endregion
|
|
99
99
|
//#region src/functions/date/addDaysToDate.d.ts
|
|
100
100
|
/**
|
|
@@ -207,11 +207,11 @@ declare class DataError extends Error {
|
|
|
207
207
|
code: string;
|
|
208
208
|
/**
|
|
209
209
|
* @param data - The data that caused the error.
|
|
210
|
-
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
211
210
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
211
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
212
212
|
* @param options - Extra options to pass to super Error constructor.
|
|
213
213
|
*/
|
|
214
|
-
constructor(data: unknown,
|
|
214
|
+
constructor(data: unknown, code?: string, message?: string, options?: ErrorOptions);
|
|
215
215
|
/**
|
|
216
216
|
* Checks whether the given input may have been caused by a DataError.
|
|
217
217
|
*
|
|
@@ -222,20 +222,6 @@ declare class DataError extends Error {
|
|
|
222
222
|
static check(input: unknown): input is DataError;
|
|
223
223
|
}
|
|
224
224
|
//#endregion
|
|
225
|
-
//#region src/types/Email.d.ts
|
|
226
|
-
declare const emailSchema: z.core.$ZodBranded<z.ZodEmail, "Email">;
|
|
227
|
-
/** @deprecated Please use the inferred type from `z.email() from Zod instead.` */
|
|
228
|
-
type Email = z.infer<typeof emailSchema>;
|
|
229
|
-
/** @deprecated Please use `z.email().parse() from Zod instead.`*/
|
|
230
|
-
declare function parseEmail(data: unknown): Email;
|
|
231
|
-
//#endregion
|
|
232
|
-
//#region src/types/UUID.d.ts
|
|
233
|
-
declare const uuidSchema: z.core.$ZodBranded<z.ZodUUID, "UUID">;
|
|
234
|
-
/** @deprecated Please use the inferred type from `z.uuid() from Zod instead.` */
|
|
235
|
-
type UUID = z.infer<typeof uuidSchema>;
|
|
236
|
-
/** @deprecated Please use `z.uuid().parse() from Zod instead.`*/
|
|
237
|
-
declare function parseUUID(UUID: unknown): UUID;
|
|
238
|
-
//#endregion
|
|
239
225
|
//#region src/types/ArrayElement.d.ts
|
|
240
226
|
/**
|
|
241
227
|
* Gets the individual element types from an array type.
|
|
@@ -244,10 +230,6 @@ declare function parseUUID(UUID: unknown): UUID;
|
|
|
244
230
|
*/
|
|
245
231
|
type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
|
|
246
232
|
//#endregion
|
|
247
|
-
//#region src/types/DeepReadonly.d.ts
|
|
248
|
-
/** @deprecated This type did not work with the deepFreeze function as intended and therefore should not be used. */
|
|
249
|
-
type DeepReadonly<T> = T extends ((...args: unknown[]) => unknown) ? T : T extends (infer ItemType)[] ? readonly DeepReadonly<ItemType>[] : T extends object ? { readonly [K in keyof T]: DeepReadonly<T[K]> } : T;
|
|
250
|
-
//#endregion
|
|
251
233
|
//#region src/types/DisallowUndefined.d.ts
|
|
252
234
|
/**
|
|
253
235
|
* Resolves to an error message type if the type argument could potentially be undefined.
|
|
@@ -406,13 +388,11 @@ declare function omitProperties<ObjectType extends Record<string, unknown> | Rea
|
|
|
406
388
|
*
|
|
407
389
|
* @param inputString - The string to parse.
|
|
408
390
|
*
|
|
409
|
-
* @throws {
|
|
391
|
+
* @throws {DataError} If the string is not either `true` or `false` (case insensitive).
|
|
410
392
|
*
|
|
411
393
|
* @returns The string parsed as an actual boolean.
|
|
412
394
|
*/
|
|
413
395
|
declare function parseBoolean(inputString: string): boolean;
|
|
414
|
-
/** @deprecated This function has been renamed to parseBoolean. */
|
|
415
|
-
declare const stringToBoolean: typeof parseBoolean;
|
|
416
396
|
//#endregion
|
|
417
397
|
//#region src/functions/parsers/parseEnv.d.ts
|
|
418
398
|
declare const envSchema: z$1.ZodEnum<{
|
|
@@ -427,7 +407,7 @@ type Env = z$1.infer<typeof envSchema>;
|
|
|
427
407
|
*
|
|
428
408
|
* @param data - The data to parse.
|
|
429
409
|
*
|
|
430
|
-
* @throws {
|
|
410
|
+
* @throws {DataError} If the data does not match one of the environments allowed by the Env types ("test" | "development" | "production").
|
|
431
411
|
*
|
|
432
412
|
* @returns The specified environment if allowed.
|
|
433
413
|
*/
|
|
@@ -461,7 +441,7 @@ declare function parseFormData(formData: FormData): Record<string, string | Blob
|
|
|
461
441
|
* @param string - A string to convert into a number.
|
|
462
442
|
* @param radix - A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.
|
|
463
443
|
*
|
|
464
|
-
* @throws {
|
|
444
|
+
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
465
445
|
*
|
|
466
446
|
* @returns The integer parsed from the input string.
|
|
467
447
|
*/
|
|
@@ -492,15 +472,17 @@ declare function parseVersionType(data: unknown): VersionType;
|
|
|
492
472
|
* @template Output - The Zod output type.
|
|
493
473
|
* @template Input - The Zod input type.
|
|
494
474
|
* @template Internals - The Zod internal types based on the output and input types.
|
|
475
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
495
476
|
*
|
|
496
477
|
* @param schema - The Zod schema to use in parsing.
|
|
497
478
|
* @param data - The data to parse.
|
|
479
|
+
* @param error - A custom error to throw on invalid data (defaults to `DataError`).
|
|
498
480
|
*
|
|
499
481
|
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
500
482
|
*
|
|
501
483
|
* @returns The parsed data from the Zod schema.
|
|
502
484
|
*/
|
|
503
|
-
declare function parseZodSchema<Output, Input, Internals extends core.$ZodTypeInternals<Output, Input
|
|
485
|
+
declare function parseZodSchema<Output, Input, Internals extends core.$ZodTypeInternals<Output, Input>, ErrorType extends Error>(schema: ZodType<Output, Input, Internals>, data: unknown, error?: ErrorType): core.output<ZodType<Output, Input, Internals>>;
|
|
504
486
|
//#endregion
|
|
505
487
|
//#region src/functions/recursive/deepCopy.d.ts
|
|
506
488
|
/**
|
|
@@ -702,23 +684,6 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
702
684
|
*/
|
|
703
685
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
704
686
|
//#endregion
|
|
705
|
-
//#region src/functions/taggedTemplate/removeIndents.d.ts
|
|
706
|
-
interface RemoveIndentsOptions {
|
|
707
|
-
preserveTabs?: boolean;
|
|
708
|
-
}
|
|
709
|
-
type RemoveIndentsFunction = (strings: TemplateStringsArray, ...interpolations: unknown[]) => string;
|
|
710
|
-
/**
|
|
711
|
-
* @param options
|
|
712
|
-
* @deprecated This function has been renamed to normaliseIndents
|
|
713
|
-
*/
|
|
714
|
-
declare function removeIndents(options: RemoveIndentsOptions): RemoveIndentsFunction;
|
|
715
|
-
/**
|
|
716
|
-
* @param strings
|
|
717
|
-
* @param interpolations
|
|
718
|
-
* @deprecated This function has been renamed to normaliseIndents
|
|
719
|
-
*/
|
|
720
|
-
declare function removeIndents(strings: TemplateStringsArray, ...interpolations: unknown[]): string;
|
|
721
|
-
//#endregion
|
|
722
687
|
//#region src/functions/versioning/determineVersionType.d.ts
|
|
723
688
|
/**
|
|
724
689
|
* Determines whether the given version is a major, minor, or patch version.
|
|
@@ -777,4 +742,4 @@ interface ParseVersionOptions {
|
|
|
777
742
|
*/
|
|
778
743
|
declare function parseVersion(input: string, options?: ParseVersionOptions): string;
|
|
779
744
|
//#endregion
|
|
780
|
-
export { APIError,
|
|
745
|
+
export { APIError, ArrayElement, CreateFormDataOptions, CreateFormDataOptionsNullableResolution, CreateFormDataOptionsUndefinedOrNullResolution, DataError, DisallowUndefined, type Env, FormDataArrayResolutionStrategy, FormDataNullableResolutionStrategy, HTTPErrorCode, IgnoreCase, type IncrementVersionOptions, KebabToCamelOptions, NAMESPACE_EXPORT_REGEX, NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, OptionalOnCondition, type ParseVersionOptions, RecordKey, StringListToArrayOptions, VERSION_NUMBER_REGEX, type VersionType, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, determineVersionType, fillArray, formatDateAndTime, getIndividualVersionNumbers, getRandomNumber, getRecordKeys, httpErrorCodeLookup, incrementVersion, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, kebabToCamel, normaliseImportPath, normaliseIndents, normalizeImportPath, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseVersion, parseVersionType, parseZodSchema, randomiseArray, range, removeDuplicates, stringListToArray, truncate, wait };
|
package/dist/index.js
CHANGED
|
@@ -59,27 +59,108 @@ function paralleliseArrays(firstArray, secondArray) {
|
|
|
59
59
|
}
|
|
60
60
|
var paralleliseArrays_default = paralleliseArrays;
|
|
61
61
|
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/types/APIError.ts
|
|
64
|
+
const httpErrorCodeLookup = {
|
|
65
|
+
400: "BAD_REQUEST",
|
|
66
|
+
401: "UNAUTHORISED",
|
|
67
|
+
403: "FORBIDDEN",
|
|
68
|
+
404: "NOT_FOUND",
|
|
69
|
+
418: "I_AM_A_TEAPOT",
|
|
70
|
+
500: "INTERNAL_SERVER_ERROR"
|
|
71
|
+
};
|
|
72
|
+
/** Represents common errors you may get from a HTTP API request. */
|
|
73
|
+
var APIError = class extends Error {
|
|
74
|
+
status;
|
|
75
|
+
/**
|
|
76
|
+
* @param status - A HTTP status code. Can be any number, but numbers between 400 and 600 are encouraged to fit with HTTP status code conventions.
|
|
77
|
+
* @param message - An error message to display alongside the status code.
|
|
78
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
79
|
+
*/
|
|
80
|
+
constructor(status = 500, message, options) {
|
|
81
|
+
super(message, options);
|
|
82
|
+
this.status = status;
|
|
83
|
+
if (message) this.message = message;
|
|
84
|
+
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
85
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
86
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Checks whether the given input may have been caused by an APIError.
|
|
90
|
+
*
|
|
91
|
+
* @param input - The input to check.
|
|
92
|
+
*
|
|
93
|
+
* @returns `true` if the input is an APIError, and `false` otherwise. The type of the input will also be narrowed down to APIError if `true`.
|
|
94
|
+
*/
|
|
95
|
+
static check(input) {
|
|
96
|
+
const data = input;
|
|
97
|
+
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var APIError_default = APIError;
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/types/DataError.ts
|
|
104
|
+
/** Represents errors you may get that may've been caused by a specific piece of data. */
|
|
105
|
+
var DataError = class extends Error {
|
|
106
|
+
data;
|
|
107
|
+
code;
|
|
108
|
+
/**
|
|
109
|
+
* @param data - The data that caused the error.
|
|
110
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
111
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
112
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
113
|
+
*/
|
|
114
|
+
constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
|
|
115
|
+
super(message, options);
|
|
116
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
117
|
+
this.name = new.target.name;
|
|
118
|
+
this.code = code;
|
|
119
|
+
this.data = data;
|
|
120
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
121
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
125
|
+
*
|
|
126
|
+
* @param input - The input to check.
|
|
127
|
+
*
|
|
128
|
+
* @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`.
|
|
129
|
+
*/
|
|
130
|
+
static check(input) {
|
|
131
|
+
const data = input;
|
|
132
|
+
return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var DataError_default = DataError;
|
|
136
|
+
|
|
62
137
|
//#endregion
|
|
63
138
|
//#region src/functions/parsers/parseIntStrict.ts
|
|
64
|
-
const IntegerParsingError = /* @__PURE__ */ new TypeError("INTEGER_PARSING_ERROR");
|
|
65
139
|
/**
|
|
66
140
|
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
67
141
|
*
|
|
68
142
|
* @param string - A string to convert into a number.
|
|
69
143
|
* @param radix - A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.
|
|
70
144
|
*
|
|
71
|
-
* @throws {
|
|
145
|
+
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
72
146
|
*
|
|
73
147
|
* @returns The integer parsed from the input string.
|
|
74
148
|
*/
|
|
75
149
|
function parseIntStrict(string, radix) {
|
|
76
150
|
const trimmedString = string.trim();
|
|
77
|
-
|
|
151
|
+
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
152
|
+
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError_default(radix ? {
|
|
153
|
+
string,
|
|
154
|
+
radix
|
|
155
|
+
} : string, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
78
156
|
if (radix && radix < 10 && [...trimmedString].some((character) => {
|
|
79
157
|
return parseInt(character) >= radix;
|
|
80
|
-
})) throw
|
|
158
|
+
})) throw new DataError_default({
|
|
159
|
+
string,
|
|
160
|
+
radix
|
|
161
|
+
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
81
162
|
const parseIntResult = parseInt(trimmedString, radix);
|
|
82
|
-
if (isNaN(parseIntResult)) throw
|
|
163
|
+
if (isNaN(parseIntResult)) throw new DataError_default(string, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
83
164
|
return parseIntResult;
|
|
84
165
|
}
|
|
85
166
|
var parseIntStrict_default = parseIntStrict;
|
|
@@ -510,19 +591,42 @@ var omitProperties_default = omitProperties;
|
|
|
510
591
|
*
|
|
511
592
|
* @param inputString - The string to parse.
|
|
512
593
|
*
|
|
513
|
-
* @throws {
|
|
594
|
+
* @throws {DataError} If the string is not either `true` or `false` (case insensitive).
|
|
514
595
|
*
|
|
515
596
|
* @returns The string parsed as an actual boolean.
|
|
516
597
|
*/
|
|
517
598
|
function parseBoolean(inputString) {
|
|
518
599
|
const normalisedString = inputString.toLowerCase();
|
|
519
|
-
if (!["true", "false"].includes(normalisedString)) throw new
|
|
600
|
+
if (!["true", "false"].includes(normalisedString)) throw new DataError_default(inputString, "INVALID_BOOLEAN_STRING", "The provided boolean string must be one of `true | false`");
|
|
520
601
|
return normalisedString === "true";
|
|
521
602
|
}
|
|
522
|
-
/** @deprecated This function has been renamed to parseBoolean. */
|
|
523
|
-
const stringToBoolean = parseBoolean;
|
|
524
603
|
var parseBoolean_default = parseBoolean;
|
|
525
604
|
|
|
605
|
+
//#endregion
|
|
606
|
+
//#region src/functions/parsers/parseZodSchema.ts
|
|
607
|
+
/**
|
|
608
|
+
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
609
|
+
*
|
|
610
|
+
* @template Output - The Zod output type.
|
|
611
|
+
* @template Input - The Zod input type.
|
|
612
|
+
* @template Internals - The Zod internal types based on the output and input types.
|
|
613
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
614
|
+
*
|
|
615
|
+
* @param schema - The Zod schema to use in parsing.
|
|
616
|
+
* @param data - The data to parse.
|
|
617
|
+
* @param error - A custom error to throw on invalid data (defaults to `DataError`).
|
|
618
|
+
*
|
|
619
|
+
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
620
|
+
*
|
|
621
|
+
* @returns The parsed data from the Zod schema.
|
|
622
|
+
*/
|
|
623
|
+
function parseZodSchema(schema, data, error) {
|
|
624
|
+
const parsedResult = schema.safeParse(data);
|
|
625
|
+
if (!parsedResult.success) throw error ?? new DataError_default(data);
|
|
626
|
+
return parsedResult.data;
|
|
627
|
+
}
|
|
628
|
+
var parseZodSchema_default = parseZodSchema;
|
|
629
|
+
|
|
526
630
|
//#endregion
|
|
527
631
|
//#region src/functions/parsers/parseEnv.ts
|
|
528
632
|
const envSchema = z$1.enum([
|
|
@@ -535,12 +639,12 @@ const envSchema = z$1.enum([
|
|
|
535
639
|
*
|
|
536
640
|
* @param data - The data to parse.
|
|
537
641
|
*
|
|
538
|
-
* @throws {
|
|
642
|
+
* @throws {DataError} If the data does not match one of the environments allowed by the Env types ("test" | "development" | "production").
|
|
539
643
|
*
|
|
540
644
|
* @returns The specified environment if allowed.
|
|
541
645
|
*/
|
|
542
646
|
function parseEnv(data) {
|
|
543
|
-
return envSchema
|
|
647
|
+
return parseZodSchema_default(envSchema, data, new DataError_default(data, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
544
648
|
}
|
|
545
649
|
var parseEnv_default = parseEnv;
|
|
546
650
|
|
|
@@ -566,122 +670,6 @@ function parseFormData(formData, dataParser) {
|
|
|
566
670
|
}
|
|
567
671
|
var parseFormData_default = parseFormData;
|
|
568
672
|
|
|
569
|
-
//#endregion
|
|
570
|
-
//#region src/types/APIError.ts
|
|
571
|
-
const httpErrorCodeLookup = {
|
|
572
|
-
400: "BAD_REQUEST",
|
|
573
|
-
401: "UNAUTHORISED",
|
|
574
|
-
403: "FORBIDDEN",
|
|
575
|
-
404: "NOT_FOUND",
|
|
576
|
-
418: "I_AM_A_TEAPOT",
|
|
577
|
-
500: "INTERNAL_SERVER_ERROR"
|
|
578
|
-
};
|
|
579
|
-
/** Represents common errors you may get from a HTTP API request. */
|
|
580
|
-
var APIError = class extends Error {
|
|
581
|
-
status;
|
|
582
|
-
/**
|
|
583
|
-
* @param status - A HTTP status code. Can be any number, but numbers between 400 and 600 are encouraged to fit with HTTP status code conventions.
|
|
584
|
-
* @param message - An error message to display alongside the status code.
|
|
585
|
-
* @param options - Extra options to be passed to super Error constructor.
|
|
586
|
-
*/
|
|
587
|
-
constructor(status = 500, message, options) {
|
|
588
|
-
super(message, options);
|
|
589
|
-
this.status = status;
|
|
590
|
-
if (message) this.message = message;
|
|
591
|
-
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
592
|
-
Object.defineProperty(this, "message", { enumerable: true });
|
|
593
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
594
|
-
}
|
|
595
|
-
/**
|
|
596
|
-
* Checks whether the given input may have been caused by an APIError.
|
|
597
|
-
*
|
|
598
|
-
* @param input - The input to check.
|
|
599
|
-
*
|
|
600
|
-
* @returns `true` if the input is an APIError, and `false` otherwise. The type of the input will also be narrowed down to APIError if `true`.
|
|
601
|
-
*/
|
|
602
|
-
static check(input) {
|
|
603
|
-
const data = input;
|
|
604
|
-
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
605
|
-
}
|
|
606
|
-
};
|
|
607
|
-
var APIError_default = APIError;
|
|
608
|
-
|
|
609
|
-
//#endregion
|
|
610
|
-
//#region src/types/DataError.ts
|
|
611
|
-
/** Represents errors you may get that may've been caused by a specific piece of data. */
|
|
612
|
-
var DataError = class extends Error {
|
|
613
|
-
data;
|
|
614
|
-
code;
|
|
615
|
-
/**
|
|
616
|
-
* @param data - The data that caused the error.
|
|
617
|
-
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
618
|
-
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
619
|
-
* @param options - Extra options to pass to super Error constructor.
|
|
620
|
-
*/
|
|
621
|
-
constructor(data, message = "The data provided is invalid", code = "INVALID_DATA", options) {
|
|
622
|
-
super(message, options);
|
|
623
|
-
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
624
|
-
this.name = new.target.name;
|
|
625
|
-
this.code = code;
|
|
626
|
-
this.data = data;
|
|
627
|
-
Object.defineProperty(this, "message", { enumerable: true });
|
|
628
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
629
|
-
}
|
|
630
|
-
/**
|
|
631
|
-
* Checks whether the given input may have been caused by a DataError.
|
|
632
|
-
*
|
|
633
|
-
* @param input - The input to check.
|
|
634
|
-
*
|
|
635
|
-
* @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`.
|
|
636
|
-
*/
|
|
637
|
-
static check(input) {
|
|
638
|
-
const data = input;
|
|
639
|
-
return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
|
|
640
|
-
}
|
|
641
|
-
};
|
|
642
|
-
var DataError_default = DataError;
|
|
643
|
-
|
|
644
|
-
//#endregion
|
|
645
|
-
//#region src/types/Email.ts
|
|
646
|
-
const emailSchema = z.email().brand();
|
|
647
|
-
/** @deprecated Please use `z.email().parse() from Zod instead.`*/
|
|
648
|
-
function parseEmail(data) {
|
|
649
|
-
return emailSchema.parse(data);
|
|
650
|
-
}
|
|
651
|
-
var Email_default = parseEmail;
|
|
652
|
-
|
|
653
|
-
//#endregion
|
|
654
|
-
//#region src/types/UUID.ts
|
|
655
|
-
const uuidSchema = z.uuid().brand();
|
|
656
|
-
/** @deprecated Please use `z.uuid().parse() from Zod instead.`*/
|
|
657
|
-
function parseUUID(UUID) {
|
|
658
|
-
return uuidSchema.parse(UUID);
|
|
659
|
-
}
|
|
660
|
-
var UUID_default = parseUUID;
|
|
661
|
-
|
|
662
|
-
//#endregion
|
|
663
|
-
//#region src/functions/parsers/parseZodSchema.ts
|
|
664
|
-
/**
|
|
665
|
-
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
666
|
-
*
|
|
667
|
-
* @template Output - The Zod output type.
|
|
668
|
-
* @template Input - The Zod input type.
|
|
669
|
-
* @template Internals - The Zod internal types based on the output and input types.
|
|
670
|
-
*
|
|
671
|
-
* @param schema - The Zod schema to use in parsing.
|
|
672
|
-
* @param data - The data to parse.
|
|
673
|
-
*
|
|
674
|
-
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
675
|
-
*
|
|
676
|
-
* @returns The parsed data from the Zod schema.
|
|
677
|
-
*/
|
|
678
|
-
function parseZodSchema(schema, data) {
|
|
679
|
-
const parsedResult = schema.safeParse(data);
|
|
680
|
-
if (!parsedResult.success) throw new DataError_default(data);
|
|
681
|
-
return parsedResult.data;
|
|
682
|
-
}
|
|
683
|
-
var parseZodSchema_default = parseZodSchema;
|
|
684
|
-
|
|
685
673
|
//#endregion
|
|
686
674
|
//#region src/functions/parsers/parseVersionType.ts
|
|
687
675
|
const versionTypeSchema = z.enum([
|
|
@@ -699,7 +687,7 @@ const versionTypeSchema = z.enum([
|
|
|
699
687
|
* @returns The given version type if allowed.
|
|
700
688
|
*/
|
|
701
689
|
function parseVersionType(data) {
|
|
702
|
-
return parseZodSchema_default(versionTypeSchema, data);
|
|
690
|
+
return parseZodSchema_default(versionTypeSchema, data, new DataError_default(data, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
703
691
|
}
|
|
704
692
|
var parseVersionType_default = parseVersionType;
|
|
705
693
|
|
|
@@ -938,25 +926,25 @@ var interpolateObjects_default = interpolateObjects;
|
|
|
938
926
|
|
|
939
927
|
//#endregion
|
|
940
928
|
//#region src/functions/taggedTemplate/normaliseIndents.ts
|
|
941
|
-
function calculateTabSize
|
|
929
|
+
function calculateTabSize(line, whitespaceLength) {
|
|
942
930
|
const potentialWhitespacePart = line.slice(0, whitespaceLength);
|
|
943
931
|
const trimmedString = line.trimStart();
|
|
944
932
|
if (potentialWhitespacePart.trim() !== "") return 0;
|
|
945
933
|
const tabSize = line.length - (trimmedString.length + whitespaceLength);
|
|
946
934
|
return tabSize < 0 ? 0 : tabSize;
|
|
947
935
|
}
|
|
948
|
-
function getWhitespaceLength
|
|
936
|
+
function getWhitespaceLength(lines) {
|
|
949
937
|
const [firstNonEmptyLine] = lines.filter((line) => {
|
|
950
938
|
return line.trim() !== "";
|
|
951
939
|
});
|
|
952
940
|
return firstNonEmptyLine.length - firstNonEmptyLine.trimStart().length;
|
|
953
941
|
}
|
|
954
|
-
function reduceLines
|
|
942
|
+
function reduceLines(lines, { preserveTabs = true }) {
|
|
955
943
|
const slicedLines = lines.slice(1);
|
|
956
944
|
const isFirstLineEmpty = lines[0].trim() === "";
|
|
957
|
-
const whitespaceLength = getWhitespaceLength
|
|
945
|
+
const whitespaceLength = getWhitespaceLength(isFirstLineEmpty ? lines : slicedLines);
|
|
958
946
|
return (isFirstLineEmpty ? slicedLines : lines).map((line) => {
|
|
959
|
-
const tabSize = calculateTabSize
|
|
947
|
+
const tabSize = calculateTabSize(line, whitespaceLength);
|
|
960
948
|
return (preserveTabs ? fillArray_default(() => {
|
|
961
949
|
return " ";
|
|
962
950
|
}, tabSize).join("") : "") + line.trimStart();
|
|
@@ -991,7 +979,7 @@ function normaliseIndents(first, ...args) {
|
|
|
991
979
|
}
|
|
992
980
|
const strings = first;
|
|
993
981
|
const options = typeof args[args.length - 1] === "object" && !Array.isArray(args[args.length - 1]) ? args.pop() : {};
|
|
994
|
-
return reduceLines
|
|
982
|
+
return reduceLines(interpolate_default(strings, ...[...args]).split("\n"), options);
|
|
995
983
|
}
|
|
996
984
|
/**
|
|
997
985
|
* Applies any options if provided, then removes any extraneous indents from a multi-line template string.
|
|
@@ -1016,45 +1004,6 @@ function normaliseIndents(first, ...args) {
|
|
|
1016
1004
|
const normalizeIndents = normaliseIndents;
|
|
1017
1005
|
var normaliseIndents_default = normaliseIndents;
|
|
1018
1006
|
|
|
1019
|
-
//#endregion
|
|
1020
|
-
//#region src/functions/taggedTemplate/removeIndents.ts
|
|
1021
|
-
function calculateTabSize(line, whitespaceLength) {
|
|
1022
|
-
const potentialWhitespacePart = line.slice(0, whitespaceLength);
|
|
1023
|
-
const trimmedString = line.trimStart();
|
|
1024
|
-
if (potentialWhitespacePart.trim() !== "") return 0;
|
|
1025
|
-
const tabSize = line.length - (trimmedString.length + whitespaceLength);
|
|
1026
|
-
return tabSize < 0 ? 0 : tabSize;
|
|
1027
|
-
}
|
|
1028
|
-
function getWhitespaceLength(lines) {
|
|
1029
|
-
const [firstNonEmptyLine] = lines.filter((line) => {
|
|
1030
|
-
return line.trim() !== "";
|
|
1031
|
-
});
|
|
1032
|
-
return firstNonEmptyLine.length - firstNonEmptyLine.trimStart().length;
|
|
1033
|
-
}
|
|
1034
|
-
function reduceLines(lines, { preserveTabs = true }) {
|
|
1035
|
-
const slicedLines = lines.slice(1);
|
|
1036
|
-
const isFirstLineEmpty = lines[0].trim() === "";
|
|
1037
|
-
const whitespaceLength = getWhitespaceLength(isFirstLineEmpty ? lines : slicedLines);
|
|
1038
|
-
return (isFirstLineEmpty ? slicedLines : lines).map((line) => {
|
|
1039
|
-
const tabSize = calculateTabSize(line, whitespaceLength);
|
|
1040
|
-
return (preserveTabs ? fillArray_default(() => {
|
|
1041
|
-
return " ";
|
|
1042
|
-
}, tabSize).join("") : "") + line.trimStart();
|
|
1043
|
-
}).join("\n");
|
|
1044
|
-
}
|
|
1045
|
-
function removeIndents(first, ...args) {
|
|
1046
|
-
if (typeof first === "object" && first !== null && !Array.isArray(first)) {
|
|
1047
|
-
const options$1 = first;
|
|
1048
|
-
return (strings$1, ...interpolations) => {
|
|
1049
|
-
return removeIndents(strings$1, ...interpolations, options$1);
|
|
1050
|
-
};
|
|
1051
|
-
}
|
|
1052
|
-
const strings = first;
|
|
1053
|
-
const options = typeof args[args.length - 1] === "object" && !Array.isArray(args[args.length - 1]) ? args.pop() : {};
|
|
1054
|
-
return reduceLines(interpolate_default(strings, ...[...args]).split("\n"), options);
|
|
1055
|
-
}
|
|
1056
|
-
var removeIndents_default = removeIndents;
|
|
1057
|
-
|
|
1058
1007
|
//#endregion
|
|
1059
1008
|
//#region src/functions/versioning/parseVersion.ts
|
|
1060
1009
|
/**
|
|
@@ -1070,7 +1019,7 @@ var removeIndents_default = removeIndents;
|
|
|
1070
1019
|
* @returns The validated version number, prefixed with `v` if it was not already.
|
|
1071
1020
|
*/
|
|
1072
1021
|
function parseVersion(input, options) {
|
|
1073
|
-
if (!RegExp(VERSION_NUMBER_REGEX_default).test(input)) throw new DataError_default(input, `"${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
|
|
1022
|
+
if (!RegExp(VERSION_NUMBER_REGEX_default).test(input)) throw new DataError_default(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.`);
|
|
1074
1023
|
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
1075
1024
|
return input.startsWith("v") ? input : `v${input}`;
|
|
1076
1025
|
}
|
|
@@ -1135,4 +1084,4 @@ function incrementVersion(version, incrementType, options) {
|
|
|
1135
1084
|
var incrementVersion_default = incrementVersion;
|
|
1136
1085
|
|
|
1137
1086
|
//#endregion
|
|
1138
|
-
export { APIError_default as APIError, DataError_default as DataError, NAMESPACE_EXPORT_REGEX_default as NAMESPACE_EXPORT_REGEX, VERSION_NUMBER_REGEX_default as VERSION_NUMBER_REGEX, addDaysToDate_default as addDaysToDate, appendSemicolon_default as appendSemicolon, camelToKebab_default as camelToKebab, convertFileToBase64_default as convertFileToBase64, createFormData_default as createFormData, createTemplateStringsArray_default as createTemplateStringsArray, deepCopy_default as deepCopy, deepFreeze_default as deepFreeze, determineVersionType_default as determineVersionType, fillArray_default as fillArray, formatDateAndTime_default as formatDateAndTime, getIndividualVersionNumbers_default as getIndividualVersionNumbers, getRandomNumber_default as getRandomNumber, getRecordKeys_default as getRecordKeys, httpErrorCodeLookup, incrementVersion_default as incrementVersion, interpolate_default as interpolate, interpolateObjects_default as interpolateObjects, isAnniversary_default as isAnniversary, isLeapYear_default as isLeapYear, isMonthlyMultiple_default as isMonthlyMultiple, isOrdered_default as isOrdered, isSameDate_default as isSameDate, kebabToCamel_default as kebabToCamel, normaliseImportPath, normaliseIndents_default as normaliseIndents, normalizeImportPath_default as normalizeImportPath, normalizeIndents, omitProperties_default as omitProperties, paralleliseArrays_default as paralleliseArrays, parseBoolean_default as parseBoolean,
|
|
1087
|
+
export { APIError_default as APIError, DataError_default as DataError, NAMESPACE_EXPORT_REGEX_default as NAMESPACE_EXPORT_REGEX, VERSION_NUMBER_REGEX_default as VERSION_NUMBER_REGEX, addDaysToDate_default as addDaysToDate, appendSemicolon_default as appendSemicolon, camelToKebab_default as camelToKebab, convertFileToBase64_default as convertFileToBase64, createFormData_default as createFormData, createTemplateStringsArray_default as createTemplateStringsArray, deepCopy_default as deepCopy, deepFreeze_default as deepFreeze, determineVersionType_default as determineVersionType, fillArray_default as fillArray, formatDateAndTime_default as formatDateAndTime, getIndividualVersionNumbers_default as getIndividualVersionNumbers, getRandomNumber_default as getRandomNumber, getRecordKeys_default as getRecordKeys, httpErrorCodeLookup, incrementVersion_default as incrementVersion, interpolate_default as interpolate, interpolateObjects_default as interpolateObjects, isAnniversary_default as isAnniversary, isLeapYear_default as isLeapYear, isMonthlyMultiple_default as isMonthlyMultiple, isOrdered_default as isOrdered, isSameDate_default as isSameDate, kebabToCamel_default as kebabToCamel, normaliseImportPath, normaliseIndents_default as normaliseIndents, normalizeImportPath_default as normalizeImportPath, normalizeIndents, omitProperties_default as omitProperties, paralleliseArrays_default as paralleliseArrays, parseBoolean_default as parseBoolean, parseEnv_default as parseEnv, parseFormData_default as parseFormData, parseIntStrict_default as parseIntStrict, parseVersion_default as parseVersion, parseVersionType_default as parseVersionType, parseZodSchema_default as parseZodSchema, randomiseArray_default as randomiseArray, range_default as range, removeDuplicates_default as removeDuplicates, stringListToArray_default as stringListToArray, truncate_default as truncate, wait_default as wait };
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alextheman/utility",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Helpful utility functions",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/AlexMan123456/utility.git"
|
|
8
8
|
},
|
|
9
|
-
"license": "
|
|
9
|
+
"license": "MIT",
|
|
10
10
|
"author": "alextheman",
|
|
11
11
|
"type": "module",
|
|
12
12
|
"main": "dist/index.cjs",
|
|
@@ -16,28 +16,30 @@
|
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"zod": "^4.1
|
|
19
|
+
"zod": "^4.2.1"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@alextheman/eslint-plugin": "^4.
|
|
23
|
-
"@types/node": "^25.0.
|
|
22
|
+
"@alextheman/eslint-plugin": "^4.9.0",
|
|
23
|
+
"@types/node": "^25.0.3",
|
|
24
|
+
"alex-c-line": "^1.9.0",
|
|
24
25
|
"dotenv-cli": "^11.0.0",
|
|
25
|
-
"eslint": "^9.39.
|
|
26
|
+
"eslint": "^9.39.2",
|
|
27
|
+
"eslint-plugin-perfectionist": "^5.0.0",
|
|
26
28
|
"globals": "^16.5.0",
|
|
27
29
|
"husky": "^9.1.7",
|
|
28
30
|
"jsdom": "^27.3.0",
|
|
29
31
|
"prettier": "^3.7.4",
|
|
30
|
-
"tsdown": "^0.
|
|
32
|
+
"tsdown": "^0.18.1",
|
|
31
33
|
"typescript": "^5.9.3",
|
|
32
|
-
"vite-tsconfig-paths": "^
|
|
33
|
-
"vitest": "^4.0.
|
|
34
|
+
"vite-tsconfig-paths": "^6.0.3",
|
|
35
|
+
"vitest": "^4.0.16"
|
|
34
36
|
},
|
|
35
37
|
"scripts": {
|
|
36
38
|
"build": "tsdown",
|
|
37
|
-
"change-major": "pnpm version major -m \"Change version number to v%s\"",
|
|
38
|
-
"change-minor": "pnpm version minor -m \"Change version number to v%s\"",
|
|
39
|
-
"change-patch": "pnpm version patch -m \"Change version number to v%s\"",
|
|
40
39
|
"create-local-package": "pnpm run build && rm -f alextheman-utility-*.tgz && pnpm pack",
|
|
40
|
+
"create-release-note-major": "git pull origin main && alex-c-line create-release-note major",
|
|
41
|
+
"create-release-note-minor": "git pull origin main && alex-c-line create-release-note minor",
|
|
42
|
+
"create-release-note-patch": "git pull origin main && alex-c-line create-release-note patch",
|
|
41
43
|
"format": "pnpm run format-prettier && pnpm run format-eslint",
|
|
42
44
|
"format-eslint": "eslint --fix --suppress-all \"package.json\" \"src/**/*.ts\" \"tests/**/*.ts\" && rm -f eslint-suppressions.json",
|
|
43
45
|
"format-prettier": "pnpm run format-prettier-typescript && pnpm run format-prettier-javascript",
|