@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.js
CHANGED
|
@@ -869,7 +869,217 @@ function parseBoolean(inputString) {
|
|
|
869
869
|
return normalisedString === "true";
|
|
870
870
|
}
|
|
871
871
|
//#endregion
|
|
872
|
-
//#region src/root/
|
|
872
|
+
//#region src/root/types/APIError.ts
|
|
873
|
+
const httpErrorCodeLookup = {
|
|
874
|
+
400: "BAD_REQUEST",
|
|
875
|
+
401: "UNAUTHORISED",
|
|
876
|
+
403: "FORBIDDEN",
|
|
877
|
+
404: "NOT_FOUND",
|
|
878
|
+
418: "I_AM_A_TEAPOT",
|
|
879
|
+
500: "INTERNAL_SERVER_ERROR"
|
|
880
|
+
};
|
|
881
|
+
/**
|
|
882
|
+
* Represents common errors you may get from a HTTP API request.
|
|
883
|
+
*
|
|
884
|
+
* @category Types
|
|
885
|
+
*/
|
|
886
|
+
var APIError = class APIError extends Error {
|
|
887
|
+
status;
|
|
888
|
+
/**
|
|
889
|
+
* @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.
|
|
890
|
+
* @param message - An error message to display alongside the status code.
|
|
891
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
892
|
+
*/
|
|
893
|
+
constructor(status = 500, message, options) {
|
|
894
|
+
super(message, options);
|
|
895
|
+
this.status = status;
|
|
896
|
+
if (message) this.message = message;
|
|
897
|
+
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
898
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
899
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* Checks whether the given input may have been caused by an APIError.
|
|
903
|
+
*
|
|
904
|
+
* @param input - The input to check.
|
|
905
|
+
*
|
|
906
|
+
* @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`.
|
|
907
|
+
*/
|
|
908
|
+
static check(input) {
|
|
909
|
+
if (input instanceof APIError) return true;
|
|
910
|
+
const data = input;
|
|
911
|
+
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
//#endregion
|
|
915
|
+
//#region src/root/types/VersionNumber.ts
|
|
916
|
+
/**
|
|
917
|
+
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
918
|
+
*
|
|
919
|
+
* @category Types
|
|
920
|
+
*/
|
|
921
|
+
var VersionNumber = class VersionNumber {
|
|
922
|
+
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
923
|
+
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
924
|
+
major = 0;
|
|
925
|
+
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
926
|
+
minor = 0;
|
|
927
|
+
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
928
|
+
patch = 0;
|
|
929
|
+
/**
|
|
930
|
+
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
931
|
+
*/
|
|
932
|
+
constructor(input) {
|
|
933
|
+
if (input instanceof VersionNumber) {
|
|
934
|
+
this.major = input.major;
|
|
935
|
+
this.minor = input.minor;
|
|
936
|
+
this.patch = input.patch;
|
|
937
|
+
} else if (typeof input === "string") {
|
|
938
|
+
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.`);
|
|
939
|
+
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
940
|
+
return parseIntStrict(number);
|
|
941
|
+
});
|
|
942
|
+
this.major = major;
|
|
943
|
+
this.minor = minor;
|
|
944
|
+
this.patch = patch;
|
|
945
|
+
} else if (Array.isArray(input)) {
|
|
946
|
+
if (input.length !== 3) throw new DataError$1({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
947
|
+
const [major, minor, patch] = input.map((number) => {
|
|
948
|
+
const parsedInteger = parseIntStrict(number?.toString());
|
|
949
|
+
if (parsedInteger < 0) throw new DataError$1({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
950
|
+
return parsedInteger;
|
|
951
|
+
});
|
|
952
|
+
this.major = major;
|
|
953
|
+
this.minor = minor;
|
|
954
|
+
this.patch = patch;
|
|
955
|
+
} else throw new DataError$1({ input }, "INVALID_INPUT", normaliseIndents`
|
|
956
|
+
The provided input can not be parsed into a valid version number.
|
|
957
|
+
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
958
|
+
`);
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* Gets the current version type of the current instance of `VersionNumber`.
|
|
962
|
+
*
|
|
963
|
+
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
964
|
+
*/
|
|
965
|
+
get type() {
|
|
966
|
+
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
967
|
+
if (this.patch === 0) return VersionType.MINOR;
|
|
968
|
+
return VersionType.PATCH;
|
|
969
|
+
}
|
|
970
|
+
static formatString(input, options) {
|
|
971
|
+
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
972
|
+
return input.startsWith("v") ? input : `v${input}`;
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
976
|
+
*
|
|
977
|
+
* @param firstVersion - The first version number to compare.
|
|
978
|
+
* @param secondVersion - The second version number to compare.
|
|
979
|
+
*
|
|
980
|
+
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
981
|
+
*/
|
|
982
|
+
static isEqual(firstVersion, secondVersion) {
|
|
983
|
+
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Get a formatted string representation of the current version number
|
|
987
|
+
*
|
|
988
|
+
* @param options - Options to apply to the string formatting.
|
|
989
|
+
*
|
|
990
|
+
* @returns A formatted string representation of the current version number with the options applied.
|
|
991
|
+
*/
|
|
992
|
+
format(options) {
|
|
993
|
+
let baseOutput = `${this.major}`;
|
|
994
|
+
if (!options?.omitMinor) {
|
|
995
|
+
baseOutput += `.${this.minor}`;
|
|
996
|
+
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
997
|
+
}
|
|
998
|
+
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
1002
|
+
*
|
|
1003
|
+
* @param incrementType - The type of increment. Can be one of the following:
|
|
1004
|
+
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
1005
|
+
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
1006
|
+
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
1007
|
+
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
1008
|
+
*
|
|
1009
|
+
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
1010
|
+
*/
|
|
1011
|
+
increment(incrementType, incrementAmount = 1) {
|
|
1012
|
+
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
1013
|
+
const calculatedRawVersion = {
|
|
1014
|
+
major: [
|
|
1015
|
+
this.major + incrementBy,
|
|
1016
|
+
0,
|
|
1017
|
+
0
|
|
1018
|
+
],
|
|
1019
|
+
minor: [
|
|
1020
|
+
this.major,
|
|
1021
|
+
this.minor + incrementBy,
|
|
1022
|
+
0
|
|
1023
|
+
],
|
|
1024
|
+
patch: [
|
|
1025
|
+
this.major,
|
|
1026
|
+
this.minor,
|
|
1027
|
+
this.patch + incrementBy
|
|
1028
|
+
]
|
|
1029
|
+
}[incrementType];
|
|
1030
|
+
try {
|
|
1031
|
+
return new VersionNumber(calculatedRawVersion);
|
|
1032
|
+
} catch (error) {
|
|
1033
|
+
if (DataError$1.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError$1({
|
|
1034
|
+
currentVersion: this.toString(),
|
|
1035
|
+
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
1036
|
+
incrementAmount
|
|
1037
|
+
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
1038
|
+
else throw error;
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
1043
|
+
*
|
|
1044
|
+
* @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).
|
|
1045
|
+
*
|
|
1046
|
+
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1047
|
+
*/
|
|
1048
|
+
[Symbol.toPrimitive](hint) {
|
|
1049
|
+
if (hint === "number") throw new DataError$1({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1050
|
+
return this.toString();
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
1054
|
+
*
|
|
1055
|
+
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1056
|
+
*/
|
|
1057
|
+
toJSON() {
|
|
1058
|
+
return this.toString();
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Get a string representation of the current version number.
|
|
1062
|
+
*
|
|
1063
|
+
* @returns A stringified representation of the current version number with the prefix.
|
|
1064
|
+
*/
|
|
1065
|
+
toString() {
|
|
1066
|
+
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
1067
|
+
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
const zodVersionNumber = z$1.union([
|
|
1071
|
+
z$1.string(),
|
|
1072
|
+
z$1.tuple([
|
|
1073
|
+
z$1.number(),
|
|
1074
|
+
z$1.number(),
|
|
1075
|
+
z$1.number()
|
|
1076
|
+
]),
|
|
1077
|
+
z$1.instanceof(VersionNumber)
|
|
1078
|
+
]).transform((rawVersionNumber) => {
|
|
1079
|
+
return new VersionNumber(rawVersionNumber);
|
|
1080
|
+
});
|
|
1081
|
+
//#endregion
|
|
1082
|
+
//#region src/root/zod/_parseZodSchema.ts
|
|
873
1083
|
function _parseZodSchema(parsedResult, input, onError) {
|
|
874
1084
|
if (!parsedResult.success) {
|
|
875
1085
|
if (onError) {
|
|
@@ -893,7 +1103,7 @@ function _parseZodSchema(parsedResult, input, onError) {
|
|
|
893
1103
|
return parsedResult.data;
|
|
894
1104
|
}
|
|
895
1105
|
//#endregion
|
|
896
|
-
//#region src/root/
|
|
1106
|
+
//#region src/root/zod/parseZodSchema.ts
|
|
897
1107
|
/**
|
|
898
1108
|
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
899
1109
|
*
|
|
@@ -901,6 +1111,8 @@ function _parseZodSchema(parsedResult, input, onError) {
|
|
|
901
1111
|
*
|
|
902
1112
|
* @category Parsers
|
|
903
1113
|
*
|
|
1114
|
+
* @deprecated Please use `az.with(schema).parse(input)` instead.
|
|
1115
|
+
*
|
|
904
1116
|
* @template SchemaType - The Zod schema type.
|
|
905
1117
|
* @template ErrorType - The type of error to throw on invalid data.
|
|
906
1118
|
*
|
|
@@ -916,6 +1128,60 @@ function parseZodSchema(schema, input, onError) {
|
|
|
916
1128
|
return _parseZodSchema(schema.safeParse(input), input, onError);
|
|
917
1129
|
}
|
|
918
1130
|
//#endregion
|
|
1131
|
+
//#region src/root/zod/parseZodSchemaAsync.ts
|
|
1132
|
+
/**
|
|
1133
|
+
* An alternative function to zodSchema.parseAsync() that can be used to strictly parse asynchronous Zod schemas.
|
|
1134
|
+
*
|
|
1135
|
+
* @category Parsers
|
|
1136
|
+
*
|
|
1137
|
+
* @deprecated Please use `az.with(schema).parseAsync(input)` instead.
|
|
1138
|
+
*
|
|
1139
|
+
* @template SchemaType - The Zod schema type.
|
|
1140
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
1141
|
+
*
|
|
1142
|
+
* @param schema - The Zod schema to use in parsing.
|
|
1143
|
+
* @param input - The data to parse.
|
|
1144
|
+
* @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.
|
|
1145
|
+
*
|
|
1146
|
+
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
1147
|
+
*
|
|
1148
|
+
* @returns The parsed data from the Zod schema.
|
|
1149
|
+
*/
|
|
1150
|
+
async function parseZodSchemaAsync(schema, input, onError) {
|
|
1151
|
+
return _parseZodSchema(await schema.safeParseAsync(input), input, onError);
|
|
1152
|
+
}
|
|
1153
|
+
//#endregion
|
|
1154
|
+
//#region src/root/zod/zodFieldWrapper.ts
|
|
1155
|
+
function zodFieldWrapper(schema) {
|
|
1156
|
+
return z$1.string().trim().transform((value) => {
|
|
1157
|
+
return value === "" ? null : value;
|
|
1158
|
+
}).pipe(schema);
|
|
1159
|
+
}
|
|
1160
|
+
//#endregion
|
|
1161
|
+
//#region src/root/zod/az.ts
|
|
1162
|
+
const az = {
|
|
1163
|
+
field: zodFieldWrapper,
|
|
1164
|
+
fieldNumber: () => {
|
|
1165
|
+
return z$1.coerce.number();
|
|
1166
|
+
},
|
|
1167
|
+
versionNumber: () => {
|
|
1168
|
+
return zodVersionNumber;
|
|
1169
|
+
},
|
|
1170
|
+
fieldDate: () => {
|
|
1171
|
+
return z$1.coerce.date();
|
|
1172
|
+
},
|
|
1173
|
+
with: (schema) => {
|
|
1174
|
+
return {
|
|
1175
|
+
parse: (input, error) => {
|
|
1176
|
+
return parseZodSchema(schema, input, error);
|
|
1177
|
+
},
|
|
1178
|
+
parseAsync: async (input, error) => {
|
|
1179
|
+
return await parseZodSchemaAsync(schema, input, error);
|
|
1180
|
+
}
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
//#endregion
|
|
919
1185
|
//#region src/root/functions/parsers/parseEnv.ts
|
|
920
1186
|
/**
|
|
921
1187
|
* Represents the three common development environments.
|
|
@@ -939,7 +1205,7 @@ const Env = {
|
|
|
939
1205
|
* @returns The specified environment if allowed.
|
|
940
1206
|
*/
|
|
941
1207
|
function parseEnv(input) {
|
|
942
|
-
return
|
|
1208
|
+
return az.with(z.enum(Env)).parse(input, new DataError$1({ input }, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
943
1209
|
}
|
|
944
1210
|
//#endregion
|
|
945
1211
|
//#region src/root/functions/parsers/parseFormData.ts
|
|
@@ -1005,28 +1271,7 @@ const VersionType = {
|
|
|
1005
1271
|
* @returns The given version type if allowed.
|
|
1006
1272
|
*/
|
|
1007
1273
|
function parseVersionType(input) {
|
|
1008
|
-
return
|
|
1009
|
-
}
|
|
1010
|
-
//#endregion
|
|
1011
|
-
//#region src/root/functions/parsers/zod/parseZodSchemaAsync.ts
|
|
1012
|
-
/**
|
|
1013
|
-
* An alternative function to zodSchema.parseAsync() that can be used to strictly parse asynchronous Zod schemas.
|
|
1014
|
-
*
|
|
1015
|
-
* @category Parsers
|
|
1016
|
-
*
|
|
1017
|
-
* @template SchemaType - The Zod schema type.
|
|
1018
|
-
* @template ErrorType - The type of error to throw on invalid data.
|
|
1019
|
-
*
|
|
1020
|
-
* @param schema - The Zod schema to use in parsing.
|
|
1021
|
-
* @param input - The data to parse.
|
|
1022
|
-
* @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.
|
|
1023
|
-
*
|
|
1024
|
-
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
1025
|
-
*
|
|
1026
|
-
* @returns The parsed data from the Zod schema.
|
|
1027
|
-
*/
|
|
1028
|
-
async function parseZodSchemaAsync(schema, input, onError) {
|
|
1029
|
-
return _parseZodSchema(await schema.safeParseAsync(input), input, onError);
|
|
1274
|
+
return az.with(z$1.enum(VersionType)).parse(input, new DataError$1({ input }, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
1030
1275
|
}
|
|
1031
1276
|
//#endregion
|
|
1032
1277
|
//#region src/root/functions/recursive/deepCopy.ts
|
|
@@ -1532,214 +1777,4 @@ var DataError = class DataError extends Error {
|
|
|
1532
1777
|
}
|
|
1533
1778
|
};
|
|
1534
1779
|
//#endregion
|
|
1535
|
-
|
|
1536
|
-
const httpErrorCodeLookup = {
|
|
1537
|
-
400: "BAD_REQUEST",
|
|
1538
|
-
401: "UNAUTHORISED",
|
|
1539
|
-
403: "FORBIDDEN",
|
|
1540
|
-
404: "NOT_FOUND",
|
|
1541
|
-
418: "I_AM_A_TEAPOT",
|
|
1542
|
-
500: "INTERNAL_SERVER_ERROR"
|
|
1543
|
-
};
|
|
1544
|
-
/**
|
|
1545
|
-
* Represents common errors you may get from a HTTP API request.
|
|
1546
|
-
*
|
|
1547
|
-
* @category Types
|
|
1548
|
-
*/
|
|
1549
|
-
var APIError = class APIError extends Error {
|
|
1550
|
-
status;
|
|
1551
|
-
/**
|
|
1552
|
-
* @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.
|
|
1553
|
-
* @param message - An error message to display alongside the status code.
|
|
1554
|
-
* @param options - Extra options to be passed to super Error constructor.
|
|
1555
|
-
*/
|
|
1556
|
-
constructor(status = 500, message, options) {
|
|
1557
|
-
super(message, options);
|
|
1558
|
-
this.status = status;
|
|
1559
|
-
if (message) this.message = message;
|
|
1560
|
-
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
1561
|
-
Object.defineProperty(this, "message", { enumerable: true });
|
|
1562
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
1563
|
-
}
|
|
1564
|
-
/**
|
|
1565
|
-
* Checks whether the given input may have been caused by an APIError.
|
|
1566
|
-
*
|
|
1567
|
-
* @param input - The input to check.
|
|
1568
|
-
*
|
|
1569
|
-
* @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`.
|
|
1570
|
-
*/
|
|
1571
|
-
static check(input) {
|
|
1572
|
-
if (input instanceof APIError) return true;
|
|
1573
|
-
const data = input;
|
|
1574
|
-
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
1575
|
-
}
|
|
1576
|
-
};
|
|
1577
|
-
//#endregion
|
|
1578
|
-
//#region src/root/types/VersionNumber.ts
|
|
1579
|
-
/**
|
|
1580
|
-
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
1581
|
-
*
|
|
1582
|
-
* @category Types
|
|
1583
|
-
*/
|
|
1584
|
-
var VersionNumber = class VersionNumber {
|
|
1585
|
-
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
1586
|
-
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
1587
|
-
major = 0;
|
|
1588
|
-
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
1589
|
-
minor = 0;
|
|
1590
|
-
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
1591
|
-
patch = 0;
|
|
1592
|
-
/**
|
|
1593
|
-
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
1594
|
-
*/
|
|
1595
|
-
constructor(input) {
|
|
1596
|
-
if (input instanceof VersionNumber) {
|
|
1597
|
-
this.major = input.major;
|
|
1598
|
-
this.minor = input.minor;
|
|
1599
|
-
this.patch = input.patch;
|
|
1600
|
-
} else if (typeof input === "string") {
|
|
1601
|
-
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError$1({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
1602
|
-
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
1603
|
-
return parseIntStrict(number);
|
|
1604
|
-
});
|
|
1605
|
-
this.major = major;
|
|
1606
|
-
this.minor = minor;
|
|
1607
|
-
this.patch = patch;
|
|
1608
|
-
} else if (Array.isArray(input)) {
|
|
1609
|
-
if (input.length !== 3) throw new DataError$1({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1610
|
-
const [major, minor, patch] = input.map((number) => {
|
|
1611
|
-
const parsedInteger = parseIntStrict(number?.toString());
|
|
1612
|
-
if (parsedInteger < 0) throw new DataError$1({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1613
|
-
return parsedInteger;
|
|
1614
|
-
});
|
|
1615
|
-
this.major = major;
|
|
1616
|
-
this.minor = minor;
|
|
1617
|
-
this.patch = patch;
|
|
1618
|
-
} else throw new DataError$1({ input }, "INVALID_INPUT", normaliseIndents`
|
|
1619
|
-
The provided input can not be parsed into a valid version number.
|
|
1620
|
-
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
1621
|
-
`);
|
|
1622
|
-
}
|
|
1623
|
-
/**
|
|
1624
|
-
* Gets the current version type of the current instance of `VersionNumber`.
|
|
1625
|
-
*
|
|
1626
|
-
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
1627
|
-
*/
|
|
1628
|
-
get type() {
|
|
1629
|
-
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
1630
|
-
if (this.patch === 0) return VersionType.MINOR;
|
|
1631
|
-
return VersionType.PATCH;
|
|
1632
|
-
}
|
|
1633
|
-
static formatString(input, options) {
|
|
1634
|
-
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
1635
|
-
return input.startsWith("v") ? input : `v${input}`;
|
|
1636
|
-
}
|
|
1637
|
-
/**
|
|
1638
|
-
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
1639
|
-
*
|
|
1640
|
-
* @param firstVersion - The first version number to compare.
|
|
1641
|
-
* @param secondVersion - The second version number to compare.
|
|
1642
|
-
*
|
|
1643
|
-
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
1644
|
-
*/
|
|
1645
|
-
static isEqual(firstVersion, secondVersion) {
|
|
1646
|
-
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
1647
|
-
}
|
|
1648
|
-
/**
|
|
1649
|
-
* Get a formatted string representation of the current version number
|
|
1650
|
-
*
|
|
1651
|
-
* @param options - Options to apply to the string formatting.
|
|
1652
|
-
*
|
|
1653
|
-
* @returns A formatted string representation of the current version number with the options applied.
|
|
1654
|
-
*/
|
|
1655
|
-
format(options) {
|
|
1656
|
-
let baseOutput = `${this.major}`;
|
|
1657
|
-
if (!options?.omitMinor) {
|
|
1658
|
-
baseOutput += `.${this.minor}`;
|
|
1659
|
-
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
1660
|
-
}
|
|
1661
|
-
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
1662
|
-
}
|
|
1663
|
-
/**
|
|
1664
|
-
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
1665
|
-
*
|
|
1666
|
-
* @param incrementType - The type of increment. Can be one of the following:
|
|
1667
|
-
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
1668
|
-
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
1669
|
-
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
1670
|
-
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
1671
|
-
*
|
|
1672
|
-
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
1673
|
-
*/
|
|
1674
|
-
increment(incrementType, incrementAmount = 1) {
|
|
1675
|
-
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
1676
|
-
const calculatedRawVersion = {
|
|
1677
|
-
major: [
|
|
1678
|
-
this.major + incrementBy,
|
|
1679
|
-
0,
|
|
1680
|
-
0
|
|
1681
|
-
],
|
|
1682
|
-
minor: [
|
|
1683
|
-
this.major,
|
|
1684
|
-
this.minor + incrementBy,
|
|
1685
|
-
0
|
|
1686
|
-
],
|
|
1687
|
-
patch: [
|
|
1688
|
-
this.major,
|
|
1689
|
-
this.minor,
|
|
1690
|
-
this.patch + incrementBy
|
|
1691
|
-
]
|
|
1692
|
-
}[incrementType];
|
|
1693
|
-
try {
|
|
1694
|
-
return new VersionNumber(calculatedRawVersion);
|
|
1695
|
-
} catch (error) {
|
|
1696
|
-
if (DataError$1.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError$1({
|
|
1697
|
-
currentVersion: this.toString(),
|
|
1698
|
-
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
1699
|
-
incrementAmount
|
|
1700
|
-
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
1701
|
-
else throw error;
|
|
1702
|
-
}
|
|
1703
|
-
}
|
|
1704
|
-
/**
|
|
1705
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
1706
|
-
*
|
|
1707
|
-
* @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).
|
|
1708
|
-
*
|
|
1709
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1710
|
-
*/
|
|
1711
|
-
[Symbol.toPrimitive](hint) {
|
|
1712
|
-
if (hint === "number") throw new DataError$1({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1713
|
-
return this.toString();
|
|
1714
|
-
}
|
|
1715
|
-
/**
|
|
1716
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
1717
|
-
*
|
|
1718
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1719
|
-
*/
|
|
1720
|
-
toJSON() {
|
|
1721
|
-
return this.toString();
|
|
1722
|
-
}
|
|
1723
|
-
/**
|
|
1724
|
-
* Get a string representation of the current version number.
|
|
1725
|
-
*
|
|
1726
|
-
* @returns A stringified representation of the current version number with the prefix.
|
|
1727
|
-
*/
|
|
1728
|
-
toString() {
|
|
1729
|
-
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
1730
|
-
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
1731
|
-
}
|
|
1732
|
-
};
|
|
1733
|
-
const zodVersionNumber = z$1.union([
|
|
1734
|
-
z$1.string(),
|
|
1735
|
-
z$1.tuple([
|
|
1736
|
-
z$1.number(),
|
|
1737
|
-
z$1.number(),
|
|
1738
|
-
z$1.number()
|
|
1739
|
-
]),
|
|
1740
|
-
z$1.instanceof(VersionNumber)
|
|
1741
|
-
]).transform((rawVersionNumber) => {
|
|
1742
|
-
return new VersionNumber(rawVersionNumber);
|
|
1743
|
-
});
|
|
1744
|
-
//#endregion
|
|
1745
|
-
export { APIError, DataError, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, ONE_DAY_IN_MILLISECONDS, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, VersionType, addDaysToDate, appendSemicolon, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|
|
1780
|
+
export { APIError, DataError, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, ONE_DAY_IN_MILLISECONDS, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, VersionType, addDaysToDate, appendSemicolon, az, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|