@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.d.ts CHANGED
@@ -44,6 +44,8 @@ type ParallelTuple<A, B> = [A, B | undefined];
44
44
  * If `secondArray` is shorter than `firstArray`, the second position in the tuple
45
45
  * will be `undefined`. Iteration always uses the length of the first array.
46
46
  *
47
+ * @category Array Helpers
48
+ *
47
49
  * @template FirstArrayItem
48
50
  * @template SecondArrayItem
49
51
  *
@@ -58,6 +60,8 @@ declare function paralleliseArrays<FirstArrayItem, SecondArrayItem>(firstArray:
58
60
  /**
59
61
  * Randomises the order of a given array and returns the result in a new array without mutating the original.
60
62
  *
63
+ * @category Array Helpers
64
+ *
61
65
  * @template ItemType - The type of the array items.
62
66
  *
63
67
  * @param array - The array to randomise.
@@ -73,6 +77,8 @@ declare function randomiseArray<ItemType>(array: ItemType[]): ItemType[];
73
77
  * - The range is inclusive of `start` and exclusive of `stop`.
74
78
  * - The sign of `step` must match the direction of the range.
75
79
  *
80
+ * @category Array Helpers
81
+ *
76
82
  * @param start - The number to start at (inclusive).
77
83
  * @param stop - The number to stop at (exclusive).
78
84
  * @param step - The step size between numbers, defaulting to 1.
@@ -88,6 +94,8 @@ declare function range(start: number, stop: number, step?: number): number[];
88
94
  /**
89
95
  * Removes duplicate values from an array.
90
96
  *
97
+ * @category Array Helpers
98
+ *
91
99
  * @template ItemType - The type of the array items.
92
100
  *
93
101
  * @param array - The array to remove duplicates from.
@@ -100,6 +108,8 @@ declare function removeDuplicates<ItemType>(array: ItemType[] | readonly ItemTyp
100
108
  /**
101
109
  * Adds a given number of days to the provided date, returning the result as a new instance of Date.
102
110
  *
111
+ * @category Date
112
+ *
103
113
  * @param currentDate - The starting date (defaults to today).
104
114
  * @param dayIncrement - The amount of days you want to add (defaults to 1)
105
115
  *
@@ -111,6 +121,8 @@ declare function addDaysToDate(currentDate?: Date, dayIncrement?: number): Date;
111
121
  /**
112
122
  * Creates a human-readable string with information about the input date.
113
123
  *
124
+ * @category Date
125
+ *
114
126
  * @param inputDate - The date to base the string on.
115
127
  *
116
128
  * @returns A new string with information about the given date.
@@ -125,6 +137,8 @@ declare function formatDateAndTime(inputDate: Date): string;
125
137
  /**
126
138
  * Checks if the provided dates are exactly a whole number of years apart.
127
139
  *
140
+ * @category Date
141
+ *
128
142
  * @param firstDate - The first date to compare.
129
143
  * @param secondDate - The second date to compare.
130
144
  *
@@ -136,6 +150,8 @@ declare function isAnniversary(firstDate: Date, secondDate: Date): boolean;
136
150
  /**
137
151
  * Checks if the provided year is a leap year.
138
152
  *
153
+ * @category Date
154
+ *
139
155
  * @param year - The year to check as a number.
140
156
  *
141
157
  * @throws {TypeError} If the year provided is not an integer.
@@ -148,6 +164,8 @@ declare function isLeapYear(year: number): boolean;
148
164
  /**
149
165
  * Checks if the provided dates are exactly a whole number of months apart.
150
166
  *
167
+ * @category Date
168
+ *
151
169
  * @param firstDate - The first date to compare.
152
170
  * @param secondDate - The second date to compare.
153
171
  *
@@ -159,6 +177,8 @@ declare function isMonthlyMultiple(firstDate: Date, secondDate: Date): boolean;
159
177
  /**
160
178
  * Checks if the provided dates happen on the exact same day, month, and year.
161
179
  *
180
+ * @category Date
181
+ *
162
182
  * @param firstDate - The first date to compare.
163
183
  * @param secondDate - The second date to compare.
164
184
  *
@@ -170,6 +190,8 @@ declare function isSameDate(firstDate: Date, secondDate: Date): boolean;
170
190
  /**
171
191
  * Asynchronously converts a file to a base 64 string
172
192
  *
193
+ * @category Miscellaneous
194
+ *
173
195
  * @param file - The file to convert.
174
196
  *
175
197
  * @throws {Error} If the file reader gives an error.
@@ -181,7 +203,11 @@ declare function convertFileToBase64(file: File): Promise<string>;
181
203
  //#region src/types/APIError.d.ts
182
204
  type HTTPErrorCode = 400 | 401 | 403 | 404 | 418 | 500;
183
205
  declare const httpErrorCodeLookup: Record<HTTPErrorCode, string>;
184
- /** Represents common errors you may get from a HTTP API request. */
206
+ /**
207
+ * Represents common errors you may get from a HTTP API request.
208
+ *
209
+ * @category Types
210
+ */
185
211
  declare class APIError extends Error {
186
212
  status: number;
187
213
  /**
@@ -201,10 +227,14 @@ declare class APIError extends Error {
201
227
  }
202
228
  //#endregion
203
229
  //#region src/types/DataError.d.ts
204
- /** Represents errors you may get that may've been caused by a specific piece of data. */
230
+ /**
231
+ * Represents errors you may get that may've been caused by a specific piece of data.
232
+ *
233
+ * @category Types
234
+ */
205
235
  declare class DataError extends Error {
206
- data: unknown;
207
236
  code: string;
237
+ data: unknown;
208
238
  /**
209
239
  * @param data - The data that caused the error.
210
240
  * @param code - A standardised code (e.g. UNEXPECTED_DATA).
@@ -223,18 +253,29 @@ declare class DataError extends Error {
223
253
  }
224
254
  //#endregion
225
255
  //#region src/types/RecordKey.d.ts
226
- /** Represents the native Record's possible key type. */
256
+ /**
257
+ * Represents the native Record's possible key type.
258
+ *
259
+ * @category Types
260
+ */
227
261
  type RecordKey = string | number | symbol;
228
262
  //#endregion
229
263
  //#region src/types/CreateEnumType.d.ts
230
264
  /**
231
265
  * Get the value types from a const object so the object can behave similarly to an enum.
232
266
  *
267
+ * @category Types
268
+ *
233
269
  * @template ObjectType - The type of the object to get the value types for.
234
270
  */
235
271
  type CreateEnumType<ObjectType extends Record<RecordKey, unknown>> = ObjectType[keyof ObjectType];
236
272
  //#endregion
237
273
  //#region src/types/VersionType.d.ts
274
+ /**
275
+ * Represents the three common software version types.
276
+ *
277
+ * @category Types
278
+ */
238
279
  declare const VersionType: {
239
280
  readonly MAJOR: "major";
240
281
  readonly MINOR: "minor";
@@ -246,34 +287,39 @@ type VersionType = CreateEnumType<typeof VersionType>;
246
287
  interface ToStringOptions {
247
288
  omitPrefix?: boolean;
248
289
  }
249
- /** Represents a software version number, considered to be made up of a major, minor, and patch part. */
290
+ /**
291
+ * Represents a software version number, considered to be made up of a major, minor, and patch part.
292
+ *
293
+ * @category Types
294
+ */
250
295
  declare class VersionNumber {
296
+ private static readonly NON_NEGATIVE_TUPLE_ERROR;
251
297
  /** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
252
298
  readonly major: number;
253
299
  /** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
254
300
  readonly minor: number;
255
301
  /** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
256
302
  readonly patch: number;
257
- private static readonly NON_NEGATIVE_TUPLE_ERROR;
258
303
  /**
259
304
  * @param input - The input to create a new instance of `VersionNumber` from.
260
305
  */
261
306
  constructor(input: string | [number, number, number] | VersionNumber);
262
- private static formatString;
263
- /**
264
- * Get a string representation of the current version number.
265
- *
266
- * @param options - Extra additional options to apply.
267
- *
268
- * @returns A stringified representation of the current version number, leaving out the prefix if `omitPrefix` option was set to true.
269
- */
270
- toString(options?: ToStringOptions): string;
271
307
  /**
272
308
  * Gets the current version type of the current instance of `VersionNumber`.
273
309
  *
274
310
  * @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
275
311
  */
276
312
  get type(): VersionType;
313
+ private static formatString;
314
+ /**
315
+ * Checks if the provided version numbers have the exact same major, minor, and patch numbers.
316
+ *
317
+ * @param firstVersion - The first version number to compare.
318
+ * @param secondVersion - The second version number to compare.
319
+ *
320
+ * @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
321
+ */
322
+ static isEqual(firstVersion: VersionNumber, secondVersion: VersionNumber): boolean;
277
323
  /**
278
324
  * Determines whether the current instance of `VersionNumber` is a major, minor, or patch version.
279
325
  *
@@ -285,12 +331,22 @@ declare class VersionNumber {
285
331
  * @returns A new instance of `VersionNumber` with the increment applied.
286
332
  */
287
333
  increment(incrementType: VersionType): VersionNumber;
334
+ /**
335
+ * Get a string representation of the current version number.
336
+ *
337
+ * @param options - Extra additional options to apply.
338
+ *
339
+ * @returns A stringified representation of the current version number, leaving out the prefix if `omitPrefix` option was set to true.
340
+ */
341
+ toString(options?: ToStringOptions): string;
288
342
  }
289
343
  //#endregion
290
344
  //#region src/types/ArrayElement.d.ts
291
345
  /**
292
346
  * Gets the individual element types from an array type.
293
347
  *
348
+ * @category Types
349
+ *
294
350
  * @template ArrayType - The type of the array itself.
295
351
  */
296
352
  type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
@@ -299,6 +355,8 @@ type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends read
299
355
  /**
300
356
  * Resolves to an error message type if the type argument could potentially be undefined.
301
357
  *
358
+ * @category Types
359
+ *
302
360
  * @template InputType - The type to disallow undefined on.
303
361
  */
304
362
  type DisallowUndefined<InputType> = undefined extends InputType ? ["Error: Generic type cannot include undefined"] : InputType;
@@ -307,6 +365,8 @@ type DisallowUndefined<InputType> = undefined extends InputType ? ["Error: Gener
307
365
  /**
308
366
  * Allows case-insensitive variants of a known string type.
309
367
  *
368
+ * @category Types
369
+ *
310
370
  * @template StringType - The input string type.
311
371
  */
312
372
  type IgnoreCase<StringType extends string> = string extends StringType ? string : StringType extends `${infer FirstCharacter}${infer SecondCharacter}${infer Remainder}` ? `${Uppercase<FirstCharacter> | Lowercase<FirstCharacter>}${Uppercase<SecondCharacter> | Lowercase<SecondCharacter>}${IgnoreCase<Remainder>}` : StringType extends `${infer FirstCharacter}${infer Remainder}` ? `${Uppercase<FirstCharacter> | Lowercase<FirstCharacter>}${IgnoreCase<Remainder>}` : "";
@@ -315,6 +375,8 @@ type IgnoreCase<StringType extends string> = string extends StringType ? string
315
375
  /**
316
376
  * Resolves to `never` if the given type may be undefined.
317
377
  *
378
+ * @category Types
379
+ *
318
380
  * @template InputType - The type to check.
319
381
  */
320
382
  type NonUndefined<InputType> = InputType extends undefined ? never : InputType;
@@ -323,6 +385,8 @@ type NonUndefined<InputType> = InputType extends undefined ? never : InputType;
323
385
  /**
324
386
  * Resolves to the given type if the first type is `true`, otherwise resolves to `undefined`
325
387
  *
388
+ * @category Types
389
+ *
326
390
  * @param Condition - The condition to check.
327
391
  * @param ResolvedTypeIfTrue - The type to resolve to if the condition may be `true`.
328
392
  */
@@ -355,6 +419,8 @@ type CreateFormDataOptions<Key extends RecordKey> = CreateFormDataOptionsUndefin
355
419
  /**
356
420
  * Creates FormData from a given object, resolving non-string types as appropriate.
357
421
  *
422
+ * @category Miscellaneous
423
+ *
358
424
  * @template DataType - The type of the given data.
359
425
  *
360
426
  * @param data - The data to create FormData from.
@@ -368,6 +434,8 @@ declare function createFormData<DataType extends Record<RecordKey, unknown>>(dat
368
434
  /**
369
435
  * Gets a random number between the given bounds.
370
436
  *
437
+ * @category Miscellaneous
438
+ *
371
439
  * @param lowerBound - The lowest number that can be chosen.
372
440
  * @param upperBound - The highest number that can be chosen.
373
441
  *
@@ -379,6 +447,8 @@ declare function getRandomNumber(lowerBound: number, upperBound: number): number
379
447
  /**
380
448
  * Checks to see if the given array is sorted in ascending order.
381
449
  *
450
+ * @category Miscellaneous
451
+ *
382
452
  * @param array - The array to check.
383
453
  *
384
454
  * @returns `true` if the array is sorted in ascending order, and `false` otherwise.
@@ -395,6 +465,8 @@ interface StringListToArrayOptions {
395
465
  /**
396
466
  * Converts a stringly-typed list to a proper array.
397
467
  *
468
+ * @category Miscellaneous
469
+ *
398
470
  * @param stringList - The stringly-typed list to convert.
399
471
  * @param options - The options to apply to the conversion.
400
472
  * @param options.separator - What each item in the list is separated by.
@@ -411,6 +483,8 @@ declare function stringListToArray(stringList: string, {
411
483
  /**
412
484
  * Waits for the given number of seconds
413
485
  *
486
+ * @category Miscellaneous
487
+ *
414
488
  * @param seconds - The number of seconds to wait.
415
489
  *
416
490
  * @returns A Promise that resolves after the given number of seconds.
@@ -421,6 +495,8 @@ declare function wait(seconds: number): Promise<void>;
421
495
  /**
422
496
  * Gets the keys from a given record object, properly typed to be an array of the key of the input object's type.
423
497
  *
498
+ * @category Object Helpers
499
+ *
424
500
  * @template InputRecordType - The type of the input object.
425
501
  *
426
502
  * @param record - The record to get the keys from.
@@ -433,6 +509,8 @@ declare function getRecordKeys<InputRecordType extends Record<RecordKey, unknown
433
509
  /**
434
510
  * Omits properties from a given object.
435
511
  *
512
+ * @category Object Helpers
513
+ *
436
514
  * @template ObjectType - The type of the input object.
437
515
  * @template KeysToOmit - A type representing the keys to omit from the object.
438
516
  *
@@ -447,6 +525,8 @@ declare function omitProperties<ObjectType extends Record<string, unknown> | Rea
447
525
  /**
448
526
  * Takes a stringly-typed boolean and converts it to an actual boolean type.
449
527
  *
528
+ * @category Parsers
529
+ *
450
530
  * @param inputString - The string to parse.
451
531
  *
452
532
  * @throws {DataError} If the string is not either `true` or `false` (case insensitive).
@@ -461,11 +541,17 @@ declare const envSchema: z$1.ZodEnum<{
461
541
  development: "development";
462
542
  production: "production";
463
543
  }>;
464
- /** Represents the most common development environments */
544
+ /**
545
+ * Represents the most common development environments
546
+ *
547
+ * @category Types
548
+ */
465
549
  type Env = z$1.infer<typeof envSchema>;
466
550
  /**
467
551
  * Parses the input and verifies it matches one of the environments allowed by the Env types ("test" | "development" | "production").
468
552
  *
553
+ * @category Parsers
554
+ *
469
555
  * @param data - The data to parse.
470
556
  *
471
557
  * @throws {DataError} If the data does not match one of the environments allowed by the Env types ("test" | "development" | "production").
@@ -499,6 +585,8 @@ declare function parseFormData(formData: FormData): Record<string, string | Blob
499
585
  /**
500
586
  * Converts a string to an integer and throws an error if it cannot be converted.
501
587
  *
588
+ * @category Parsers
589
+ *
502
590
  * @param string - A string to convert into a number.
503
591
  * @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.
504
592
  *
@@ -512,6 +600,8 @@ declare function parseIntStrict(string: string, radix?: number): number;
512
600
  /**
513
601
  * Parses the input and verifies it is a valid software version type (i.e. `"major" | "minor" | "patch"`)
514
602
  *
603
+ * @category Parsers
604
+ *
515
605
  * @param data - The data to parse.
516
606
  *
517
607
  * @throws {DataError} If the data does not match one of the allowed version types (`"major" | "minor" | "patch"`).
@@ -524,6 +614,8 @@ declare function parseVersionType(data: unknown): VersionType;
524
614
  /**
525
615
  * An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
526
616
  *
617
+ * @category Parsers
618
+ *
527
619
  * @template Output - The Zod output type.
528
620
  * @template Input - The Zod input type.
529
621
  * @template Internals - The Zod internal types based on the output and input types.
@@ -543,6 +635,8 @@ declare function parseZodSchema<Output, Input, Internals extends core.$ZodTypeIn
543
635
  /**
544
636
  * Deeply copies an object or array such that all child objects/arrays are also copied.
545
637
  *
638
+ * @category Recursive
639
+ *
546
640
  * @template ObjectType - The type of the input object.
547
641
  *
548
642
  * @param object - The object to copy. May also be an array.
@@ -558,6 +652,8 @@ declare function deepCopy<ObjectType extends object>(object: ObjectType): Object
558
652
  * Note that this will also freeze the input itself as well.
559
653
  * 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.
560
654
  *
655
+ * @category Recursive
656
+ *
561
657
  * @template ObjectType - The type of the input object.
562
658
  *
563
659
  * @param object - The object to freeze. May also be an array.
@@ -570,6 +666,8 @@ declare function deepFreeze<ObjectType extends object>(object: ObjectType): Read
570
666
  /**
571
667
  * Appends a semicolon to the end of a string, trimming where necessary first.
572
668
  *
669
+ * @category String Helpers
670
+ *
573
671
  * @param stringToAppendTo - The string to append a semicolon to.
574
672
  *
575
673
  * @throws {Error} If the string contains multiple lines.
@@ -586,6 +684,8 @@ interface CamelToKebabOptions {
586
684
  /**
587
685
  * Converts a string from camelCase to kebab-case
588
686
  *
687
+ * @category String Helpers
688
+ *
589
689
  * @param string - The string to convert.
590
690
  * @param options - Options to apply to the conversion.
591
691
  *
@@ -601,6 +701,8 @@ interface KebabToCamelOptions {
601
701
  /**
602
702
  * Converts a string from kebab-case to camelCase
603
703
  *
704
+ * @category String Helpers
705
+ *
604
706
  * @param string - The string to convert.
605
707
  * @param options - Options to apply to the conversion.
606
708
  *
@@ -611,13 +713,14 @@ declare function kebabToCamel(string: string, options?: KebabToCamelOptions): st
611
713
  //#region src/functions/stringHelpers/normalizeImportPath.d.ts
612
714
  /**
613
715
  * Normalizes an import path meant for use in an import statement in JavaScript.
614
- *
615
716
  * 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.
616
717
  *
617
718
  * If the path starts with ./, it is preserved (unlike what would happen with path.posix.normalize() normally).
618
719
  *
619
720
  * Helpful for custom linter rules that need to check (or fix) import paths.
620
721
  *
722
+ * @category String Helpers
723
+ *
621
724
  * @param importPath - The import path to normalize.
622
725
  *
623
726
  * @returns The import path normalized.
@@ -625,13 +728,14 @@ declare function kebabToCamel(string: string, options?: KebabToCamelOptions): st
625
728
  declare function normalizeImportPath(importPath: string): string;
626
729
  /**
627
730
  * Normalises an import path meant for use in an import statement in JavaScript.
628
- *
629
731
  * 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.
630
732
  *
631
733
  * If the path starts with ./, it is preserved (unlike what would happen with path.posix.normalize() normally).
632
734
  *
633
735
  * Helpful for custom linter rules that need to check (or fix) import paths.
634
736
  *
737
+ * @category String Helpers
738
+ *
635
739
  * @param importPath - The import path to normalise.
636
740
  *
637
741
  * @returns The import path normalised.
@@ -642,6 +746,8 @@ declare const normaliseImportPath: typeof normalizeImportPath;
642
746
  /**
643
747
  * Truncates a string and appends `...` to the end of it
644
748
  *
749
+ * @category String Helpers
750
+ *
645
751
  * @param stringToTruncate - The string to truncate.
646
752
  * @param maxLength - The length at which to start truncating. Note that this does not include the `...` part that would be appended.
647
753
  *
@@ -653,6 +759,8 @@ declare function truncate(stringToTruncate: string, maxLength?: number): string;
653
759
  /**
654
760
  * Creates a template strings array given a regular array of strings
655
761
  *
762
+ * @category Tagged Template
763
+ *
656
764
  * @param strings - The array of strings.
657
765
  *
658
766
  * @returns A template strings array that can be passed as the first argument of any tagged template function.
@@ -669,6 +777,8 @@ declare function createTemplateStringsArray(strings: readonly string[]): Templat
669
777
  *
670
778
  * In this case, it will be functionally the same as if you just wrote the template string by itself.
671
779
  *
780
+ * @category Tagged Template
781
+ *
672
782
  * @param strings - The strings from the template to process.
673
783
  * @param interpolations - An array of all interpolations from the template.
674
784
  *
@@ -684,6 +794,8 @@ declare function interpolate(strings: TemplateStringsArray, ...interpolations: u
684
794
  *
685
795
  * interpolateObjects`Template string here ${{ my: "object" }}`.
686
796
  *
797
+ * @category Tagged Template
798
+ *
687
799
  * @param strings - The strings from the template to process.
688
800
  * @param interpolations - An array of all interpolations from the template.
689
801
  *
@@ -702,6 +814,8 @@ type NormalizeIndentsFunction = NormaliseIndentsFunction;
702
814
  /**
703
815
  * Provides a new function that removes any extraneous indents from a multi-line template string, with the given options applied.
704
816
  *
817
+ * @category Tagged Template
818
+ *
705
819
  * @param options - The options to apply.
706
820
  *
707
821
  * @returns A function that takes a template string, and returns a new string with the strings and interpolations from the template applied, and extraneous indents removed.
@@ -716,6 +830,8 @@ declare function normaliseIndents(options: NormaliseIndentsOptions): NormaliseIn
716
830
  * with a new line
717
831
  * and another new line`.
718
832
  *
833
+ *@category Tagged Template
834
+ *
719
835
  * @param strings - The strings from the template to process.
720
836
  * @param interpolations - An array of all interpolations from the template.
721
837
  *