@alextheman/utility 4.2.1 → 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
  */
@@ -202,7 +223,7 @@ var VersionNumber = class VersionNumber {
202
223
  if (input.length !== 3) throw new DataError_default(input, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
203
224
  const [major, minor, patch] = input.map((number) => {
204
225
  const parsedInteger = parseIntStrict_default(number?.toString());
205
- if (parsedInteger < 0) throw new DataError_default(input, "NON_POSITIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
226
+ if (parsedInteger < 0) throw new DataError_default(input, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
206
227
  return parsedInteger;
207
228
  });
208
229
  this.major = major;
@@ -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,24 +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);
287
+ }
288
+ /**
289
+ * Get a string representation of the current version number.
290
+ *
291
+ * @param options - Extra additional options to apply.
292
+ *
293
+ * @returns A stringified representation of the current version number, leaving out the prefix if `omitPrefix` option was set to true.
294
+ */
295
+ toString(options) {
296
+ const rawString = `${this.major}.${this.minor}.${this.patch}`;
297
+ return VersionNumber.formatString(rawString, options);
267
298
  }
268
299
  };
269
300
  var VersionNumber_default = VersionNumber;
@@ -273,6 +304,8 @@ var VersionNumber_default = VersionNumber;
273
304
  /**
274
305
  * Converts a string to an integer and throws an error if it cannot be converted.
275
306
  *
307
+ * @category Parsers
308
+ *
276
309
  * @param string - A string to convert into a number.
277
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.
278
311
  *
@@ -304,6 +337,8 @@ var parseIntStrict_default = parseIntStrict;
304
337
  /**
305
338
  * Gets a random number between the given bounds.
306
339
  *
340
+ * @category Miscellaneous
341
+ *
307
342
  * @param lowerBound - The lowest number that can be chosen.
308
343
  * @param upperBound - The highest number that can be chosen.
309
344
  *
@@ -321,6 +356,8 @@ var getRandomNumber_default = getRandomNumber;
321
356
  /**
322
357
  * Randomises the order of a given array and returns the result in a new array without mutating the original.
323
358
  *
359
+ * @category Array Helpers
360
+ *
324
361
  * @template ItemType - The type of the array items.
325
362
  *
326
363
  * @param array - The array to randomise.
@@ -346,6 +383,8 @@ var randomiseArray_default = randomiseArray;
346
383
  * - The range is inclusive of `start` and exclusive of `stop`.
347
384
  * - The sign of `step` must match the direction of the range.
348
385
  *
386
+ * @category Array Helpers
387
+ *
349
388
  * @param start - The number to start at (inclusive).
350
389
  * @param stop - The number to stop at (exclusive).
351
390
  * @param step - The step size between numbers, defaulting to 1.
@@ -374,6 +413,8 @@ var range_default = range;
374
413
  /**
375
414
  * Removes duplicate values from an array.
376
415
  *
416
+ * @category Array Helpers
417
+ *
377
418
  * @template ItemType - The type of the array items.
378
419
  *
379
420
  * @param array - The array to remove duplicates from.
@@ -392,6 +433,8 @@ var removeDuplicates_default = removeDuplicates;
392
433
  /**
393
434
  * Adds a given number of days to the provided date, returning the result as a new instance of Date.
394
435
  *
436
+ * @category Date
437
+ *
395
438
  * @param currentDate - The starting date (defaults to today).
396
439
  * @param dayIncrement - The amount of days you want to add (defaults to 1)
397
440
  *
@@ -409,6 +452,8 @@ var addDaysToDate_default = addDaysToDate;
409
452
  /**
410
453
  * Checks if the provided dates happen on the exact same day, month, and year.
411
454
  *
455
+ * @category Date
456
+ *
412
457
  * @param firstDate - The first date to compare.
413
458
  * @param secondDate - The second date to compare.
414
459
  *
@@ -424,6 +469,8 @@ var isSameDate_default = isSameDate;
424
469
  /**
425
470
  * Creates a human-readable string with information about the input date.
426
471
  *
472
+ * @category Date
473
+ *
427
474
  * @param inputDate - The date to base the string on.
428
475
  *
429
476
  * @returns A new string with information about the given date.
@@ -447,6 +494,8 @@ var formatDateAndTime_default = formatDateAndTime;
447
494
  /**
448
495
  * Checks if the provided year is a leap year.
449
496
  *
497
+ * @category Date
498
+ *
450
499
  * @param year - The year to check as a number.
451
500
  *
452
501
  * @throws {TypeError} If the year provided is not an integer.
@@ -468,6 +517,8 @@ function checkLeapYear(firstDate, secondDate) {
468
517
  /**
469
518
  * Checks if the provided dates are exactly a whole number of years apart.
470
519
  *
520
+ * @category Date
521
+ *
471
522
  * @param firstDate - The first date to compare.
472
523
  * @param secondDate - The second date to compare.
473
524
  *
@@ -514,6 +565,8 @@ function leapYearFebruaryChecks(firstDate, secondDate) {
514
565
  /**
515
566
  * Checks if the provided dates are exactly a whole number of months apart.
516
567
  *
568
+ * @category Date
569
+ *
517
570
  * @param firstDate - The first date to compare.
518
571
  * @param secondDate - The second date to compare.
519
572
  *
@@ -532,6 +585,8 @@ var isMonthlyMultiple_default = isMonthlyMultiple;
532
585
  /**
533
586
  * Asynchronously converts a file to a base 64 string
534
587
  *
588
+ * @category Miscellaneous
589
+ *
535
590
  * @param file - The file to convert.
536
591
  *
537
592
  * @throws {Error} If the file reader gives an error.
@@ -564,6 +619,8 @@ function getNullableResolutionStrategy(key, strategy) {
564
619
  /**
565
620
  * Creates FormData from a given object, resolving non-string types as appropriate.
566
621
  *
622
+ * @category Miscellaneous
623
+ *
567
624
  * @template DataType - The type of the given data.
568
625
  *
569
626
  * @param data - The data to create FormData from.
@@ -629,6 +686,8 @@ var createFormData_default = createFormData;
629
686
  /**
630
687
  * Checks to see if the given array is sorted in ascending order.
631
688
  *
689
+ * @category Miscellaneous
690
+ *
632
691
  * @param array - The array to check.
633
692
  *
634
693
  * @returns `true` if the array is sorted in ascending order, and `false` otherwise.
@@ -646,6 +705,8 @@ var isOrdered_default = isOrdered;
646
705
  /**
647
706
  * Converts a stringly-typed list to a proper array.
648
707
  *
708
+ * @category Miscellaneous
709
+ *
649
710
  * @param stringList - The stringly-typed list to convert.
650
711
  * @param options - The options to apply to the conversion.
651
712
  * @param options.separator - What each item in the list is separated by.
@@ -667,6 +728,8 @@ var stringListToArray_default = stringListToArray;
667
728
  /**
668
729
  * Waits for the given number of seconds
669
730
  *
731
+ * @category Miscellaneous
732
+ *
670
733
  * @param seconds - The number of seconds to wait.
671
734
  *
672
735
  * @returns A Promise that resolves after the given number of seconds.
@@ -685,6 +748,8 @@ var wait_default = wait;
685
748
  /**
686
749
  * Gets the keys from a given record object, properly typed to be an array of the key of the input object's type.
687
750
  *
751
+ * @category Object Helpers
752
+ *
688
753
  * @template InputRecordType - The type of the input object.
689
754
  *
690
755
  * @param record - The record to get the keys from.
@@ -701,6 +766,8 @@ var getRecordKeys_default = getRecordKeys;
701
766
  /**
702
767
  * Omits properties from a given object.
703
768
  *
769
+ * @category Object Helpers
770
+ *
704
771
  * @template ObjectType - The type of the input object.
705
772
  * @template KeysToOmit - A type representing the keys to omit from the object.
706
773
  *
@@ -723,6 +790,8 @@ var omitProperties_default = omitProperties;
723
790
  /**
724
791
  * Takes a stringly-typed boolean and converts it to an actual boolean type.
725
792
  *
793
+ * @category Parsers
794
+ *
726
795
  * @param inputString - The string to parse.
727
796
  *
728
797
  * @throws {DataError} If the string is not either `true` or `false` (case insensitive).
@@ -741,6 +810,8 @@ var parseBoolean_default = parseBoolean;
741
810
  /**
742
811
  * An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
743
812
  *
813
+ * @category Parsers
814
+ *
744
815
  * @template Output - The Zod output type.
745
816
  * @template Input - The Zod input type.
746
817
  * @template Internals - The Zod internal types based on the output and input types.
@@ -771,6 +842,8 @@ const envSchema = zod.z.enum([
771
842
  /**
772
843
  * Parses the input and verifies it matches one of the environments allowed by the Env types ("test" | "development" | "production").
773
844
  *
845
+ * @category Parsers
846
+ *
774
847
  * @param data - The data to parse.
775
848
  *
776
849
  * @throws {DataError} If the data does not match one of the environments allowed by the Env types ("test" | "development" | "production").
@@ -787,6 +860,8 @@ var parseEnv_default = parseEnv;
787
860
  /**
788
861
  * Returns an object given FormData and an optional data parser function to call on the resulting object.
789
862
  *
863
+ * @category Parsers
864
+ *
790
865
  * @template DataType - The type of the resulting object when called from the dataParser.
791
866
  *
792
867
  * @param formData - The FormData to parse.
@@ -809,6 +884,8 @@ var parseFormData_default = parseFormData;
809
884
  /**
810
885
  * Parses the input and verifies it is a valid software version type (i.e. `"major" | "minor" | "patch"`)
811
886
  *
887
+ * @category Parsers
888
+ *
812
889
  * @param data - The data to parse.
813
890
  *
814
891
  * @throws {DataError} If the data does not match one of the allowed version types (`"major" | "minor" | "patch"`).
@@ -828,6 +905,8 @@ function callDeepCopy(input) {
828
905
  /**
829
906
  * Deeply copies an object or array such that all child objects/arrays are also copied.
830
907
  *
908
+ * @category Recursive
909
+ *
831
910
  * @template ObjectType - The type of the input object.
832
911
  *
833
912
  * @param object - The object to copy. May also be an array.
@@ -855,6 +934,8 @@ var deepCopy_default = deepCopy;
855
934
  * Note that this will also freeze the input itself as well.
856
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.
857
936
  *
937
+ * @category Recursive
938
+ *
858
939
  * @template ObjectType - The type of the input object.
859
940
  *
860
941
  * @param object - The object to freeze. May also be an array.
@@ -875,6 +956,8 @@ var deepFreeze_default = deepFreeze;
875
956
  /**
876
957
  * Appends a semicolon to the end of a string, trimming where necessary first.
877
958
  *
959
+ * @category String Helpers
960
+ *
878
961
  * @param stringToAppendTo - The string to append a semicolon to.
879
962
  *
880
963
  * @throws {Error} If the string contains multiple lines.
@@ -894,6 +977,8 @@ var appendSemicolon_default = appendSemicolon;
894
977
  /**
895
978
  * Converts a string from camelCase to kebab-case
896
979
  *
980
+ * @category String Helpers
981
+ *
897
982
  * @param string - The string to convert.
898
983
  * @param options - Options to apply to the conversion.
899
984
  *
@@ -932,6 +1017,8 @@ var camelToKebab_default = camelToKebab;
932
1017
  /**
933
1018
  * Converts a string from kebab-case to camelCase
934
1019
  *
1020
+ * @category String Helpers
1021
+ *
935
1022
  * @param string - The string to convert.
936
1023
  * @param options - Options to apply to the conversion.
937
1024
  *
@@ -969,13 +1056,14 @@ var kebabToCamel_default = kebabToCamel;
969
1056
  //#region src/functions/stringHelpers/normalizeImportPath.ts
970
1057
  /**
971
1058
  * Normalizes an import path meant for use in an import statement in JavaScript.
972
- *
973
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.
974
1060
  *
975
1061
  * If the path starts with ./, it is preserved (unlike what would happen with path.posix.normalize() normally).
976
1062
  *
977
1063
  * Helpful for custom linter rules that need to check (or fix) import paths.
978
1064
  *
1065
+ * @category String Helpers
1066
+ *
979
1067
  * @param importPath - The import path to normalize.
980
1068
  *
981
1069
  * @returns The import path normalized.
@@ -987,13 +1075,14 @@ function normalizeImportPath(importPath) {
987
1075
  }
988
1076
  /**
989
1077
  * Normalises an import path meant for use in an import statement in JavaScript.
990
- *
991
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.
992
1079
  *
993
1080
  * If the path starts with ./, it is preserved (unlike what would happen with path.posix.normalize() normally).
994
1081
  *
995
1082
  * Helpful for custom linter rules that need to check (or fix) import paths.
996
1083
  *
1084
+ * @category String Helpers
1085
+ *
997
1086
  * @param importPath - The import path to normalise.
998
1087
  *
999
1088
  * @returns The import path normalised.
@@ -1006,6 +1095,8 @@ var normalizeImportPath_default = normalizeImportPath;
1006
1095
  /**
1007
1096
  * Truncates a string and appends `...` to the end of it
1008
1097
  *
1098
+ * @category String Helpers
1099
+ *
1009
1100
  * @param stringToTruncate - The string to truncate.
1010
1101
  * @param maxLength - The length at which to start truncating. Note that this does not include the `...` part that would be appended.
1011
1102
  *
@@ -1021,6 +1112,8 @@ var truncate_default = truncate;
1021
1112
  /**
1022
1113
  * Creates a template strings array given a regular array of strings
1023
1114
  *
1115
+ * @category Tagged Template
1116
+ *
1024
1117
  * @param strings - The array of strings.
1025
1118
  *
1026
1119
  * @returns A template strings array that can be passed as the first argument of any tagged template function.
@@ -1041,6 +1134,8 @@ var createTemplateStringsArray_default = createTemplateStringsArray;
1041
1134
  *
1042
1135
  * In this case, it will be functionally the same as if you just wrote the template string by itself.
1043
1136
  *
1137
+ * @category Tagged Template
1138
+ *
1044
1139
  * @param strings - The strings from the template to process.
1045
1140
  * @param interpolations - An array of all interpolations from the template.
1046
1141
  *
@@ -1062,6 +1157,8 @@ var interpolate_default = interpolate;
1062
1157
  *
1063
1158
  * interpolateObjects`Template string here ${{ my: "object" }}`.
1064
1159
  *
1160
+ * @category Tagged Template
1161
+ *
1065
1162
  * @param strings - The strings from the template to process.
1066
1163
  * @param interpolations - An array of all interpolations from the template.
1067
1164
  *
@@ -1118,6 +1215,8 @@ function reduceLines(lines, { preserveTabs = true }) {
1118
1215
  * with a new line
1119
1216
  * and another new line`.
1120
1217
  *
1218
+ *@category Tagged Template
1219
+ *
1121
1220
  * @param first - The strings from the template to process, or the options to apply.
1122
1221
  * @param args - An array of all interpolations from the template.
1123
1222
  *