@alextheman/utility 5.11.0 → 5.11.2
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/internal/index.cjs +0 -219
- package/dist/internal/index.js +0 -219
- package/dist/node/index.cjs +0 -217
- package/dist/node/index.d.ts +0 -1
- package/dist/node/index.js +0 -216
- package/package.json +7 -4
package/dist/internal/index.cjs
CHANGED
|
@@ -171,213 +171,6 @@ var DataError = class DataError extends Error {
|
|
|
171
171
|
}
|
|
172
172
|
};
|
|
173
173
|
//#endregion
|
|
174
|
-
//#region src/root/constants/FILE_PATH_REGEX.ts
|
|
175
|
-
const FILE_PATH_PATTERN = String.raw`(?<directory>.+)[\/\\](?<base>[^\/\\]+)`;
|
|
176
|
-
RegExp(`^${FILE_PATH_PATTERN}$`);
|
|
177
|
-
new RegExp(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`);
|
|
178
|
-
//#endregion
|
|
179
|
-
//#region src/root/constants/VERSION_NUMBER_REGEX.ts
|
|
180
|
-
const VERSION_NUMBER_PATTERN = String.raw`^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
|
|
181
|
-
const VERSION_NUMBER_REGEX = RegExp(`^${VERSION_NUMBER_PATTERN}$`);
|
|
182
|
-
//#endregion
|
|
183
|
-
//#region src/root/types/VersionNumber.ts
|
|
184
|
-
/**
|
|
185
|
-
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
186
|
-
*
|
|
187
|
-
* @category Types
|
|
188
|
-
*/
|
|
189
|
-
var VersionNumber = class VersionNumber {
|
|
190
|
-
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
191
|
-
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
192
|
-
major = 0;
|
|
193
|
-
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
194
|
-
minor = 0;
|
|
195
|
-
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
196
|
-
patch = 0;
|
|
197
|
-
/**
|
|
198
|
-
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
199
|
-
*/
|
|
200
|
-
constructor(input) {
|
|
201
|
-
if (input instanceof VersionNumber) {
|
|
202
|
-
this.major = input.major;
|
|
203
|
-
this.minor = input.minor;
|
|
204
|
-
this.patch = input.patch;
|
|
205
|
-
} else if (typeof input === "string") {
|
|
206
|
-
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
207
|
-
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
208
|
-
return parseIntStrict(number);
|
|
209
|
-
});
|
|
210
|
-
this.major = major;
|
|
211
|
-
this.minor = minor;
|
|
212
|
-
this.patch = patch;
|
|
213
|
-
} else if (Array.isArray(input)) {
|
|
214
|
-
if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
215
|
-
const [major, minor, patch] = input.map((number) => {
|
|
216
|
-
const parsedInteger = parseIntStrict(number?.toString());
|
|
217
|
-
if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
218
|
-
return parsedInteger;
|
|
219
|
-
});
|
|
220
|
-
this.major = major;
|
|
221
|
-
this.minor = minor;
|
|
222
|
-
this.patch = patch;
|
|
223
|
-
} else throw new DataError({ input }, "INVALID_INPUT", normaliseIndents`
|
|
224
|
-
The provided input can not be parsed into a valid version number.
|
|
225
|
-
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
226
|
-
`);
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Gets the current version type of the current instance of `VersionNumber`.
|
|
230
|
-
*
|
|
231
|
-
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
232
|
-
*/
|
|
233
|
-
get type() {
|
|
234
|
-
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
235
|
-
if (this.patch === 0) return VersionType.MINOR;
|
|
236
|
-
return VersionType.PATCH;
|
|
237
|
-
}
|
|
238
|
-
static formatString(input, options) {
|
|
239
|
-
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
240
|
-
return input.startsWith("v") ? input : `v${input}`;
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
244
|
-
*
|
|
245
|
-
* @param firstVersion - The first version number to compare.
|
|
246
|
-
* @param secondVersion - The second version number to compare.
|
|
247
|
-
*
|
|
248
|
-
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
249
|
-
*/
|
|
250
|
-
static isEqual(firstVersion, secondVersion) {
|
|
251
|
-
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Get a formatted string representation of the current version number
|
|
255
|
-
*
|
|
256
|
-
* @param options - Options to apply to the string formatting.
|
|
257
|
-
*
|
|
258
|
-
* @returns A formatted string representation of the current version number with the options applied.
|
|
259
|
-
*/
|
|
260
|
-
format(options) {
|
|
261
|
-
let baseOutput = `${this.major}`;
|
|
262
|
-
if (!options?.omitMinor) {
|
|
263
|
-
baseOutput += `.${this.minor}`;
|
|
264
|
-
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
265
|
-
}
|
|
266
|
-
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
270
|
-
*
|
|
271
|
-
* @param incrementType - The type of increment. Can be one of the following:
|
|
272
|
-
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
273
|
-
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
274
|
-
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
275
|
-
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
276
|
-
*
|
|
277
|
-
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
278
|
-
*/
|
|
279
|
-
increment(incrementType, incrementAmount = 1) {
|
|
280
|
-
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
281
|
-
const calculatedRawVersion = {
|
|
282
|
-
major: [
|
|
283
|
-
this.major + incrementBy,
|
|
284
|
-
0,
|
|
285
|
-
0
|
|
286
|
-
],
|
|
287
|
-
minor: [
|
|
288
|
-
this.major,
|
|
289
|
-
this.minor + incrementBy,
|
|
290
|
-
0
|
|
291
|
-
],
|
|
292
|
-
patch: [
|
|
293
|
-
this.major,
|
|
294
|
-
this.minor,
|
|
295
|
-
this.patch + incrementBy
|
|
296
|
-
]
|
|
297
|
-
}[incrementType];
|
|
298
|
-
try {
|
|
299
|
-
return new VersionNumber(calculatedRawVersion);
|
|
300
|
-
} catch (error) {
|
|
301
|
-
if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
|
|
302
|
-
currentVersion: this.toString(),
|
|
303
|
-
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
304
|
-
incrementAmount
|
|
305
|
-
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
306
|
-
else throw error;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
311
|
-
*
|
|
312
|
-
* @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).
|
|
313
|
-
*
|
|
314
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
315
|
-
*/
|
|
316
|
-
[Symbol.toPrimitive](hint) {
|
|
317
|
-
if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
318
|
-
return this.toString();
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
322
|
-
*
|
|
323
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
324
|
-
*/
|
|
325
|
-
toJSON() {
|
|
326
|
-
return this.toString();
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Get a string representation of the current version number.
|
|
330
|
-
*
|
|
331
|
-
* @returns A stringified representation of the current version number with the prefix.
|
|
332
|
-
*/
|
|
333
|
-
toString() {
|
|
334
|
-
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
335
|
-
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
zod.default.union([
|
|
339
|
-
zod.default.string(),
|
|
340
|
-
zod.default.tuple([
|
|
341
|
-
zod.default.number(),
|
|
342
|
-
zod.default.number(),
|
|
343
|
-
zod.default.number()
|
|
344
|
-
]),
|
|
345
|
-
zod.default.instanceof(VersionNumber)
|
|
346
|
-
]).transform((rawVersionNumber) => {
|
|
347
|
-
return new VersionNumber(rawVersionNumber);
|
|
348
|
-
});
|
|
349
|
-
//#endregion
|
|
350
|
-
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
351
|
-
/**
|
|
352
|
-
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
353
|
-
*
|
|
354
|
-
* @category Parsers
|
|
355
|
-
*
|
|
356
|
-
* @param string - A string to convert into a number.
|
|
357
|
-
* @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.
|
|
358
|
-
*
|
|
359
|
-
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
360
|
-
*
|
|
361
|
-
* @returns The integer parsed from the input string.
|
|
362
|
-
*/
|
|
363
|
-
function parseIntStrict(string, radix) {
|
|
364
|
-
const trimmedString = string.trim();
|
|
365
|
-
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
366
|
-
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError(radix ? {
|
|
367
|
-
string,
|
|
368
|
-
radix
|
|
369
|
-
} : { string }, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
370
|
-
if (radix && radix < 10 && [...trimmedString.replace(/^[+-]/, "")].some((character) => {
|
|
371
|
-
return parseInt(character) >= radix;
|
|
372
|
-
})) throw new DataError({
|
|
373
|
-
string,
|
|
374
|
-
radix
|
|
375
|
-
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
376
|
-
const parseIntResult = parseInt(trimmedString, radix);
|
|
377
|
-
if (isNaN(parseIntResult)) throw new DataError({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
378
|
-
return parseIntResult;
|
|
379
|
-
}
|
|
380
|
-
//#endregion
|
|
381
174
|
//#region src/root/functions/taggedTemplate/interpolate.ts
|
|
382
175
|
/**
|
|
383
176
|
* Returns the result of interpolating a template string when given the strings and interpolations separately.
|
|
@@ -628,18 +421,6 @@ function parseZodSchema(schema, input, onError) {
|
|
|
628
421
|
return _parseZodSchema(schema.safeParse(input), input, onError);
|
|
629
422
|
}
|
|
630
423
|
//#endregion
|
|
631
|
-
//#region src/root/functions/parsers/parseVersionType.ts
|
|
632
|
-
/**
|
|
633
|
-
* Represents the three common software version types.
|
|
634
|
-
*
|
|
635
|
-
* @category Types
|
|
636
|
-
*/
|
|
637
|
-
const VersionType = {
|
|
638
|
-
MAJOR: "major",
|
|
639
|
-
MINOR: "minor",
|
|
640
|
-
PATCH: "patch"
|
|
641
|
-
};
|
|
642
|
-
//#endregion
|
|
643
424
|
//#region src/internal/getDependenciesFromGroup.ts
|
|
644
425
|
/**
|
|
645
426
|
* Get the dependencies from a given dependency group in `package.json`.
|
package/dist/internal/index.js
CHANGED
|
@@ -146,213 +146,6 @@ var DataError = class DataError extends Error {
|
|
|
146
146
|
}
|
|
147
147
|
};
|
|
148
148
|
//#endregion
|
|
149
|
-
//#region src/root/constants/FILE_PATH_REGEX.ts
|
|
150
|
-
const FILE_PATH_PATTERN = String.raw`(?<directory>.+)[\/\\](?<base>[^\/\\]+)`;
|
|
151
|
-
RegExp(`^${FILE_PATH_PATTERN}$`);
|
|
152
|
-
new RegExp(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`);
|
|
153
|
-
//#endregion
|
|
154
|
-
//#region src/root/constants/VERSION_NUMBER_REGEX.ts
|
|
155
|
-
const VERSION_NUMBER_PATTERN = String.raw`^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
|
|
156
|
-
const VERSION_NUMBER_REGEX = RegExp(`^${VERSION_NUMBER_PATTERN}$`);
|
|
157
|
-
//#endregion
|
|
158
|
-
//#region src/root/types/VersionNumber.ts
|
|
159
|
-
/**
|
|
160
|
-
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
161
|
-
*
|
|
162
|
-
* @category Types
|
|
163
|
-
*/
|
|
164
|
-
var VersionNumber = class VersionNumber {
|
|
165
|
-
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
166
|
-
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
167
|
-
major = 0;
|
|
168
|
-
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
169
|
-
minor = 0;
|
|
170
|
-
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
171
|
-
patch = 0;
|
|
172
|
-
/**
|
|
173
|
-
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
174
|
-
*/
|
|
175
|
-
constructor(input) {
|
|
176
|
-
if (input instanceof VersionNumber) {
|
|
177
|
-
this.major = input.major;
|
|
178
|
-
this.minor = input.minor;
|
|
179
|
-
this.patch = input.patch;
|
|
180
|
-
} else if (typeof input === "string") {
|
|
181
|
-
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
182
|
-
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
183
|
-
return parseIntStrict(number);
|
|
184
|
-
});
|
|
185
|
-
this.major = major;
|
|
186
|
-
this.minor = minor;
|
|
187
|
-
this.patch = patch;
|
|
188
|
-
} else if (Array.isArray(input)) {
|
|
189
|
-
if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
190
|
-
const [major, minor, patch] = input.map((number) => {
|
|
191
|
-
const parsedInteger = parseIntStrict(number?.toString());
|
|
192
|
-
if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
193
|
-
return parsedInteger;
|
|
194
|
-
});
|
|
195
|
-
this.major = major;
|
|
196
|
-
this.minor = minor;
|
|
197
|
-
this.patch = patch;
|
|
198
|
-
} else throw new DataError({ input }, "INVALID_INPUT", normaliseIndents`
|
|
199
|
-
The provided input can not be parsed into a valid version number.
|
|
200
|
-
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
201
|
-
`);
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Gets the current version type of the current instance of `VersionNumber`.
|
|
205
|
-
*
|
|
206
|
-
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
207
|
-
*/
|
|
208
|
-
get type() {
|
|
209
|
-
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
210
|
-
if (this.patch === 0) return VersionType.MINOR;
|
|
211
|
-
return VersionType.PATCH;
|
|
212
|
-
}
|
|
213
|
-
static formatString(input, options) {
|
|
214
|
-
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
215
|
-
return input.startsWith("v") ? input : `v${input}`;
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
219
|
-
*
|
|
220
|
-
* @param firstVersion - The first version number to compare.
|
|
221
|
-
* @param secondVersion - The second version number to compare.
|
|
222
|
-
*
|
|
223
|
-
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
224
|
-
*/
|
|
225
|
-
static isEqual(firstVersion, secondVersion) {
|
|
226
|
-
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Get a formatted string representation of the current version number
|
|
230
|
-
*
|
|
231
|
-
* @param options - Options to apply to the string formatting.
|
|
232
|
-
*
|
|
233
|
-
* @returns A formatted string representation of the current version number with the options applied.
|
|
234
|
-
*/
|
|
235
|
-
format(options) {
|
|
236
|
-
let baseOutput = `${this.major}`;
|
|
237
|
-
if (!options?.omitMinor) {
|
|
238
|
-
baseOutput += `.${this.minor}`;
|
|
239
|
-
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
240
|
-
}
|
|
241
|
-
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
245
|
-
*
|
|
246
|
-
* @param incrementType - The type of increment. Can be one of the following:
|
|
247
|
-
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
248
|
-
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
249
|
-
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
250
|
-
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
251
|
-
*
|
|
252
|
-
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
253
|
-
*/
|
|
254
|
-
increment(incrementType, incrementAmount = 1) {
|
|
255
|
-
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
256
|
-
const calculatedRawVersion = {
|
|
257
|
-
major: [
|
|
258
|
-
this.major + incrementBy,
|
|
259
|
-
0,
|
|
260
|
-
0
|
|
261
|
-
],
|
|
262
|
-
minor: [
|
|
263
|
-
this.major,
|
|
264
|
-
this.minor + incrementBy,
|
|
265
|
-
0
|
|
266
|
-
],
|
|
267
|
-
patch: [
|
|
268
|
-
this.major,
|
|
269
|
-
this.minor,
|
|
270
|
-
this.patch + incrementBy
|
|
271
|
-
]
|
|
272
|
-
}[incrementType];
|
|
273
|
-
try {
|
|
274
|
-
return new VersionNumber(calculatedRawVersion);
|
|
275
|
-
} catch (error) {
|
|
276
|
-
if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
|
|
277
|
-
currentVersion: this.toString(),
|
|
278
|
-
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
279
|
-
incrementAmount
|
|
280
|
-
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
281
|
-
else throw error;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
286
|
-
*
|
|
287
|
-
* @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).
|
|
288
|
-
*
|
|
289
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
290
|
-
*/
|
|
291
|
-
[Symbol.toPrimitive](hint) {
|
|
292
|
-
if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
293
|
-
return this.toString();
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
297
|
-
*
|
|
298
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
299
|
-
*/
|
|
300
|
-
toJSON() {
|
|
301
|
-
return this.toString();
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Get a string representation of the current version number.
|
|
305
|
-
*
|
|
306
|
-
* @returns A stringified representation of the current version number with the prefix.
|
|
307
|
-
*/
|
|
308
|
-
toString() {
|
|
309
|
-
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
310
|
-
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
311
|
-
}
|
|
312
|
-
};
|
|
313
|
-
z.union([
|
|
314
|
-
z.string(),
|
|
315
|
-
z.tuple([
|
|
316
|
-
z.number(),
|
|
317
|
-
z.number(),
|
|
318
|
-
z.number()
|
|
319
|
-
]),
|
|
320
|
-
z.instanceof(VersionNumber)
|
|
321
|
-
]).transform((rawVersionNumber) => {
|
|
322
|
-
return new VersionNumber(rawVersionNumber);
|
|
323
|
-
});
|
|
324
|
-
//#endregion
|
|
325
|
-
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
326
|
-
/**
|
|
327
|
-
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
328
|
-
*
|
|
329
|
-
* @category Parsers
|
|
330
|
-
*
|
|
331
|
-
* @param string - A string to convert into a number.
|
|
332
|
-
* @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.
|
|
333
|
-
*
|
|
334
|
-
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
335
|
-
*
|
|
336
|
-
* @returns The integer parsed from the input string.
|
|
337
|
-
*/
|
|
338
|
-
function parseIntStrict(string, radix) {
|
|
339
|
-
const trimmedString = string.trim();
|
|
340
|
-
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
341
|
-
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError(radix ? {
|
|
342
|
-
string,
|
|
343
|
-
radix
|
|
344
|
-
} : { string }, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
345
|
-
if (radix && radix < 10 && [...trimmedString.replace(/^[+-]/, "")].some((character) => {
|
|
346
|
-
return parseInt(character) >= radix;
|
|
347
|
-
})) throw new DataError({
|
|
348
|
-
string,
|
|
349
|
-
radix
|
|
350
|
-
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
351
|
-
const parseIntResult = parseInt(trimmedString, radix);
|
|
352
|
-
if (isNaN(parseIntResult)) throw new DataError({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
353
|
-
return parseIntResult;
|
|
354
|
-
}
|
|
355
|
-
//#endregion
|
|
356
149
|
//#region src/root/functions/taggedTemplate/interpolate.ts
|
|
357
150
|
/**
|
|
358
151
|
* Returns the result of interpolating a template string when given the strings and interpolations separately.
|
|
@@ -603,18 +396,6 @@ function parseZodSchema(schema, input, onError) {
|
|
|
603
396
|
return _parseZodSchema(schema.safeParse(input), input, onError);
|
|
604
397
|
}
|
|
605
398
|
//#endregion
|
|
606
|
-
//#region src/root/functions/parsers/parseVersionType.ts
|
|
607
|
-
/**
|
|
608
|
-
* Represents the three common software version types.
|
|
609
|
-
*
|
|
610
|
-
* @category Types
|
|
611
|
-
*/
|
|
612
|
-
const VersionType = {
|
|
613
|
-
MAJOR: "major",
|
|
614
|
-
MINOR: "minor",
|
|
615
|
-
PATCH: "patch"
|
|
616
|
-
};
|
|
617
|
-
//#endregion
|
|
618
399
|
//#region src/internal/getDependenciesFromGroup.ts
|
|
619
400
|
/**
|
|
620
401
|
* Get the dependencies from a given dependency group in `package.json`.
|
package/dist/node/index.cjs
CHANGED
|
@@ -23,8 +23,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
//#endregion
|
|
24
24
|
let node_path = require("node:path");
|
|
25
25
|
node_path = __toESM(node_path);
|
|
26
|
-
let zod = require("zod");
|
|
27
|
-
zod = __toESM(zod);
|
|
28
26
|
//#region src/node/functions/normalizeImportPath.ts
|
|
29
27
|
/**
|
|
30
28
|
* Normalizes an import path meant for use in an import statement in JavaScript.
|
|
@@ -64,11 +62,6 @@ const normaliseImportPath = normalizeImportPath;
|
|
|
64
62
|
//#region src/root/constants/FILE_PATH_REGEX.ts
|
|
65
63
|
const FILE_PATH_PATTERN = String.raw`(?<directory>.+)[\/\\](?<base>[^\/\\]+)`;
|
|
66
64
|
const FILE_PATH_REGEX = RegExp(`^${FILE_PATH_PATTERN}$`);
|
|
67
|
-
new RegExp(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`);
|
|
68
|
-
//#endregion
|
|
69
|
-
//#region src/root/constants/VERSION_NUMBER_REGEX.ts
|
|
70
|
-
const VERSION_NUMBER_PATTERN = String.raw`^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
|
|
71
|
-
const VERSION_NUMBER_REGEX = RegExp(`^${VERSION_NUMBER_PATTERN}$`);
|
|
72
65
|
//#endregion
|
|
73
66
|
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
74
67
|
/**
|
|
@@ -119,37 +112,6 @@ function paralleliseArrays(firstArray, secondArray) {
|
|
|
119
112
|
return outputArray;
|
|
120
113
|
}
|
|
121
114
|
//#endregion
|
|
122
|
-
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
123
|
-
/**
|
|
124
|
-
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
125
|
-
*
|
|
126
|
-
* @category Parsers
|
|
127
|
-
*
|
|
128
|
-
* @param string - A string to convert into a number.
|
|
129
|
-
* @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.
|
|
130
|
-
*
|
|
131
|
-
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
132
|
-
*
|
|
133
|
-
* @returns The integer parsed from the input string.
|
|
134
|
-
*/
|
|
135
|
-
function parseIntStrict(string, radix) {
|
|
136
|
-
const trimmedString = string.trim();
|
|
137
|
-
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
138
|
-
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError(radix ? {
|
|
139
|
-
string,
|
|
140
|
-
radix
|
|
141
|
-
} : { string }, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
142
|
-
if (radix && radix < 10 && [...trimmedString.replace(/^[+-]/, "")].some((character) => {
|
|
143
|
-
return parseInt(character) >= radix;
|
|
144
|
-
})) throw new DataError({
|
|
145
|
-
string,
|
|
146
|
-
radix
|
|
147
|
-
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
148
|
-
const parseIntResult = parseInt(trimmedString, radix);
|
|
149
|
-
if (isNaN(parseIntResult)) throw new DataError({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
150
|
-
return parseIntResult;
|
|
151
|
-
}
|
|
152
|
-
//#endregion
|
|
153
115
|
//#region src/root/functions/taggedTemplate/interpolate.ts
|
|
154
116
|
/**
|
|
155
117
|
* Returns the result of interpolating a template string when given the strings and interpolations separately.
|
|
@@ -353,18 +315,6 @@ function sayHello() {
|
|
|
353
315
|
`;
|
|
354
316
|
}
|
|
355
317
|
//#endregion
|
|
356
|
-
//#region src/root/functions/parsers/parseVersionType.ts
|
|
357
|
-
/**
|
|
358
|
-
* Represents the three common software version types.
|
|
359
|
-
*
|
|
360
|
-
* @category Types
|
|
361
|
-
*/
|
|
362
|
-
const VersionType = {
|
|
363
|
-
MAJOR: "major",
|
|
364
|
-
MINOR: "minor",
|
|
365
|
-
PATCH: "patch"
|
|
366
|
-
};
|
|
367
|
-
//#endregion
|
|
368
318
|
//#region src/root/types/DataError.ts
|
|
369
319
|
/**
|
|
370
320
|
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
@@ -454,173 +404,6 @@ var DataError = class DataError extends Error {
|
|
|
454
404
|
}
|
|
455
405
|
};
|
|
456
406
|
//#endregion
|
|
457
|
-
//#region src/root/types/VersionNumber.ts
|
|
458
|
-
/**
|
|
459
|
-
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
460
|
-
*
|
|
461
|
-
* @category Types
|
|
462
|
-
*/
|
|
463
|
-
var VersionNumber = class VersionNumber {
|
|
464
|
-
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
465
|
-
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
466
|
-
major = 0;
|
|
467
|
-
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
468
|
-
minor = 0;
|
|
469
|
-
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
470
|
-
patch = 0;
|
|
471
|
-
/**
|
|
472
|
-
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
473
|
-
*/
|
|
474
|
-
constructor(input) {
|
|
475
|
-
if (input instanceof VersionNumber) {
|
|
476
|
-
this.major = input.major;
|
|
477
|
-
this.minor = input.minor;
|
|
478
|
-
this.patch = input.patch;
|
|
479
|
-
} else if (typeof input === "string") {
|
|
480
|
-
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
481
|
-
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
482
|
-
return parseIntStrict(number);
|
|
483
|
-
});
|
|
484
|
-
this.major = major;
|
|
485
|
-
this.minor = minor;
|
|
486
|
-
this.patch = patch;
|
|
487
|
-
} else if (Array.isArray(input)) {
|
|
488
|
-
if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
489
|
-
const [major, minor, patch] = input.map((number) => {
|
|
490
|
-
const parsedInteger = parseIntStrict(number?.toString());
|
|
491
|
-
if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
492
|
-
return parsedInteger;
|
|
493
|
-
});
|
|
494
|
-
this.major = major;
|
|
495
|
-
this.minor = minor;
|
|
496
|
-
this.patch = patch;
|
|
497
|
-
} else throw new DataError({ input }, "INVALID_INPUT", normaliseIndents`
|
|
498
|
-
The provided input can not be parsed into a valid version number.
|
|
499
|
-
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
500
|
-
`);
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* Gets the current version type of the current instance of `VersionNumber`.
|
|
504
|
-
*
|
|
505
|
-
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
506
|
-
*/
|
|
507
|
-
get type() {
|
|
508
|
-
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
509
|
-
if (this.patch === 0) return VersionType.MINOR;
|
|
510
|
-
return VersionType.PATCH;
|
|
511
|
-
}
|
|
512
|
-
static formatString(input, options) {
|
|
513
|
-
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
514
|
-
return input.startsWith("v") ? input : `v${input}`;
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
518
|
-
*
|
|
519
|
-
* @param firstVersion - The first version number to compare.
|
|
520
|
-
* @param secondVersion - The second version number to compare.
|
|
521
|
-
*
|
|
522
|
-
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
523
|
-
*/
|
|
524
|
-
static isEqual(firstVersion, secondVersion) {
|
|
525
|
-
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
526
|
-
}
|
|
527
|
-
/**
|
|
528
|
-
* Get a formatted string representation of the current version number
|
|
529
|
-
*
|
|
530
|
-
* @param options - Options to apply to the string formatting.
|
|
531
|
-
*
|
|
532
|
-
* @returns A formatted string representation of the current version number with the options applied.
|
|
533
|
-
*/
|
|
534
|
-
format(options) {
|
|
535
|
-
let baseOutput = `${this.major}`;
|
|
536
|
-
if (!options?.omitMinor) {
|
|
537
|
-
baseOutput += `.${this.minor}`;
|
|
538
|
-
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
539
|
-
}
|
|
540
|
-
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
544
|
-
*
|
|
545
|
-
* @param incrementType - The type of increment. Can be one of the following:
|
|
546
|
-
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
547
|
-
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
548
|
-
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
549
|
-
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
550
|
-
*
|
|
551
|
-
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
552
|
-
*/
|
|
553
|
-
increment(incrementType, incrementAmount = 1) {
|
|
554
|
-
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
555
|
-
const calculatedRawVersion = {
|
|
556
|
-
major: [
|
|
557
|
-
this.major + incrementBy,
|
|
558
|
-
0,
|
|
559
|
-
0
|
|
560
|
-
],
|
|
561
|
-
minor: [
|
|
562
|
-
this.major,
|
|
563
|
-
this.minor + incrementBy,
|
|
564
|
-
0
|
|
565
|
-
],
|
|
566
|
-
patch: [
|
|
567
|
-
this.major,
|
|
568
|
-
this.minor,
|
|
569
|
-
this.patch + incrementBy
|
|
570
|
-
]
|
|
571
|
-
}[incrementType];
|
|
572
|
-
try {
|
|
573
|
-
return new VersionNumber(calculatedRawVersion);
|
|
574
|
-
} catch (error) {
|
|
575
|
-
if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
|
|
576
|
-
currentVersion: this.toString(),
|
|
577
|
-
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
578
|
-
incrementAmount
|
|
579
|
-
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
580
|
-
else throw error;
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
/**
|
|
584
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
585
|
-
*
|
|
586
|
-
* @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).
|
|
587
|
-
*
|
|
588
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
589
|
-
*/
|
|
590
|
-
[Symbol.toPrimitive](hint) {
|
|
591
|
-
if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
592
|
-
return this.toString();
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
596
|
-
*
|
|
597
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
598
|
-
*/
|
|
599
|
-
toJSON() {
|
|
600
|
-
return this.toString();
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* Get a string representation of the current version number.
|
|
604
|
-
*
|
|
605
|
-
* @returns A stringified representation of the current version number with the prefix.
|
|
606
|
-
*/
|
|
607
|
-
toString() {
|
|
608
|
-
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
609
|
-
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
610
|
-
}
|
|
611
|
-
};
|
|
612
|
-
zod.default.union([
|
|
613
|
-
zod.default.string(),
|
|
614
|
-
zod.default.tuple([
|
|
615
|
-
zod.default.number(),
|
|
616
|
-
zod.default.number(),
|
|
617
|
-
zod.default.number()
|
|
618
|
-
]),
|
|
619
|
-
zod.default.instanceof(VersionNumber)
|
|
620
|
-
]).transform((rawVersionNumber) => {
|
|
621
|
-
return new VersionNumber(rawVersionNumber);
|
|
622
|
-
});
|
|
623
|
-
//#endregion
|
|
624
407
|
//#region src/node/functions/parseFilePath.ts
|
|
625
408
|
/**
|
|
626
409
|
* Takes a file path string and parses it into the directory part, the base part, and the full path.
|
package/dist/node/index.d.ts
CHANGED
package/dist/node/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
-
import z from "zod";
|
|
3
2
|
//#region src/node/functions/normalizeImportPath.ts
|
|
4
3
|
/**
|
|
5
4
|
* Normalizes an import path meant for use in an import statement in JavaScript.
|
|
@@ -39,11 +38,6 @@ const normaliseImportPath = normalizeImportPath;
|
|
|
39
38
|
//#region src/root/constants/FILE_PATH_REGEX.ts
|
|
40
39
|
const FILE_PATH_PATTERN = String.raw`(?<directory>.+)[\/\\](?<base>[^\/\\]+)`;
|
|
41
40
|
const FILE_PATH_REGEX = RegExp(`^${FILE_PATH_PATTERN}$`);
|
|
42
|
-
new RegExp(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`);
|
|
43
|
-
//#endregion
|
|
44
|
-
//#region src/root/constants/VERSION_NUMBER_REGEX.ts
|
|
45
|
-
const VERSION_NUMBER_PATTERN = String.raw`^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
|
|
46
|
-
const VERSION_NUMBER_REGEX = RegExp(`^${VERSION_NUMBER_PATTERN}$`);
|
|
47
41
|
//#endregion
|
|
48
42
|
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
49
43
|
/**
|
|
@@ -94,37 +88,6 @@ function paralleliseArrays(firstArray, secondArray) {
|
|
|
94
88
|
return outputArray;
|
|
95
89
|
}
|
|
96
90
|
//#endregion
|
|
97
|
-
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
98
|
-
/**
|
|
99
|
-
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
100
|
-
*
|
|
101
|
-
* @category Parsers
|
|
102
|
-
*
|
|
103
|
-
* @param string - A string to convert into a number.
|
|
104
|
-
* @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.
|
|
105
|
-
*
|
|
106
|
-
* @throws {DataError} If the provided string cannot safely be converted to an integer.
|
|
107
|
-
*
|
|
108
|
-
* @returns The integer parsed from the input string.
|
|
109
|
-
*/
|
|
110
|
-
function parseIntStrict(string, radix) {
|
|
111
|
-
const trimmedString = string.trim();
|
|
112
|
-
const maxAllowedAlphabeticalCharacter = radix && radix > 10 && radix <= 36 ? String.fromCharCode(87 + radix - 1) : void 0;
|
|
113
|
-
if (!(radix && radix > 10 && radix <= 36 ? new RegExp(`^[+-]?[0-9a-${maxAllowedAlphabeticalCharacter}]+$`, "i") : /^[+-]?\d+$/).test(trimmedString)) throw new DataError(radix ? {
|
|
114
|
-
string,
|
|
115
|
-
radix
|
|
116
|
-
} : { string }, "INTEGER_PARSING_ERROR", `Only numeric values${radix && radix > 10 && radix <= 36 ? ` or character${radix !== 11 ? "s" : ""} A${radix !== 11 ? `-${maxAllowedAlphabeticalCharacter?.toUpperCase()} ` : " "}` : " "}are allowed.`);
|
|
117
|
-
if (radix && radix < 10 && [...trimmedString.replace(/^[+-]/, "")].some((character) => {
|
|
118
|
-
return parseInt(character) >= radix;
|
|
119
|
-
})) throw new DataError({
|
|
120
|
-
string,
|
|
121
|
-
radix
|
|
122
|
-
}, "INTEGER_PARSING_ERROR", "Value contains one or more digits outside of the range of the given radix.");
|
|
123
|
-
const parseIntResult = parseInt(trimmedString, radix);
|
|
124
|
-
if (isNaN(parseIntResult)) throw new DataError({ string }, "INTEGER_PARSING_ERROR", "Value is not a valid integer.");
|
|
125
|
-
return parseIntResult;
|
|
126
|
-
}
|
|
127
|
-
//#endregion
|
|
128
91
|
//#region src/root/functions/taggedTemplate/interpolate.ts
|
|
129
92
|
/**
|
|
130
93
|
* Returns the result of interpolating a template string when given the strings and interpolations separately.
|
|
@@ -328,18 +291,6 @@ function sayHello() {
|
|
|
328
291
|
`;
|
|
329
292
|
}
|
|
330
293
|
//#endregion
|
|
331
|
-
//#region src/root/functions/parsers/parseVersionType.ts
|
|
332
|
-
/**
|
|
333
|
-
* Represents the three common software version types.
|
|
334
|
-
*
|
|
335
|
-
* @category Types
|
|
336
|
-
*/
|
|
337
|
-
const VersionType = {
|
|
338
|
-
MAJOR: "major",
|
|
339
|
-
MINOR: "minor",
|
|
340
|
-
PATCH: "patch"
|
|
341
|
-
};
|
|
342
|
-
//#endregion
|
|
343
294
|
//#region src/root/types/DataError.ts
|
|
344
295
|
/**
|
|
345
296
|
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
@@ -429,173 +380,6 @@ var DataError = class DataError extends Error {
|
|
|
429
380
|
}
|
|
430
381
|
};
|
|
431
382
|
//#endregion
|
|
432
|
-
//#region src/root/types/VersionNumber.ts
|
|
433
|
-
/**
|
|
434
|
-
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
435
|
-
*
|
|
436
|
-
* @category Types
|
|
437
|
-
*/
|
|
438
|
-
var VersionNumber = class VersionNumber {
|
|
439
|
-
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
440
|
-
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
441
|
-
major = 0;
|
|
442
|
-
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
443
|
-
minor = 0;
|
|
444
|
-
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
445
|
-
patch = 0;
|
|
446
|
-
/**
|
|
447
|
-
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
448
|
-
*/
|
|
449
|
-
constructor(input) {
|
|
450
|
-
if (input instanceof VersionNumber) {
|
|
451
|
-
this.major = input.major;
|
|
452
|
-
this.minor = input.minor;
|
|
453
|
-
this.patch = input.patch;
|
|
454
|
-
} else if (typeof input === "string") {
|
|
455
|
-
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
456
|
-
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
457
|
-
return parseIntStrict(number);
|
|
458
|
-
});
|
|
459
|
-
this.major = major;
|
|
460
|
-
this.minor = minor;
|
|
461
|
-
this.patch = patch;
|
|
462
|
-
} else if (Array.isArray(input)) {
|
|
463
|
-
if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
464
|
-
const [major, minor, patch] = input.map((number) => {
|
|
465
|
-
const parsedInteger = parseIntStrict(number?.toString());
|
|
466
|
-
if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
467
|
-
return parsedInteger;
|
|
468
|
-
});
|
|
469
|
-
this.major = major;
|
|
470
|
-
this.minor = minor;
|
|
471
|
-
this.patch = patch;
|
|
472
|
-
} else throw new DataError({ input }, "INVALID_INPUT", normaliseIndents`
|
|
473
|
-
The provided input can not be parsed into a valid version number.
|
|
474
|
-
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
475
|
-
`);
|
|
476
|
-
}
|
|
477
|
-
/**
|
|
478
|
-
* Gets the current version type of the current instance of `VersionNumber`.
|
|
479
|
-
*
|
|
480
|
-
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
481
|
-
*/
|
|
482
|
-
get type() {
|
|
483
|
-
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
484
|
-
if (this.patch === 0) return VersionType.MINOR;
|
|
485
|
-
return VersionType.PATCH;
|
|
486
|
-
}
|
|
487
|
-
static formatString(input, options) {
|
|
488
|
-
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
489
|
-
return input.startsWith("v") ? input : `v${input}`;
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
493
|
-
*
|
|
494
|
-
* @param firstVersion - The first version number to compare.
|
|
495
|
-
* @param secondVersion - The second version number to compare.
|
|
496
|
-
*
|
|
497
|
-
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
498
|
-
*/
|
|
499
|
-
static isEqual(firstVersion, secondVersion) {
|
|
500
|
-
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* Get a formatted string representation of the current version number
|
|
504
|
-
*
|
|
505
|
-
* @param options - Options to apply to the string formatting.
|
|
506
|
-
*
|
|
507
|
-
* @returns A formatted string representation of the current version number with the options applied.
|
|
508
|
-
*/
|
|
509
|
-
format(options) {
|
|
510
|
-
let baseOutput = `${this.major}`;
|
|
511
|
-
if (!options?.omitMinor) {
|
|
512
|
-
baseOutput += `.${this.minor}`;
|
|
513
|
-
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
514
|
-
}
|
|
515
|
-
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
516
|
-
}
|
|
517
|
-
/**
|
|
518
|
-
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
519
|
-
*
|
|
520
|
-
* @param incrementType - The type of increment. Can be one of the following:
|
|
521
|
-
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
522
|
-
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
523
|
-
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
524
|
-
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
525
|
-
*
|
|
526
|
-
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
527
|
-
*/
|
|
528
|
-
increment(incrementType, incrementAmount = 1) {
|
|
529
|
-
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
530
|
-
const calculatedRawVersion = {
|
|
531
|
-
major: [
|
|
532
|
-
this.major + incrementBy,
|
|
533
|
-
0,
|
|
534
|
-
0
|
|
535
|
-
],
|
|
536
|
-
minor: [
|
|
537
|
-
this.major,
|
|
538
|
-
this.minor + incrementBy,
|
|
539
|
-
0
|
|
540
|
-
],
|
|
541
|
-
patch: [
|
|
542
|
-
this.major,
|
|
543
|
-
this.minor,
|
|
544
|
-
this.patch + incrementBy
|
|
545
|
-
]
|
|
546
|
-
}[incrementType];
|
|
547
|
-
try {
|
|
548
|
-
return new VersionNumber(calculatedRawVersion);
|
|
549
|
-
} catch (error) {
|
|
550
|
-
if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
|
|
551
|
-
currentVersion: this.toString(),
|
|
552
|
-
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
553
|
-
incrementAmount
|
|
554
|
-
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
555
|
-
else throw error;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
/**
|
|
559
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
560
|
-
*
|
|
561
|
-
* @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).
|
|
562
|
-
*
|
|
563
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
564
|
-
*/
|
|
565
|
-
[Symbol.toPrimitive](hint) {
|
|
566
|
-
if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
567
|
-
return this.toString();
|
|
568
|
-
}
|
|
569
|
-
/**
|
|
570
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
571
|
-
*
|
|
572
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
573
|
-
*/
|
|
574
|
-
toJSON() {
|
|
575
|
-
return this.toString();
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Get a string representation of the current version number.
|
|
579
|
-
*
|
|
580
|
-
* @returns A stringified representation of the current version number with the prefix.
|
|
581
|
-
*/
|
|
582
|
-
toString() {
|
|
583
|
-
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
584
|
-
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
585
|
-
}
|
|
586
|
-
};
|
|
587
|
-
z.union([
|
|
588
|
-
z.string(),
|
|
589
|
-
z.tuple([
|
|
590
|
-
z.number(),
|
|
591
|
-
z.number(),
|
|
592
|
-
z.number()
|
|
593
|
-
]),
|
|
594
|
-
z.instanceof(VersionNumber)
|
|
595
|
-
]).transform((rawVersionNumber) => {
|
|
596
|
-
return new VersionNumber(rawVersionNumber);
|
|
597
|
-
});
|
|
598
|
-
//#endregion
|
|
599
383
|
//#region src/node/functions/parseFilePath.ts
|
|
600
384
|
/**
|
|
601
385
|
* Takes a file path string and parses it into the directory part, the base part, and the full path.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alextheman/utility",
|
|
3
|
-
"version": "5.11.
|
|
3
|
+
"version": "5.11.2",
|
|
4
4
|
"description": "Helpful utility functions.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"author": "alextheman",
|
|
11
11
|
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
12
13
|
"exports": {
|
|
13
14
|
".": {
|
|
14
15
|
"types": "./dist/index.d.ts",
|
|
@@ -35,9 +36,9 @@
|
|
|
35
36
|
"zod": "4.3.6"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"@alextheman/eslint-plugin": "5.
|
|
39
|
+
"@alextheman/eslint-plugin": "5.13.0",
|
|
39
40
|
"@types/node": "25.6.0",
|
|
40
|
-
"alex-c-line": "2.
|
|
41
|
+
"alex-c-line": "2.6.1",
|
|
41
42
|
"cross-env": "10.1.0",
|
|
42
43
|
"dotenv-cli": "11.0.0",
|
|
43
44
|
"eslint": "10.2.0",
|
|
@@ -49,9 +50,11 @@
|
|
|
49
50
|
"tsdown": "0.21.7",
|
|
50
51
|
"tsx": "4.21.0",
|
|
51
52
|
"typedoc": "0.28.18",
|
|
53
|
+
"typedoc-plugin-markdown": "4.11.0",
|
|
54
|
+
"typedoc-rhineai-theme": "1.2.0",
|
|
52
55
|
"typescript": "6.0.2",
|
|
53
56
|
"typescript-eslint": "8.58.1",
|
|
54
|
-
"vite
|
|
57
|
+
"vite": "8.0.8",
|
|
55
58
|
"vitest": "4.1.4"
|
|
56
59
|
},
|
|
57
60
|
"engines": {
|