@alextheman/utility 4.3.0 → 4.3.1

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 CHANGED
@@ -47,6 +47,8 @@ var VERSION_NUMBER_REGEX_default = VERSION_NUMBER_REGEX;
47
47
  * If the callback returns at least one Promise, the entire result will be wrapped
48
48
  * in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
49
49
  *
50
+ * @category Array Helpers
51
+ *
50
52
  * @template ItemType
51
53
  *
52
54
  * @param callback - A function invoked with the current index. May return a value or a Promise.
@@ -73,6 +75,8 @@ var fillArray_default = fillArray;
73
75
  * If `secondArray` is shorter than `firstArray`, the second position in the tuple
74
76
  * will be `undefined`. Iteration always uses the length of the first array.
75
77
  *
78
+ * @category Array Helpers
79
+ *
76
80
  * @template FirstArrayItem
77
81
  * @template SecondArrayItem
78
82
  *
@@ -98,7 +102,11 @@ const httpErrorCodeLookup = {
98
102
  418: "I_AM_A_TEAPOT",
99
103
  500: "INTERNAL_SERVER_ERROR"
100
104
  };
101
- /** Represents common errors you may get from a HTTP API request. */
105
+ /**
106
+ * Represents common errors you may get from a HTTP API request.
107
+ *
108
+ * @category Types
109
+ */
102
110
  var APIError = class extends Error {
103
111
  status;
104
112
  /**
@@ -130,10 +138,14 @@ var APIError_default = APIError;
130
138
 
131
139
  //#endregion
132
140
  //#region src/types/DataError.ts
133
- /** Represents errors you may get that may've been caused by a specific piece of data. */
141
+ /**
142
+ * Represents errors you may get that may've been caused by a specific piece of data.
143
+ *
144
+ * @category Types
145
+ */
134
146
  var DataError = class extends Error {
135
- data;
136
147
  code;
148
+ data;
137
149
  /**
138
150
  * @param data - The data that caused the error.
139
151
  * @param code - A standardised code (e.g. UNEXPECTED_DATA).
@@ -165,6 +177,11 @@ var DataError_default = DataError;
165
177
 
166
178
  //#endregion
167
179
  //#region src/types/VersionType.ts
180
+ /**
181
+ * Represents the three common software version types.
182
+ *
183
+ * @category Types
184
+ */
168
185
  const VersionType = {
169
186
  MAJOR: "major",
170
187
  MINOR: "minor",
@@ -173,15 +190,19 @@ const VersionType = {
173
190
 
174
191
  //#endregion
175
192
  //#region src/types/VersionNumber.ts
176
- /** Represents a software version number, considered to be made up of a major, minor, and patch part. */
193
+ /**
194
+ * Represents a software version number, considered to be made up of a major, minor, and patch part.
195
+ *
196
+ * @category Types
197
+ */
177
198
  var VersionNumber = class VersionNumber {
199
+ static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
178
200
  /** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
179
201
  major = 0;
180
202
  /** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
181
203
  minor = 0;
182
204
  /** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
183
205
  patch = 0;
184
- static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
185
206
  /**
186
207
  * @param input - The input to create a new instance of `VersionNumber` from.
187
208
  */
@@ -210,21 +231,6 @@ var VersionNumber = class VersionNumber {
210
231
  this.patch = patch;
211
232
  }
212
233
  }
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
- * Get a string representation of the current version number.
219
- *
220
- * @param options - Extra additional options to apply.
221
- *
222
- * @returns A stringified representation of the current version number, leaving out the prefix if `omitPrefix` option was set to true.
223
- */
224
- toString(options) {
225
- const rawString = `${this.major}.${this.minor}.${this.patch}`;
226
- return VersionNumber.formatString(rawString, options);
227
- }
228
234
  /**
229
235
  * Gets the current version type of the current instance of `VersionNumber`.
230
236
  *
@@ -235,6 +241,21 @@ var VersionNumber = class VersionNumber {
235
241
  if (this.patch === 0) return VersionType.MINOR;
236
242
  return VersionType.PATCH;
237
243
  }
244
+ static formatString(input, options) {
245
+ if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
246
+ return input.startsWith("v") ? input : `v${input}`;
247
+ }
248
+ /**
249
+ * Checks if the provided version numbers have the exact same major, minor, and patch numbers.
250
+ *
251
+ * @param firstVersion - The first version number to compare.
252
+ * @param secondVersion - The second version number to compare.
253
+ *
254
+ * @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
255
+ */
256
+ static isEqual(firstVersion, secondVersion) {
257
+ return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
258
+ }
238
259
  /**
239
260
  * Determines whether the current instance of `VersionNumber` is a major, minor, or patch version.
240
261
  *
@@ -246,35 +267,34 @@ var VersionNumber = class VersionNumber {
246
267
  * @returns A new instance of `VersionNumber` with the increment applied.
247
268
  */
248
269
  increment(incrementType) {
249
- const newVersion = {
250
- major: [
270
+ return {
271
+ major: new VersionNumber([
251
272
  this.major + 1,
252
273
  0,
253
274
  0
254
- ],
255
- minor: [
275
+ ]),
276
+ minor: new VersionNumber([
256
277
  this.major,
257
278
  this.minor + 1,
258
279
  0
259
- ],
260
- patch: [
280
+ ]),
281
+ patch: new VersionNumber([
261
282
  this.major,
262
283
  this.minor,
263
284
  this.patch + 1
264
- ]
285
+ ])
265
286
  }[incrementType];
266
- return new VersionNumber(newVersion);
267
287
  }
268
288
  /**
269
- * Checks if the provided version numbers have the exact same major, minor, and patch numbers.
289
+ * Get a string representation of the current version number.
270
290
  *
271
- * @param firstVersion - The first version number to compare.
272
- * @param secondVersion - The second version number to compare.
291
+ * @param options - Extra additional options to apply.
273
292
  *
274
- * @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
293
+ * @returns A stringified representation of the current version number, leaving out the prefix if `omitPrefix` option was set to true.
275
294
  */
276
- static isEqual(firstVersion, secondVersion) {
277
- return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
295
+ toString(options) {
296
+ const rawString = `${this.major}.${this.minor}.${this.patch}`;
297
+ return VersionNumber.formatString(rawString, options);
278
298
  }
279
299
  };
280
300
  var VersionNumber_default = VersionNumber;
@@ -284,6 +304,8 @@ var VersionNumber_default = VersionNumber;
284
304
  /**
285
305
  * Converts a string to an integer and throws an error if it cannot be converted.
286
306
  *
307
+ * @category Parsers
308
+ *
287
309
  * @param string - A string to convert into a number.
288
310
  * @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.
289
311
  *
@@ -315,6 +337,8 @@ var parseIntStrict_default = parseIntStrict;
315
337
  /**
316
338
  * Gets a random number between the given bounds.
317
339
  *
340
+ * @category Miscellaneous
341
+ *
318
342
  * @param lowerBound - The lowest number that can be chosen.
319
343
  * @param upperBound - The highest number that can be chosen.
320
344
  *
@@ -332,6 +356,8 @@ var getRandomNumber_default = getRandomNumber;
332
356
  /**
333
357
  * Randomises the order of a given array and returns the result in a new array without mutating the original.
334
358
  *
359
+ * @category Array Helpers
360
+ *
335
361
  * @template ItemType - The type of the array items.
336
362
  *
337
363
  * @param array - The array to randomise.
@@ -357,6 +383,8 @@ var randomiseArray_default = randomiseArray;
357
383
  * - The range is inclusive of `start` and exclusive of `stop`.
358
384
  * - The sign of `step` must match the direction of the range.
359
385
  *
386
+ * @category Array Helpers
387
+ *
360
388
  * @param start - The number to start at (inclusive).
361
389
  * @param stop - The number to stop at (exclusive).
362
390
  * @param step - The step size between numbers, defaulting to 1.
@@ -385,6 +413,8 @@ var range_default = range;
385
413
  /**
386
414
  * Removes duplicate values from an array.
387
415
  *
416
+ * @category Array Helpers
417
+ *
388
418
  * @template ItemType - The type of the array items.
389
419
  *
390
420
  * @param array - The array to remove duplicates from.
@@ -403,6 +433,8 @@ var removeDuplicates_default = removeDuplicates;
403
433
  /**
404
434
  * Adds a given number of days to the provided date, returning the result as a new instance of Date.
405
435
  *
436
+ * @category Date
437
+ *
406
438
  * @param currentDate - The starting date (defaults to today).
407
439
  * @param dayIncrement - The amount of days you want to add (defaults to 1)
408
440
  *
@@ -420,6 +452,8 @@ var addDaysToDate_default = addDaysToDate;
420
452
  /**
421
453
  * Checks if the provided dates happen on the exact same day, month, and year.
422
454
  *
455
+ * @category Date
456
+ *
423
457
  * @param firstDate - The first date to compare.
424
458
  * @param secondDate - The second date to compare.
425
459
  *
@@ -435,6 +469,8 @@ var isSameDate_default = isSameDate;
435
469
  /**
436
470
  * Creates a human-readable string with information about the input date.
437
471
  *
472
+ * @category Date
473
+ *
438
474
  * @param inputDate - The date to base the string on.
439
475
  *
440
476
  * @returns A new string with information about the given date.
@@ -458,6 +494,8 @@ var formatDateAndTime_default = formatDateAndTime;
458
494
  /**
459
495
  * Checks if the provided year is a leap year.
460
496
  *
497
+ * @category Date
498
+ *
461
499
  * @param year - The year to check as a number.
462
500
  *
463
501
  * @throws {TypeError} If the year provided is not an integer.
@@ -479,6 +517,8 @@ function checkLeapYear(firstDate, secondDate) {
479
517
  /**
480
518
  * Checks if the provided dates are exactly a whole number of years apart.
481
519
  *
520
+ * @category Date
521
+ *
482
522
  * @param firstDate - The first date to compare.
483
523
  * @param secondDate - The second date to compare.
484
524
  *
@@ -525,6 +565,8 @@ function leapYearFebruaryChecks(firstDate, secondDate) {
525
565
  /**
526
566
  * Checks if the provided dates are exactly a whole number of months apart.
527
567
  *
568
+ * @category Date
569
+ *
528
570
  * @param firstDate - The first date to compare.
529
571
  * @param secondDate - The second date to compare.
530
572
  *
@@ -543,6 +585,8 @@ var isMonthlyMultiple_default = isMonthlyMultiple;
543
585
  /**
544
586
  * Asynchronously converts a file to a base 64 string
545
587
  *
588
+ * @category Miscellaneous
589
+ *
546
590
  * @param file - The file to convert.
547
591
  *
548
592
  * @throws {Error} If the file reader gives an error.
@@ -575,6 +619,8 @@ function getNullableResolutionStrategy(key, strategy) {
575
619
  /**
576
620
  * Creates FormData from a given object, resolving non-string types as appropriate.
577
621
  *
622
+ * @category Miscellaneous
623
+ *
578
624
  * @template DataType - The type of the given data.
579
625
  *
580
626
  * @param data - The data to create FormData from.
@@ -640,6 +686,8 @@ var createFormData_default = createFormData;
640
686
  /**
641
687
  * Checks to see if the given array is sorted in ascending order.
642
688
  *
689
+ * @category Miscellaneous
690
+ *
643
691
  * @param array - The array to check.
644
692
  *
645
693
  * @returns `true` if the array is sorted in ascending order, and `false` otherwise.
@@ -657,6 +705,8 @@ var isOrdered_default = isOrdered;
657
705
  /**
658
706
  * Converts a stringly-typed list to a proper array.
659
707
  *
708
+ * @category Miscellaneous
709
+ *
660
710
  * @param stringList - The stringly-typed list to convert.
661
711
  * @param options - The options to apply to the conversion.
662
712
  * @param options.separator - What each item in the list is separated by.
@@ -678,6 +728,8 @@ var stringListToArray_default = stringListToArray;
678
728
  /**
679
729
  * Waits for the given number of seconds
680
730
  *
731
+ * @category Miscellaneous
732
+ *
681
733
  * @param seconds - The number of seconds to wait.
682
734
  *
683
735
  * @returns A Promise that resolves after the given number of seconds.
@@ -696,6 +748,8 @@ var wait_default = wait;
696
748
  /**
697
749
  * Gets the keys from a given record object, properly typed to be an array of the key of the input object's type.
698
750
  *
751
+ * @category Object Helpers
752
+ *
699
753
  * @template InputRecordType - The type of the input object.
700
754
  *
701
755
  * @param record - The record to get the keys from.
@@ -712,6 +766,8 @@ var getRecordKeys_default = getRecordKeys;
712
766
  /**
713
767
  * Omits properties from a given object.
714
768
  *
769
+ * @category Object Helpers
770
+ *
715
771
  * @template ObjectType - The type of the input object.
716
772
  * @template KeysToOmit - A type representing the keys to omit from the object.
717
773
  *
@@ -734,6 +790,8 @@ var omitProperties_default = omitProperties;
734
790
  /**
735
791
  * Takes a stringly-typed boolean and converts it to an actual boolean type.
736
792
  *
793
+ * @category Parsers
794
+ *
737
795
  * @param inputString - The string to parse.
738
796
  *
739
797
  * @throws {DataError} If the string is not either `true` or `false` (case insensitive).
@@ -752,6 +810,8 @@ var parseBoolean_default = parseBoolean;
752
810
  /**
753
811
  * An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
754
812
  *
813
+ * @category Parsers
814
+ *
755
815
  * @template Output - The Zod output type.
756
816
  * @template Input - The Zod input type.
757
817
  * @template Internals - The Zod internal types based on the output and input types.
@@ -782,6 +842,8 @@ const envSchema = zod.z.enum([
782
842
  /**
783
843
  * Parses the input and verifies it matches one of the environments allowed by the Env types ("test" | "development" | "production").
784
844
  *
845
+ * @category Parsers
846
+ *
785
847
  * @param data - The data to parse.
786
848
  *
787
849
  * @throws {DataError} If the data does not match one of the environments allowed by the Env types ("test" | "development" | "production").
@@ -798,6 +860,8 @@ var parseEnv_default = parseEnv;
798
860
  /**
799
861
  * Returns an object given FormData and an optional data parser function to call on the resulting object.
800
862
  *
863
+ * @category Parsers
864
+ *
801
865
  * @template DataType - The type of the resulting object when called from the dataParser.
802
866
  *
803
867
  * @param formData - The FormData to parse.
@@ -820,6 +884,8 @@ var parseFormData_default = parseFormData;
820
884
  /**
821
885
  * Parses the input and verifies it is a valid software version type (i.e. `"major" | "minor" | "patch"`)
822
886
  *
887
+ * @category Parsers
888
+ *
823
889
  * @param data - The data to parse.
824
890
  *
825
891
  * @throws {DataError} If the data does not match one of the allowed version types (`"major" | "minor" | "patch"`).
@@ -839,6 +905,8 @@ function callDeepCopy(input) {
839
905
  /**
840
906
  * Deeply copies an object or array such that all child objects/arrays are also copied.
841
907
  *
908
+ * @category Recursive
909
+ *
842
910
  * @template ObjectType - The type of the input object.
843
911
  *
844
912
  * @param object - The object to copy. May also be an array.
@@ -866,6 +934,8 @@ var deepCopy_default = deepCopy;
866
934
  * Note that this will also freeze the input itself as well.
867
935
  * If the intent is to create a newly frozen object with a different reference in memory, pass your object through deepCopy first before passing to deepFreeze.
868
936
  *
937
+ * @category Recursive
938
+ *
869
939
  * @template ObjectType - The type of the input object.
870
940
  *
871
941
  * @param object - The object to freeze. May also be an array.
@@ -886,6 +956,8 @@ var deepFreeze_default = deepFreeze;
886
956
  /**
887
957
  * Appends a semicolon to the end of a string, trimming where necessary first.
888
958
  *
959
+ * @category String Helpers
960
+ *
889
961
  * @param stringToAppendTo - The string to append a semicolon to.
890
962
  *
891
963
  * @throws {Error} If the string contains multiple lines.
@@ -905,6 +977,8 @@ var appendSemicolon_default = appendSemicolon;
905
977
  /**
906
978
  * Converts a string from camelCase to kebab-case
907
979
  *
980
+ * @category String Helpers
981
+ *
908
982
  * @param string - The string to convert.
909
983
  * @param options - Options to apply to the conversion.
910
984
  *
@@ -943,6 +1017,8 @@ var camelToKebab_default = camelToKebab;
943
1017
  /**
944
1018
  * Converts a string from kebab-case to camelCase
945
1019
  *
1020
+ * @category String Helpers
1021
+ *
946
1022
  * @param string - The string to convert.
947
1023
  * @param options - Options to apply to the conversion.
948
1024
  *
@@ -980,13 +1056,14 @@ var kebabToCamel_default = kebabToCamel;
980
1056
  //#region src/functions/stringHelpers/normalizeImportPath.ts
981
1057
  /**
982
1058
  * Normalizes an import path meant for use in an import statement in JavaScript.
983
- *
984
1059
  * When multiple slashes are found, they're replaced by a single one; when the path contains a trailing slash, it is preserved. On Windows backslashes are used. If the path is a zero-length string, '.' is returned, representing the current working directory.
985
1060
  *
986
1061
  * If the path starts with ./, it is preserved (unlike what would happen with path.posix.normalize() normally).
987
1062
  *
988
1063
  * Helpful for custom linter rules that need to check (or fix) import paths.
989
1064
  *
1065
+ * @category String Helpers
1066
+ *
990
1067
  * @param importPath - The import path to normalize.
991
1068
  *
992
1069
  * @returns The import path normalized.
@@ -998,13 +1075,14 @@ function normalizeImportPath(importPath) {
998
1075
  }
999
1076
  /**
1000
1077
  * Normalises an import path meant for use in an import statement in JavaScript.
1001
- *
1002
1078
  * When multiple slashes are found, they're replaced by a single one; when the path contains a trailing slash, it is preserved. On Windows backslashes are used. If the path is a zero-length string, '.' is returned, representing the current working directory.
1003
1079
  *
1004
1080
  * If the path starts with ./, it is preserved (unlike what would happen with path.posix.normalize() normally).
1005
1081
  *
1006
1082
  * Helpful for custom linter rules that need to check (or fix) import paths.
1007
1083
  *
1084
+ * @category String Helpers
1085
+ *
1008
1086
  * @param importPath - The import path to normalise.
1009
1087
  *
1010
1088
  * @returns The import path normalised.
@@ -1017,6 +1095,8 @@ var normalizeImportPath_default = normalizeImportPath;
1017
1095
  /**
1018
1096
  * Truncates a string and appends `...` to the end of it
1019
1097
  *
1098
+ * @category String Helpers
1099
+ *
1020
1100
  * @param stringToTruncate - The string to truncate.
1021
1101
  * @param maxLength - The length at which to start truncating. Note that this does not include the `...` part that would be appended.
1022
1102
  *
@@ -1032,6 +1112,8 @@ var truncate_default = truncate;
1032
1112
  /**
1033
1113
  * Creates a template strings array given a regular array of strings
1034
1114
  *
1115
+ * @category Tagged Template
1116
+ *
1035
1117
  * @param strings - The array of strings.
1036
1118
  *
1037
1119
  * @returns A template strings array that can be passed as the first argument of any tagged template function.
@@ -1052,6 +1134,8 @@ var createTemplateStringsArray_default = createTemplateStringsArray;
1052
1134
  *
1053
1135
  * In this case, it will be functionally the same as if you just wrote the template string by itself.
1054
1136
  *
1137
+ * @category Tagged Template
1138
+ *
1055
1139
  * @param strings - The strings from the template to process.
1056
1140
  * @param interpolations - An array of all interpolations from the template.
1057
1141
  *
@@ -1073,6 +1157,8 @@ var interpolate_default = interpolate;
1073
1157
  *
1074
1158
  * interpolateObjects`Template string here ${{ my: "object" }}`.
1075
1159
  *
1160
+ * @category Tagged Template
1161
+ *
1076
1162
  * @param strings - The strings from the template to process.
1077
1163
  * @param interpolations - An array of all interpolations from the template.
1078
1164
  *
@@ -1129,6 +1215,8 @@ function reduceLines(lines, { preserveTabs = true }) {
1129
1215
  * with a new line
1130
1216
  * and another new line`.
1131
1217
  *
1218
+ *@category Tagged Template
1219
+ *
1132
1220
  * @param first - The strings from the template to process, or the options to apply.
1133
1221
  * @param args - An array of all interpolations from the template.
1134
1222
  *