@alextheman/utility 5.13.1 → 5.14.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 +271 -235
- package/dist/index.d.cts +175 -156
- package/dist/index.d.ts +174 -155
- package/dist/index.js +271 -236
- package/dist/internal/index.cjs +311 -41
- package/dist/internal/index.js +311 -41
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -893,7 +893,217 @@ function parseBoolean(inputString) {
|
|
|
893
893
|
return normalisedString === "true";
|
|
894
894
|
}
|
|
895
895
|
//#endregion
|
|
896
|
-
//#region src/root/
|
|
896
|
+
//#region src/root/types/APIError.ts
|
|
897
|
+
const httpErrorCodeLookup = {
|
|
898
|
+
400: "BAD_REQUEST",
|
|
899
|
+
401: "UNAUTHORISED",
|
|
900
|
+
403: "FORBIDDEN",
|
|
901
|
+
404: "NOT_FOUND",
|
|
902
|
+
418: "I_AM_A_TEAPOT",
|
|
903
|
+
500: "INTERNAL_SERVER_ERROR"
|
|
904
|
+
};
|
|
905
|
+
/**
|
|
906
|
+
* Represents common errors you may get from a HTTP API request.
|
|
907
|
+
*
|
|
908
|
+
* @category Types
|
|
909
|
+
*/
|
|
910
|
+
var APIError = class APIError extends Error {
|
|
911
|
+
status;
|
|
912
|
+
/**
|
|
913
|
+
* @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.
|
|
914
|
+
* @param message - An error message to display alongside the status code.
|
|
915
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
916
|
+
*/
|
|
917
|
+
constructor(status = 500, message, options) {
|
|
918
|
+
super(message, options);
|
|
919
|
+
this.status = status;
|
|
920
|
+
if (message) this.message = message;
|
|
921
|
+
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
922
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
923
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Checks whether the given input may have been caused by an APIError.
|
|
927
|
+
*
|
|
928
|
+
* @param input - The input to check.
|
|
929
|
+
*
|
|
930
|
+
* @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`.
|
|
931
|
+
*/
|
|
932
|
+
static check(input) {
|
|
933
|
+
if (input instanceof APIError) return true;
|
|
934
|
+
const data = input;
|
|
935
|
+
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
//#endregion
|
|
939
|
+
//#region src/root/types/VersionNumber.ts
|
|
940
|
+
/**
|
|
941
|
+
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
942
|
+
*
|
|
943
|
+
* @category Types
|
|
944
|
+
*/
|
|
945
|
+
var VersionNumber = class VersionNumber {
|
|
946
|
+
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
947
|
+
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
948
|
+
major = 0;
|
|
949
|
+
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
950
|
+
minor = 0;
|
|
951
|
+
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
952
|
+
patch = 0;
|
|
953
|
+
/**
|
|
954
|
+
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
955
|
+
*/
|
|
956
|
+
constructor(input) {
|
|
957
|
+
if (input instanceof VersionNumber) {
|
|
958
|
+
this.major = input.major;
|
|
959
|
+
this.minor = input.minor;
|
|
960
|
+
this.patch = input.patch;
|
|
961
|
+
} else if (typeof input === "string") {
|
|
962
|
+
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError$1({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
963
|
+
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
964
|
+
return parseIntStrict(number);
|
|
965
|
+
});
|
|
966
|
+
this.major = major;
|
|
967
|
+
this.minor = minor;
|
|
968
|
+
this.patch = patch;
|
|
969
|
+
} else if (Array.isArray(input)) {
|
|
970
|
+
if (input.length !== 3) throw new DataError$1({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
971
|
+
const [major, minor, patch] = input.map((number) => {
|
|
972
|
+
const parsedInteger = parseIntStrict(number?.toString());
|
|
973
|
+
if (parsedInteger < 0) throw new DataError$1({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
974
|
+
return parsedInteger;
|
|
975
|
+
});
|
|
976
|
+
this.major = major;
|
|
977
|
+
this.minor = minor;
|
|
978
|
+
this.patch = patch;
|
|
979
|
+
} else throw new DataError$1({ input }, "INVALID_INPUT", normaliseIndents`
|
|
980
|
+
The provided input can not be parsed into a valid version number.
|
|
981
|
+
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
982
|
+
`);
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Gets the current version type of the current instance of `VersionNumber`.
|
|
986
|
+
*
|
|
987
|
+
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
988
|
+
*/
|
|
989
|
+
get type() {
|
|
990
|
+
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
991
|
+
if (this.patch === 0) return VersionType.MINOR;
|
|
992
|
+
return VersionType.PATCH;
|
|
993
|
+
}
|
|
994
|
+
static formatString(input, options) {
|
|
995
|
+
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
996
|
+
return input.startsWith("v") ? input : `v${input}`;
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
1000
|
+
*
|
|
1001
|
+
* @param firstVersion - The first version number to compare.
|
|
1002
|
+
* @param secondVersion - The second version number to compare.
|
|
1003
|
+
*
|
|
1004
|
+
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
1005
|
+
*/
|
|
1006
|
+
static isEqual(firstVersion, secondVersion) {
|
|
1007
|
+
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Get a formatted string representation of the current version number
|
|
1011
|
+
*
|
|
1012
|
+
* @param options - Options to apply to the string formatting.
|
|
1013
|
+
*
|
|
1014
|
+
* @returns A formatted string representation of the current version number with the options applied.
|
|
1015
|
+
*/
|
|
1016
|
+
format(options) {
|
|
1017
|
+
let baseOutput = `${this.major}`;
|
|
1018
|
+
if (!options?.omitMinor) {
|
|
1019
|
+
baseOutput += `.${this.minor}`;
|
|
1020
|
+
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
1021
|
+
}
|
|
1022
|
+
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
1026
|
+
*
|
|
1027
|
+
* @param incrementType - The type of increment. Can be one of the following:
|
|
1028
|
+
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
1029
|
+
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
1030
|
+
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
1031
|
+
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
1032
|
+
*
|
|
1033
|
+
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
1034
|
+
*/
|
|
1035
|
+
increment(incrementType, incrementAmount = 1) {
|
|
1036
|
+
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
1037
|
+
const calculatedRawVersion = {
|
|
1038
|
+
major: [
|
|
1039
|
+
this.major + incrementBy,
|
|
1040
|
+
0,
|
|
1041
|
+
0
|
|
1042
|
+
],
|
|
1043
|
+
minor: [
|
|
1044
|
+
this.major,
|
|
1045
|
+
this.minor + incrementBy,
|
|
1046
|
+
0
|
|
1047
|
+
],
|
|
1048
|
+
patch: [
|
|
1049
|
+
this.major,
|
|
1050
|
+
this.minor,
|
|
1051
|
+
this.patch + incrementBy
|
|
1052
|
+
]
|
|
1053
|
+
}[incrementType];
|
|
1054
|
+
try {
|
|
1055
|
+
return new VersionNumber(calculatedRawVersion);
|
|
1056
|
+
} catch (error) {
|
|
1057
|
+
if (DataError$1.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError$1({
|
|
1058
|
+
currentVersion: this.toString(),
|
|
1059
|
+
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
1060
|
+
incrementAmount
|
|
1061
|
+
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
1062
|
+
else throw error;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
1067
|
+
*
|
|
1068
|
+
* @param hint - Not used as of now, but generally used to help with numeric coercion, I think (which we most likely do not need for version numbers).
|
|
1069
|
+
*
|
|
1070
|
+
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1071
|
+
*/
|
|
1072
|
+
[Symbol.toPrimitive](hint) {
|
|
1073
|
+
if (hint === "number") throw new DataError$1({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1074
|
+
return this.toString();
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
1078
|
+
*
|
|
1079
|
+
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1080
|
+
*/
|
|
1081
|
+
toJSON() {
|
|
1082
|
+
return this.toString();
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* Get a string representation of the current version number.
|
|
1086
|
+
*
|
|
1087
|
+
* @returns A stringified representation of the current version number with the prefix.
|
|
1088
|
+
*/
|
|
1089
|
+
toString() {
|
|
1090
|
+
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
1091
|
+
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
1092
|
+
}
|
|
1093
|
+
};
|
|
1094
|
+
const zodVersionNumber = zod.default.union([
|
|
1095
|
+
zod.default.string(),
|
|
1096
|
+
zod.default.tuple([
|
|
1097
|
+
zod.default.number(),
|
|
1098
|
+
zod.default.number(),
|
|
1099
|
+
zod.default.number()
|
|
1100
|
+
]),
|
|
1101
|
+
zod.default.instanceof(VersionNumber)
|
|
1102
|
+
]).transform((rawVersionNumber) => {
|
|
1103
|
+
return new VersionNumber(rawVersionNumber);
|
|
1104
|
+
});
|
|
1105
|
+
//#endregion
|
|
1106
|
+
//#region src/root/zod/_parseZodSchema.ts
|
|
897
1107
|
function _parseZodSchema(parsedResult, input, onError) {
|
|
898
1108
|
if (!parsedResult.success) {
|
|
899
1109
|
if (onError) {
|
|
@@ -917,7 +1127,7 @@ function _parseZodSchema(parsedResult, input, onError) {
|
|
|
917
1127
|
return parsedResult.data;
|
|
918
1128
|
}
|
|
919
1129
|
//#endregion
|
|
920
|
-
//#region src/root/
|
|
1130
|
+
//#region src/root/zod/parseZodSchema.ts
|
|
921
1131
|
/**
|
|
922
1132
|
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
923
1133
|
*
|
|
@@ -925,6 +1135,8 @@ function _parseZodSchema(parsedResult, input, onError) {
|
|
|
925
1135
|
*
|
|
926
1136
|
* @category Parsers
|
|
927
1137
|
*
|
|
1138
|
+
* @deprecated Please use `az.with(schema).parse(input)` instead.
|
|
1139
|
+
*
|
|
928
1140
|
* @template SchemaType - The Zod schema type.
|
|
929
1141
|
* @template ErrorType - The type of error to throw on invalid data.
|
|
930
1142
|
*
|
|
@@ -940,6 +1152,60 @@ function parseZodSchema(schema, input, onError) {
|
|
|
940
1152
|
return _parseZodSchema(schema.safeParse(input), input, onError);
|
|
941
1153
|
}
|
|
942
1154
|
//#endregion
|
|
1155
|
+
//#region src/root/zod/parseZodSchemaAsync.ts
|
|
1156
|
+
/**
|
|
1157
|
+
* An alternative function to zodSchema.parseAsync() that can be used to strictly parse asynchronous Zod schemas.
|
|
1158
|
+
*
|
|
1159
|
+
* @category Parsers
|
|
1160
|
+
*
|
|
1161
|
+
* @deprecated Please use `az.with(schema).parseAsync(input)` instead.
|
|
1162
|
+
*
|
|
1163
|
+
* @template SchemaType - The Zod schema type.
|
|
1164
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
1165
|
+
*
|
|
1166
|
+
* @param schema - The Zod schema to use in parsing.
|
|
1167
|
+
* @param input - The data to parse.
|
|
1168
|
+
* @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
|
|
1169
|
+
*
|
|
1170
|
+
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
1171
|
+
*
|
|
1172
|
+
* @returns The parsed data from the Zod schema.
|
|
1173
|
+
*/
|
|
1174
|
+
async function parseZodSchemaAsync(schema, input, onError) {
|
|
1175
|
+
return _parseZodSchema(await schema.safeParseAsync(input), input, onError);
|
|
1176
|
+
}
|
|
1177
|
+
//#endregion
|
|
1178
|
+
//#region src/root/zod/zodFieldWrapper.ts
|
|
1179
|
+
function zodFieldWrapper(schema) {
|
|
1180
|
+
return zod.default.string().trim().transform((value) => {
|
|
1181
|
+
return value === "" ? null : value;
|
|
1182
|
+
}).pipe(schema);
|
|
1183
|
+
}
|
|
1184
|
+
//#endregion
|
|
1185
|
+
//#region src/root/zod/az.ts
|
|
1186
|
+
const az = {
|
|
1187
|
+
field: zodFieldWrapper,
|
|
1188
|
+
fieldNumber: () => {
|
|
1189
|
+
return zod.default.coerce.number();
|
|
1190
|
+
},
|
|
1191
|
+
versionNumber: () => {
|
|
1192
|
+
return zodVersionNumber;
|
|
1193
|
+
},
|
|
1194
|
+
fieldDate: () => {
|
|
1195
|
+
return zod.default.coerce.date();
|
|
1196
|
+
},
|
|
1197
|
+
with: (schema) => {
|
|
1198
|
+
return {
|
|
1199
|
+
parse: (input, error) => {
|
|
1200
|
+
return parseZodSchema(schema, input, error);
|
|
1201
|
+
},
|
|
1202
|
+
parseAsync: async (input, error) => {
|
|
1203
|
+
return await parseZodSchemaAsync(schema, input, error);
|
|
1204
|
+
}
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
};
|
|
1208
|
+
//#endregion
|
|
943
1209
|
//#region src/root/functions/parsers/parseEnv.ts
|
|
944
1210
|
/**
|
|
945
1211
|
* Represents the three common development environments.
|
|
@@ -963,7 +1229,7 @@ const Env = {
|
|
|
963
1229
|
* @returns The specified environment if allowed.
|
|
964
1230
|
*/
|
|
965
1231
|
function parseEnv(input) {
|
|
966
|
-
return
|
|
1232
|
+
return az.with(zod.z.enum(Env)).parse(input, new DataError$1({ input }, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
967
1233
|
}
|
|
968
1234
|
//#endregion
|
|
969
1235
|
//#region src/root/functions/parsers/parseFormData.ts
|
|
@@ -1029,28 +1295,7 @@ const VersionType = {
|
|
|
1029
1295
|
* @returns The given version type if allowed.
|
|
1030
1296
|
*/
|
|
1031
1297
|
function parseVersionType(input) {
|
|
1032
|
-
return
|
|
1033
|
-
}
|
|
1034
|
-
//#endregion
|
|
1035
|
-
//#region src/root/functions/parsers/zod/parseZodSchemaAsync.ts
|
|
1036
|
-
/**
|
|
1037
|
-
* An alternative function to zodSchema.parseAsync() that can be used to strictly parse asynchronous Zod schemas.
|
|
1038
|
-
*
|
|
1039
|
-
* @category Parsers
|
|
1040
|
-
*
|
|
1041
|
-
* @template SchemaType - The Zod schema type.
|
|
1042
|
-
* @template ErrorType - The type of error to throw on invalid data.
|
|
1043
|
-
*
|
|
1044
|
-
* @param schema - The Zod schema to use in parsing.
|
|
1045
|
-
* @param input - The data to parse.
|
|
1046
|
-
* @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
|
|
1047
|
-
*
|
|
1048
|
-
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
1049
|
-
*
|
|
1050
|
-
* @returns The parsed data from the Zod schema.
|
|
1051
|
-
*/
|
|
1052
|
-
async function parseZodSchemaAsync(schema, input, onError) {
|
|
1053
|
-
return _parseZodSchema(await schema.safeParseAsync(input), input, onError);
|
|
1298
|
+
return az.with(zod.default.enum(VersionType)).parse(input, new DataError$1({ input }, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
1054
1299
|
}
|
|
1055
1300
|
//#endregion
|
|
1056
1301
|
//#region src/root/functions/recursive/deepCopy.ts
|
|
@@ -1556,216 +1801,6 @@ var DataError = class DataError extends Error {
|
|
|
1556
1801
|
}
|
|
1557
1802
|
};
|
|
1558
1803
|
//#endregion
|
|
1559
|
-
//#region src/root/types/APIError.ts
|
|
1560
|
-
const httpErrorCodeLookup = {
|
|
1561
|
-
400: "BAD_REQUEST",
|
|
1562
|
-
401: "UNAUTHORISED",
|
|
1563
|
-
403: "FORBIDDEN",
|
|
1564
|
-
404: "NOT_FOUND",
|
|
1565
|
-
418: "I_AM_A_TEAPOT",
|
|
1566
|
-
500: "INTERNAL_SERVER_ERROR"
|
|
1567
|
-
};
|
|
1568
|
-
/**
|
|
1569
|
-
* Represents common errors you may get from a HTTP API request.
|
|
1570
|
-
*
|
|
1571
|
-
* @category Types
|
|
1572
|
-
*/
|
|
1573
|
-
var APIError = class APIError extends Error {
|
|
1574
|
-
status;
|
|
1575
|
-
/**
|
|
1576
|
-
* @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.
|
|
1577
|
-
* @param message - An error message to display alongside the status code.
|
|
1578
|
-
* @param options - Extra options to be passed to super Error constructor.
|
|
1579
|
-
*/
|
|
1580
|
-
constructor(status = 500, message, options) {
|
|
1581
|
-
super(message, options);
|
|
1582
|
-
this.status = status;
|
|
1583
|
-
if (message) this.message = message;
|
|
1584
|
-
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
1585
|
-
Object.defineProperty(this, "message", { enumerable: true });
|
|
1586
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
1587
|
-
}
|
|
1588
|
-
/**
|
|
1589
|
-
* Checks whether the given input may have been caused by an APIError.
|
|
1590
|
-
*
|
|
1591
|
-
* @param input - The input to check.
|
|
1592
|
-
*
|
|
1593
|
-
* @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`.
|
|
1594
|
-
*/
|
|
1595
|
-
static check(input) {
|
|
1596
|
-
if (input instanceof APIError) return true;
|
|
1597
|
-
const data = input;
|
|
1598
|
-
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
1599
|
-
}
|
|
1600
|
-
};
|
|
1601
|
-
//#endregion
|
|
1602
|
-
//#region src/root/types/VersionNumber.ts
|
|
1603
|
-
/**
|
|
1604
|
-
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
1605
|
-
*
|
|
1606
|
-
* @category Types
|
|
1607
|
-
*/
|
|
1608
|
-
var VersionNumber = class VersionNumber {
|
|
1609
|
-
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
1610
|
-
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
1611
|
-
major = 0;
|
|
1612
|
-
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
1613
|
-
minor = 0;
|
|
1614
|
-
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
1615
|
-
patch = 0;
|
|
1616
|
-
/**
|
|
1617
|
-
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
1618
|
-
*/
|
|
1619
|
-
constructor(input) {
|
|
1620
|
-
if (input instanceof VersionNumber) {
|
|
1621
|
-
this.major = input.major;
|
|
1622
|
-
this.minor = input.minor;
|
|
1623
|
-
this.patch = input.patch;
|
|
1624
|
-
} else if (typeof input === "string") {
|
|
1625
|
-
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError$1({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
1626
|
-
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
1627
|
-
return parseIntStrict(number);
|
|
1628
|
-
});
|
|
1629
|
-
this.major = major;
|
|
1630
|
-
this.minor = minor;
|
|
1631
|
-
this.patch = patch;
|
|
1632
|
-
} else if (Array.isArray(input)) {
|
|
1633
|
-
if (input.length !== 3) throw new DataError$1({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1634
|
-
const [major, minor, patch] = input.map((number) => {
|
|
1635
|
-
const parsedInteger = parseIntStrict(number?.toString());
|
|
1636
|
-
if (parsedInteger < 0) throw new DataError$1({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1637
|
-
return parsedInteger;
|
|
1638
|
-
});
|
|
1639
|
-
this.major = major;
|
|
1640
|
-
this.minor = minor;
|
|
1641
|
-
this.patch = patch;
|
|
1642
|
-
} else throw new DataError$1({ input }, "INVALID_INPUT", normaliseIndents`
|
|
1643
|
-
The provided input can not be parsed into a valid version number.
|
|
1644
|
-
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
1645
|
-
`);
|
|
1646
|
-
}
|
|
1647
|
-
/**
|
|
1648
|
-
* Gets the current version type of the current instance of `VersionNumber`.
|
|
1649
|
-
*
|
|
1650
|
-
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
1651
|
-
*/
|
|
1652
|
-
get type() {
|
|
1653
|
-
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
1654
|
-
if (this.patch === 0) return VersionType.MINOR;
|
|
1655
|
-
return VersionType.PATCH;
|
|
1656
|
-
}
|
|
1657
|
-
static formatString(input, options) {
|
|
1658
|
-
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
1659
|
-
return input.startsWith("v") ? input : `v${input}`;
|
|
1660
|
-
}
|
|
1661
|
-
/**
|
|
1662
|
-
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
1663
|
-
*
|
|
1664
|
-
* @param firstVersion - The first version number to compare.
|
|
1665
|
-
* @param secondVersion - The second version number to compare.
|
|
1666
|
-
*
|
|
1667
|
-
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
1668
|
-
*/
|
|
1669
|
-
static isEqual(firstVersion, secondVersion) {
|
|
1670
|
-
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
1671
|
-
}
|
|
1672
|
-
/**
|
|
1673
|
-
* Get a formatted string representation of the current version number
|
|
1674
|
-
*
|
|
1675
|
-
* @param options - Options to apply to the string formatting.
|
|
1676
|
-
*
|
|
1677
|
-
* @returns A formatted string representation of the current version number with the options applied.
|
|
1678
|
-
*/
|
|
1679
|
-
format(options) {
|
|
1680
|
-
let baseOutput = `${this.major}`;
|
|
1681
|
-
if (!options?.omitMinor) {
|
|
1682
|
-
baseOutput += `.${this.minor}`;
|
|
1683
|
-
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
1684
|
-
}
|
|
1685
|
-
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
1686
|
-
}
|
|
1687
|
-
/**
|
|
1688
|
-
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
1689
|
-
*
|
|
1690
|
-
* @param incrementType - The type of increment. Can be one of the following:
|
|
1691
|
-
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
1692
|
-
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
1693
|
-
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
1694
|
-
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
1695
|
-
*
|
|
1696
|
-
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
1697
|
-
*/
|
|
1698
|
-
increment(incrementType, incrementAmount = 1) {
|
|
1699
|
-
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
1700
|
-
const calculatedRawVersion = {
|
|
1701
|
-
major: [
|
|
1702
|
-
this.major + incrementBy,
|
|
1703
|
-
0,
|
|
1704
|
-
0
|
|
1705
|
-
],
|
|
1706
|
-
minor: [
|
|
1707
|
-
this.major,
|
|
1708
|
-
this.minor + incrementBy,
|
|
1709
|
-
0
|
|
1710
|
-
],
|
|
1711
|
-
patch: [
|
|
1712
|
-
this.major,
|
|
1713
|
-
this.minor,
|
|
1714
|
-
this.patch + incrementBy
|
|
1715
|
-
]
|
|
1716
|
-
}[incrementType];
|
|
1717
|
-
try {
|
|
1718
|
-
return new VersionNumber(calculatedRawVersion);
|
|
1719
|
-
} catch (error) {
|
|
1720
|
-
if (DataError$1.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError$1({
|
|
1721
|
-
currentVersion: this.toString(),
|
|
1722
|
-
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
1723
|
-
incrementAmount
|
|
1724
|
-
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
1725
|
-
else throw error;
|
|
1726
|
-
}
|
|
1727
|
-
}
|
|
1728
|
-
/**
|
|
1729
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
1730
|
-
*
|
|
1731
|
-
* @param hint - Not used as of now, but generally used to help with numeric coercion, I think (which we most likely do not need for version numbers).
|
|
1732
|
-
*
|
|
1733
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1734
|
-
*/
|
|
1735
|
-
[Symbol.toPrimitive](hint) {
|
|
1736
|
-
if (hint === "number") throw new DataError$1({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1737
|
-
return this.toString();
|
|
1738
|
-
}
|
|
1739
|
-
/**
|
|
1740
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
1741
|
-
*
|
|
1742
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1743
|
-
*/
|
|
1744
|
-
toJSON() {
|
|
1745
|
-
return this.toString();
|
|
1746
|
-
}
|
|
1747
|
-
/**
|
|
1748
|
-
* Get a string representation of the current version number.
|
|
1749
|
-
*
|
|
1750
|
-
* @returns A stringified representation of the current version number with the prefix.
|
|
1751
|
-
*/
|
|
1752
|
-
toString() {
|
|
1753
|
-
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
1754
|
-
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
1755
|
-
}
|
|
1756
|
-
};
|
|
1757
|
-
const zodVersionNumber = zod.default.union([
|
|
1758
|
-
zod.default.string(),
|
|
1759
|
-
zod.default.tuple([
|
|
1760
|
-
zod.default.number(),
|
|
1761
|
-
zod.default.number(),
|
|
1762
|
-
zod.default.number()
|
|
1763
|
-
]),
|
|
1764
|
-
zod.default.instanceof(VersionNumber)
|
|
1765
|
-
]).transform((rawVersionNumber) => {
|
|
1766
|
-
return new VersionNumber(rawVersionNumber);
|
|
1767
|
-
});
|
|
1768
|
-
//#endregion
|
|
1769
1804
|
exports.APIError = APIError;
|
|
1770
1805
|
exports.DataError = DataError;
|
|
1771
1806
|
exports.Env = Env;
|
|
@@ -1780,6 +1815,7 @@ exports.VersionNumber = VersionNumber;
|
|
|
1780
1815
|
exports.VersionType = VersionType;
|
|
1781
1816
|
exports.addDaysToDate = addDaysToDate;
|
|
1782
1817
|
exports.appendSemicolon = appendSemicolon;
|
|
1818
|
+
exports.az = az;
|
|
1783
1819
|
exports.calculateMonthlyDifference = calculateMonthlyDifference;
|
|
1784
1820
|
exports.camelToKebab = camelToKebab;
|
|
1785
1821
|
exports.convertFileToBase64 = convertFileToBase64;
|