@alextheman/utility 5.0.0 → 5.1.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.
@@ -32,6 +32,28 @@ declare function normalizeImportPath(importPath: string): string;
32
32
  */
33
33
  declare const normaliseImportPath: typeof normalizeImportPath;
34
34
  //#endregion
35
+ //#region src/node/functions/parseFilePath.d.ts
36
+ interface FilePathData {
37
+ /** The file path without the final part. */
38
+ directory: string;
39
+ /** The final part of the file path. */
40
+ base: string;
41
+ /** The full file path, normalised. */
42
+ fullPath: string;
43
+ }
44
+ /**
45
+ * Takes a file path string and parses it into the directory part, the base part, and the full path.
46
+ *
47
+ * @category Parsers
48
+ *
49
+ * @param filePath - The file path to parse.
50
+ *
51
+ * @throws {DataError} If the file path is invalid.
52
+ *
53
+ * @returns An object representing the different ways the file path can be represented.
54
+ */
55
+ declare function parseFilePath(filePath: string): FilePathData;
56
+ //#endregion
35
57
  //#region src/root/types/IsTypeArgumentString.d.ts
36
58
  type IsTypeArgumentString<Argument extends string> = Argument;
37
59
  //#endregion
@@ -45,4 +67,4 @@ type IsTypeArgumentString<Argument extends string> = Argument;
45
67
  */
46
68
  declare function sayHello(): string;
47
69
  //#endregion
48
- export { type IsTypeArgumentString, normaliseImportPath, normalizeImportPath, sayHello };
70
+ export { type IsTypeArgumentString, normaliseImportPath, normalizeImportPath, parseFilePath, sayHello };
@@ -46,6 +46,47 @@ const FILE_PATH_REGEX = String.raw`^(?<directory>.+)[\/\\](?<base>[^\/\\]+)$`;
46
46
  //#region src/root/constants/VERSION_NUMBER_REGEX.ts
47
47
  const VERSION_NUMBER_REGEX = "^(?:v)?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$";
48
48
 
49
+ //#endregion
50
+ //#region src/root/types/DataError.ts
51
+ /**
52
+ * Represents errors you may get that may've been caused by a specific piece of data.
53
+ *
54
+ * @category Types
55
+ *
56
+ * @template DataType - The type of the data that caused the error.
57
+ */
58
+ var DataError = class DataError extends Error {
59
+ code;
60
+ data;
61
+ /**
62
+ * @param data - The data that caused the error.
63
+ * @param code - A standardised code (e.g. UNEXPECTED_DATA).
64
+ * @param message - A human-readable error message (e.g. The data provided is invalid).
65
+ * @param options - Extra options to pass to super Error constructor.
66
+ */
67
+ constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
68
+ super(message, options);
69
+ if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
70
+ this.name = new.target.name;
71
+ this.code = code;
72
+ this.data = data;
73
+ Object.defineProperty(this, "message", { enumerable: true });
74
+ Object.setPrototypeOf(this, new.target.prototype);
75
+ }
76
+ /**
77
+ * Checks whether the given input may have been caused by a DataError.
78
+ *
79
+ * @param input - The input to check.
80
+ *
81
+ * @returns `true` if the input is a DataError, and `false` otherwise. The type of the input will also be narrowed down to DataError if `true`.
82
+ */
83
+ static check(input) {
84
+ if (input instanceof DataError) return true;
85
+ const data = input;
86
+ return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
87
+ }
88
+ };
89
+
49
90
  //#endregion
50
91
  //#region src/root/functions/arrayHelpers/fillArray.ts
51
92
  /**
@@ -97,212 +138,6 @@ function paralleliseArrays(firstArray, secondArray) {
97
138
  return outputArray;
98
139
  }
99
140
 
100
- //#endregion
101
- //#region src/root/types/DataError.ts
102
- /**
103
- * Represents errors you may get that may've been caused by a specific piece of data.
104
- *
105
- * @category Types
106
- *
107
- * @template DataType - The type of the data that caused the error.
108
- */
109
- var DataError = class DataError extends Error {
110
- code;
111
- data;
112
- /**
113
- * @param data - The data that caused the error.
114
- * @param code - A standardised code (e.g. UNEXPECTED_DATA).
115
- * @param message - A human-readable error message (e.g. The data provided is invalid).
116
- * @param options - Extra options to pass to super Error constructor.
117
- */
118
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
119
- super(message, options);
120
- if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
121
- this.name = new.target.name;
122
- this.code = code;
123
- this.data = data;
124
- Object.defineProperty(this, "message", { enumerable: true });
125
- Object.setPrototypeOf(this, new.target.prototype);
126
- }
127
- /**
128
- * Checks whether the given input may have been caused by a DataError.
129
- *
130
- * @param input - The input to check.
131
- *
132
- * @returns `true` if the input is a DataError, and `false` otherwise. The type of the input will also be narrowed down to DataError if `true`.
133
- */
134
- static check(input) {
135
- if (input instanceof DataError) return true;
136
- const data = input;
137
- return typeof data === "object" && data !== null && typeof data.message === "string" && typeof data.code === "string" && "data" in data;
138
- }
139
- };
140
-
141
- //#endregion
142
- //#region src/root/types/VersionNumber.ts
143
- /**
144
- * Represents a software version number, considered to be made up of a major, minor, and patch part.
145
- *
146
- * @category Types
147
- */
148
- var VersionNumber = class VersionNumber {
149
- static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
150
- /** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
151
- major = 0;
152
- /** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
153
- minor = 0;
154
- /** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
155
- patch = 0;
156
- /**
157
- * @param input - The input to create a new instance of `VersionNumber` from.
158
- */
159
- constructor(input) {
160
- if (input instanceof VersionNumber) {
161
- this.major = input.major;
162
- this.minor = input.minor;
163
- this.patch = input.patch;
164
- } else if (typeof input === "string") {
165
- if (!RegExp(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.`);
166
- const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
167
- return parseIntStrict(number);
168
- });
169
- this.major = major;
170
- this.minor = minor;
171
- this.patch = patch;
172
- } else if (Array.isArray(input)) {
173
- if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
174
- const [major, minor, patch] = input.map((number) => {
175
- const parsedInteger = parseIntStrict(number?.toString());
176
- if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
177
- return parsedInteger;
178
- });
179
- this.major = major;
180
- this.minor = minor;
181
- this.patch = patch;
182
- }
183
- }
184
- /**
185
- * Gets the current version type of the current instance of `VersionNumber`.
186
- *
187
- * @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
188
- */
189
- get type() {
190
- if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
191
- if (this.patch === 0) return VersionType.MINOR;
192
- return VersionType.PATCH;
193
- }
194
- static formatString(input, options) {
195
- if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
196
- return input.startsWith("v") ? input : `v${input}`;
197
- }
198
- /**
199
- * Checks if the provided version numbers have the exact same major, minor, and patch numbers.
200
- *
201
- * @param firstVersion - The first version number to compare.
202
- * @param secondVersion - The second version number to compare.
203
- *
204
- * @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
205
- */
206
- static isEqual(firstVersion, secondVersion) {
207
- return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
208
- }
209
- /**
210
- * Get a formatted string representation of the current version number
211
- *
212
- * @param options - Options to apply to the string formatting.
213
- *
214
- * @returns A formatted string representation of the current version number with the options applied.
215
- */
216
- format(options) {
217
- let baseOutput = `${this.major}`;
218
- if (!options?.omitMinor) {
219
- baseOutput += `.${this.minor}`;
220
- if (!options?.omitPatch) baseOutput += `.${this.patch}`;
221
- }
222
- return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
223
- }
224
- /**
225
- * Increments the current version number by the given increment type, returning the result as a new reference in memory.
226
- *
227
- * @param incrementType - The type of increment. Can be one of the following:
228
- * - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
229
- * - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
230
- * - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
231
- * @param incrementAmount - The amount to increment by (defaults to 1).
232
- *
233
- * @returns A new instance of `VersionNumber` with the increment applied.
234
- */
235
- increment(incrementType, incrementAmount = 1) {
236
- const incrementBy = parseIntStrict(String(incrementAmount));
237
- const calculatedRawVersion = {
238
- major: [
239
- this.major + incrementBy,
240
- 0,
241
- 0
242
- ],
243
- minor: [
244
- this.major,
245
- this.minor + incrementBy,
246
- 0
247
- ],
248
- patch: [
249
- this.major,
250
- this.minor,
251
- this.patch + incrementBy
252
- ]
253
- }[incrementType];
254
- try {
255
- return new VersionNumber(calculatedRawVersion);
256
- } catch (error) {
257
- if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
258
- currentVersion: this.toString(),
259
- calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
260
- incrementAmount
261
- }, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
262
- else throw error;
263
- }
264
- }
265
- /**
266
- * Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
267
- *
268
- * @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).
269
- *
270
- * @returns A stringified representation of the current version number, prefixed with `v`.
271
- */
272
- [Symbol.toPrimitive](hint) {
273
- if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
274
- return this.toString();
275
- }
276
- /**
277
- * Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
278
- *
279
- * @returns A stringified representation of the current version number, prefixed with `v`.
280
- */
281
- toJSON() {
282
- return this.toString();
283
- }
284
- /**
285
- * Get a string representation of the current version number.
286
- *
287
- * @returns A stringified representation of the current version number with the prefix.
288
- */
289
- toString() {
290
- const rawString = `${this.major}.${this.minor}.${this.patch}`;
291
- return VersionNumber.formatString(rawString, { omitPrefix: false });
292
- }
293
- };
294
- const zodVersionNumber = z.union([
295
- z.string(),
296
- z.tuple([
297
- z.number(),
298
- z.number(),
299
- z.number()
300
- ]),
301
- z.instanceof(VersionNumber)
302
- ]).transform((rawVersionNumber) => {
303
- return new VersionNumber(rawVersionNumber);
304
- });
305
-
306
141
  //#endregion
307
142
  //#region src/root/functions/parsers/parseIntStrict.ts
308
143
  /**
@@ -554,9 +389,205 @@ const VersionType = {
554
389
  PATCH: "patch"
555
390
  };
556
391
 
392
+ //#endregion
393
+ //#region src/root/types/VersionNumber.ts
394
+ /**
395
+ * Represents a software version number, considered to be made up of a major, minor, and patch part.
396
+ *
397
+ * @category Types
398
+ */
399
+ var VersionNumber = class VersionNumber {
400
+ static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
401
+ /** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
402
+ major = 0;
403
+ /** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
404
+ minor = 0;
405
+ /** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
406
+ patch = 0;
407
+ /**
408
+ * @param input - The input to create a new instance of `VersionNumber` from.
409
+ */
410
+ constructor(input) {
411
+ if (input instanceof VersionNumber) {
412
+ this.major = input.major;
413
+ this.minor = input.minor;
414
+ this.patch = input.patch;
415
+ } else if (typeof input === "string") {
416
+ if (!RegExp(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.`);
417
+ const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
418
+ return parseIntStrict(number);
419
+ });
420
+ this.major = major;
421
+ this.minor = minor;
422
+ this.patch = patch;
423
+ } else if (Array.isArray(input)) {
424
+ if (input.length !== 3) throw new DataError({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
425
+ const [major, minor, patch] = input.map((number) => {
426
+ const parsedInteger = parseIntStrict(number?.toString());
427
+ if (parsedInteger < 0) throw new DataError({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
428
+ return parsedInteger;
429
+ });
430
+ this.major = major;
431
+ this.minor = minor;
432
+ this.patch = patch;
433
+ }
434
+ }
435
+ /**
436
+ * Gets the current version type of the current instance of `VersionNumber`.
437
+ *
438
+ * @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
439
+ */
440
+ get type() {
441
+ if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
442
+ if (this.patch === 0) return VersionType.MINOR;
443
+ return VersionType.PATCH;
444
+ }
445
+ static formatString(input, options) {
446
+ if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
447
+ return input.startsWith("v") ? input : `v${input}`;
448
+ }
449
+ /**
450
+ * Checks if the provided version numbers have the exact same major, minor, and patch numbers.
451
+ *
452
+ * @param firstVersion - The first version number to compare.
453
+ * @param secondVersion - The second version number to compare.
454
+ *
455
+ * @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
456
+ */
457
+ static isEqual(firstVersion, secondVersion) {
458
+ return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
459
+ }
460
+ /**
461
+ * Get a formatted string representation of the current version number
462
+ *
463
+ * @param options - Options to apply to the string formatting.
464
+ *
465
+ * @returns A formatted string representation of the current version number with the options applied.
466
+ */
467
+ format(options) {
468
+ let baseOutput = `${this.major}`;
469
+ if (!options?.omitMinor) {
470
+ baseOutput += `.${this.minor}`;
471
+ if (!options?.omitPatch) baseOutput += `.${this.patch}`;
472
+ }
473
+ return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
474
+ }
475
+ /**
476
+ * Increments the current version number by the given increment type, returning the result as a new reference in memory.
477
+ *
478
+ * @param incrementType - The type of increment. Can be one of the following:
479
+ * - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
480
+ * - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
481
+ * - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
482
+ * @param incrementAmount - The amount to increment by (defaults to 1).
483
+ *
484
+ * @returns A new instance of `VersionNumber` with the increment applied.
485
+ */
486
+ increment(incrementType, incrementAmount = 1) {
487
+ const incrementBy = parseIntStrict(String(incrementAmount));
488
+ const calculatedRawVersion = {
489
+ major: [
490
+ this.major + incrementBy,
491
+ 0,
492
+ 0
493
+ ],
494
+ minor: [
495
+ this.major,
496
+ this.minor + incrementBy,
497
+ 0
498
+ ],
499
+ patch: [
500
+ this.major,
501
+ this.minor,
502
+ this.patch + incrementBy
503
+ ]
504
+ }[incrementType];
505
+ try {
506
+ return new VersionNumber(calculatedRawVersion);
507
+ } catch (error) {
508
+ if (DataError.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError({
509
+ currentVersion: this.toString(),
510
+ calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
511
+ incrementAmount
512
+ }, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
513
+ else throw error;
514
+ }
515
+ }
516
+ /**
517
+ * Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
518
+ *
519
+ * @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).
520
+ *
521
+ * @returns A stringified representation of the current version number, prefixed with `v`.
522
+ */
523
+ [Symbol.toPrimitive](hint) {
524
+ if (hint === "number") throw new DataError({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
525
+ return this.toString();
526
+ }
527
+ /**
528
+ * Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
529
+ *
530
+ * @returns A stringified representation of the current version number, prefixed with `v`.
531
+ */
532
+ toJSON() {
533
+ return this.toString();
534
+ }
535
+ /**
536
+ * Get a string representation of the current version number.
537
+ *
538
+ * @returns A stringified representation of the current version number with the prefix.
539
+ */
540
+ toString() {
541
+ const rawString = `${this.major}.${this.minor}.${this.patch}`;
542
+ return VersionNumber.formatString(rawString, { omitPrefix: false });
543
+ }
544
+ };
545
+ const zodVersionNumber = z.union([
546
+ z.string(),
547
+ z.tuple([
548
+ z.number(),
549
+ z.number(),
550
+ z.number()
551
+ ]),
552
+ z.instanceof(VersionNumber)
553
+ ]).transform((rawVersionNumber) => {
554
+ return new VersionNumber(rawVersionNumber);
555
+ });
556
+
557
+ //#endregion
558
+ //#region src/node/functions/parseFilePath.ts
559
+ /**
560
+ * Takes a file path string and parses it into the directory part, the base part, and the full path.
561
+ *
562
+ * @category Parsers
563
+ *
564
+ * @param filePath - The file path to parse.
565
+ *
566
+ * @throws {DataError} If the file path is invalid.
567
+ *
568
+ * @returns An object representing the different ways the file path can be represented.
569
+ */
570
+ function parseFilePath(filePath) {
571
+ const caughtGroups = filePath.match(RegExp(FILE_PATH_REGEX));
572
+ if (!caughtGroups) {
573
+ if (!(filePath.includes("/") || filePath.includes("\\")) && filePath.includes(".")) return {
574
+ directory: "",
575
+ base: filePath,
576
+ fullPath: filePath
577
+ };
578
+ throw new DataError({ filePath }, "INVALID_FILE_PATH", "The file path you provided is not valid.");
579
+ }
580
+ if (!caughtGroups.groups) throw new DataError({ filePath }, "PARSING_ERROR", "An error occurred while trying to parse the data.");
581
+ return {
582
+ directory: caughtGroups.groups.directory,
583
+ base: caughtGroups.groups.base,
584
+ fullPath: path.join(caughtGroups.groups.directory.replaceAll("\\", "/"), caughtGroups.groups.base)
585
+ };
586
+ }
587
+
557
588
  //#endregion
558
589
  //#region src/node/functions/sayHello.ts
559
590
  var sayHello_default = sayHello;
560
591
 
561
592
  //#endregion
562
- export { normaliseImportPath, normalizeImportPath, sayHello_default as sayHello };
593
+ export { normaliseImportPath, normalizeImportPath, parseFilePath, sayHello_default as sayHello };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alextheman/utility",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Helpful utility functions.",
5
5
  "repository": {
6
6
  "type": "git",