@alextheman/utility 5.20.0 → 5.21.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.
package/dist/index.cjs CHANGED
@@ -827,6 +827,20 @@ function createFormData(data, options = {
827
827
  return formData;
828
828
  }
829
829
  //#endregion
830
+ //#region src/root/functions/miscellaneous/identity.ts
831
+ /**
832
+ * Gives back the original input.
833
+ *
834
+ * @template InputType - The input type.
835
+ *
836
+ * @param input - The input value.
837
+ *
838
+ * @returns The original input value.
839
+ */
840
+ function identity(input) {
841
+ return input;
842
+ }
843
+ //#endregion
830
844
  //#region src/root/functions/miscellaneous/isOrdered.ts
831
845
  /**
832
846
  * Checks to see if the given array is sorted in ascending order.
@@ -957,6 +971,33 @@ I'll commit to you!
957
971
  `;
958
972
  }
959
973
  //#endregion
974
+ //#region src/root/functions/miscellaneous/sortBy.ts
975
+ /**
976
+ * Provides a function to pass to the `sort` or `toSorted` methods on arrays, which will allow those methods to sort the items by the chosen value in the chosen direction.
977
+ *
978
+ * @template ItemType - The type of each array item.
979
+ * @template SortValue - The type of the value to sort by.
980
+ *
981
+ * @param selector - A function that takes the item and returns the value to sort by.
982
+ * @param direction - The sort direction.
983
+ *
984
+ * @returns A function to pass into the `.sort()` or `.toSorted()` methods on arrays.
985
+ */
986
+ function sortBy(selector, direction = "asc") {
987
+ return (first, second) => {
988
+ const firstValue = selector(first);
989
+ const secondValue = selector(second);
990
+ if (firstValue instanceof Date && secondValue instanceof Date) {
991
+ const firstTime = firstValue.getTime();
992
+ const secondTime = secondValue.getTime();
993
+ return direction === "asc" ? firstTime - secondTime : secondTime - firstTime;
994
+ }
995
+ if (firstValue < secondValue) return direction === "asc" ? -1 : 1;
996
+ if (firstValue > secondValue) return direction === "asc" ? 1 : -1;
997
+ return 0;
998
+ };
999
+ }
1000
+ //#endregion
960
1001
  //#region src/root/functions/miscellaneous/stringifyDotenv.ts
961
1002
  /**
962
1003
  * Converts an object into a string in .env file format.
@@ -1101,6 +1142,12 @@ function parseBoolean(inputString) {
1101
1142
  return normalisedString === "true";
1102
1143
  }
1103
1144
  //#endregion
1145
+ //#region src/root/types/SortDirection.ts
1146
+ const SortDirection = {
1147
+ DESC: "desc",
1148
+ ASC: "asc"
1149
+ };
1150
+ //#endregion
1104
1151
  //#region src/root/types/VersionNumber.ts
1105
1152
  /**
1106
1153
  * Represents a software version number, considered to be made up of a major, minor, and patch part.
@@ -1966,6 +2013,7 @@ exports.Env = Env;
1966
2013
  exports.FILE_PATH_PATTERN = FILE_PATH_PATTERN;
1967
2014
  exports.FILE_PATH_REGEX = FILE_PATH_REGEX;
1968
2015
  exports.ONE_DAY_IN_MILLISECONDS = ONE_DAY_IN_MILLISECONDS;
2016
+ exports.SortDirection = SortDirection;
1969
2017
  exports.UUID_PATTERN = UUID_PATTERN;
1970
2018
  exports.UUID_REGEX = UUID_REGEX;
1971
2019
  exports.VERSION_NUMBER_PATTERN = VERSION_NUMBER_PATTERN;
@@ -1993,6 +2041,7 @@ exports.getRandomNumber = getRandomNumber;
1993
2041
  exports.getRecordKeys = getRecordKeys;
1994
2042
  exports.getStringsAndInterpolations = getStringsAndInterpolations;
1995
2043
  exports.httpErrorCodeLookup = httpErrorCodeLookup;
2044
+ exports.identity = identity;
1996
2045
  exports.interpolate = interpolate;
1997
2046
  exports.interpolateObjects = interpolateObjects;
1998
2047
  exports.isAnniversary = isAnniversary;
@@ -2021,6 +2070,7 @@ exports.range = range;
2021
2070
  exports.removeDuplicates = removeDuplicates;
2022
2071
  exports.removeUndefinedFromObject = removeUndefinedFromObject;
2023
2072
  exports.sayHello = sayHello;
2073
+ exports.sortBy = sortBy;
2024
2074
  exports.stringListToArray = stringListToArray;
2025
2075
  exports.stringifyDotenv = stringifyDotenv;
2026
2076
  exports.toTitleCase = toTitleCase;
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { DotenvParseOutput } from "dotenv";
2
1
  import z, { ZodCoercedDate, ZodCoercedNumber, ZodError, ZodType, z as z$1 } from "zod";
2
+ import { DotenvParseOutput } from "dotenv";
3
3
 
4
4
  //#region src/root/constants/FILE_PATH_REGEX.d.ts
5
5
  declare const FILE_PATH_PATTERN: string;
@@ -402,6 +402,18 @@ declare function createFormData<DataType extends Record<PropertyKey, unknown>>(d
402
402
  */
403
403
  declare function getRandomNumber(lowerBound: number, upperBound: number): number;
404
404
  //#endregion
405
+ //#region src/root/functions/miscellaneous/identity.d.ts
406
+ /**
407
+ * Gives back the original input.
408
+ *
409
+ * @template InputType - The input type.
410
+ *
411
+ * @param input - The input value.
412
+ *
413
+ * @returns The original input value.
414
+ */
415
+ declare function identity<InputType>(input: InputType): InputType;
416
+ //#endregion
405
417
  //#region src/root/functions/miscellaneous/isOrdered.d.ts
406
418
  /**
407
419
  * Checks to see if the given array is sorted in ascending order.
@@ -424,117 +436,22 @@ declare function isOrdered(array: ReadonlyArray<number>): boolean;
424
436
  */
425
437
  declare function sayHello(): string;
426
438
  //#endregion
427
- //#region src/root/functions/miscellaneous/stringifyDotenv.d.ts
428
- type QuoteStyle = "double" | "single" | "none";
429
- interface StringifyDotenvOptions {
430
- /** The quote style to use for the values (defaults to `"double"`) */
431
- quoteStyle: QuoteStyle;
432
- }
433
- /**
434
- * Converts an object into a string in .env file format.
435
- *
436
- * @param contents - The object to convert. Must be a record whose values are strings.
437
- * @param options - Extra options to apply.
438
- *
439
- * @returns A string representation of the object in .env file format.
440
- */
441
- declare function stringifyDotenv(contents: Record<PropertyKey, string> | DotenvParseOutput, options?: StringifyDotenvOptions): string;
442
- //#endregion
443
- //#region src/root/functions/miscellaneous/stringListToArray.d.ts
444
- /**
445
- * Options to apply to the conversion of a string list to an array.
446
- *
447
- * @category Function Options
448
- */
449
- interface StringListToArrayOptions {
450
- /** What each item in the list is separated by. */
451
- separator?: string;
452
- /** An option to trim any extra whitespace. */
453
- trimWhitespace?: boolean;
454
- }
455
- /**
456
- * Converts a stringly-typed list to a proper array.
457
- *
458
- * @category Miscellaneous
459
- *
460
- * @param stringList - The stringly-typed list to convert.
461
- * @param options - The options to apply to the conversion.
462
- * @param options.separator - What each item in the list is separated by.
463
- * @param options.trimWhitespace - An option to trim any extra whitespace.
464
- *
465
- * @returns A new array with each item being an item from the given list.
466
- */
467
- declare function stringListToArray(stringList: string, {
468
- separator,
469
- trimWhitespace
470
- }?: StringListToArrayOptions): Array<string>;
471
- //#endregion
472
- //#region src/root/functions/miscellaneous/wait.d.ts
473
- /**
474
- * Waits for the given number of seconds
475
- *
476
- * @category Miscellaneous
477
- *
478
- * @param seconds - The number of seconds to wait.
479
- *
480
- * @returns A Promise that resolves after the given number of seconds.
481
- */
482
- declare function wait(seconds: number): Promise<void>;
483
- //#endregion
484
- //#region src/root/functions/objectHelpers/getRecordKeys.d.ts
485
- /**
486
- * Gets the keys from a given record object, properly typed to be an array of the key of the input object's type.
487
- *
488
- * @category Object Helpers
489
- *
490
- * @template InputRecordType - The type of the input object.
491
- *
492
- * @param record - The record to get the keys from.
493
- *
494
- * @returns An array with all the keys of the input object in string form, but properly typed as `keyof InputRecordType`.
495
- */
496
- declare function getRecordKeys<InputRecordType extends Record<PropertyKey, unknown>>(record: InputRecordType & object): Array<keyof InputRecordType>;
497
- //#endregion
498
- //#region src/root/functions/objectHelpers/omitProperties.d.ts
499
- /**
500
- * Omits properties from a given object.
501
- *
502
- * @category Object Helpers
503
- *
504
- * @template ObjectType - The type of the input object.
505
- * @template KeysToOmit - A type representing the keys to omit from the object.
506
- *
507
- * @param object - The object to omit properties from.
508
- * @param keysToOmit - The keys to omit from the object. Can either be a single string to omit one, or an array to omit multiple.
509
- *
510
- * @returns An object with a new reference in memory, with the properties omitted.
511
- */
512
- declare function omitProperties<ObjectType extends object, KeysToOmit extends keyof ObjectType>(object: ObjectType, keysToOmit: KeysToOmit | ReadonlyArray<KeysToOmit>): Omit<ObjectType, KeysToOmit>;
513
- //#endregion
514
- //#region src/root/functions/objectHelpers/removeUndefinedFromObject.d.ts
515
- type RemoveUndefined<RecordType extends Record<PropertyKey, unknown>> = { [Key in keyof RecordType]: Exclude<RecordType[Key], undefined> };
439
+ //#region src/root/types/CreateEnumType.d.ts
516
440
  /**
517
- * Removes entries whose values are `undefined` from a given object (not including null etc.).
441
+ * Get the value types from a const object so the object can behave similarly to an enum.
518
442
  *
519
- * @param object - The object to remove undefined entries from.
443
+ * @category Types
520
444
  *
521
- * @returns An object with a new reference in memory, with undefined entries removed.
445
+ * @template ObjectType - The type of the object to get the value types for.
522
446
  */
523
- declare function removeUndefinedFromObject<RecordType extends Record<PropertyKey, unknown>>(object: RecordType): RemoveUndefined<RecordType>;
447
+ type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
524
448
  //#endregion
525
- //#region src/root/functions/parsers/parseBoolean.d.ts
526
- /**
527
- * Takes a stringly-typed boolean and converts it to an actual boolean type.
528
- *
529
- * @category Parsers
530
- *
531
- * @param inputString - The string to parse.
532
- *
533
- * @throws {DataError} If the string is not either `true` or `false` (case insensitive).
534
- *
535
- * @returns The string parsed as an actual boolean.
536
- */
537
- declare function parseBoolean(inputString: string): boolean;
449
+ //#region src/root/types/SortDirection.d.ts
450
+ declare const SortDirection: {
451
+ readonly DESC: "desc";
452
+ readonly ASC: "asc";
453
+ };
454
+ type SortDirection = CreateEnumType<typeof SortDirection>;
538
455
  //#endregion
539
456
  //#region src/root/types/VersionNumber.d.ts
540
457
  /**
@@ -648,16 +565,6 @@ type ArrayElement<ArrayType extends ReadonlyArray<unknown>> = ArrayType extends
648
565
  //#region src/root/types/CallReturnType.d.ts
649
566
  type CallReturnType<Function, Arguments> = Function extends ((arg: Arguments) => infer Return) ? Return : never;
650
567
  //#endregion
651
- //#region src/root/types/CreateEnumType.d.ts
652
- /**
653
- * Get the value types from a const object so the object can behave similarly to an enum.
654
- *
655
- * @category Types
656
- *
657
- * @template ObjectType - The type of the object to get the value types for.
658
- */
659
- type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
660
- //#endregion
661
568
  //#region src/root/types/DisallowUndefined.d.ts
662
569
  /**
663
570
  * Resolves to an error message type if the type argument could potentially be undefined.
@@ -723,6 +630,133 @@ type NullableOnCondition<Condition extends boolean, ResolvedTypeIfTrue> = Condit
723
630
  */
724
631
  type OptionalOnCondition<Condition extends boolean, ResolvedTypeIfTrue> = Condition extends true ? ResolvedTypeIfTrue : ResolvedTypeIfTrue | undefined;
725
632
  //#endregion
633
+ //#region src/root/functions/miscellaneous/sortBy.d.ts
634
+ type SortFunction<ItemType> = (first: ItemType, second: ItemType) => number;
635
+ /**
636
+ * Provides a function to pass to the `sort` or `toSorted` methods on arrays, which will allow those methods to sort the items by the chosen value in the chosen direction.
637
+ *
638
+ * @template ItemType - The type of each array item.
639
+ * @template SortValue - The type of the value to sort by.
640
+ *
641
+ * @param selector - A function that takes the item and returns the value to sort by.
642
+ * @param direction - The sort direction.
643
+ *
644
+ * @returns A function to pass into the `.sort()` or `.toSorted()` methods on arrays.
645
+ */
646
+ declare function sortBy<ItemType, SortValue extends string | number | Date>(selector: (item: ItemType) => SortValue, direction?: SortDirection): SortFunction<ItemType>;
647
+ //#endregion
648
+ //#region src/root/functions/miscellaneous/stringifyDotenv.d.ts
649
+ type QuoteStyle = "double" | "single" | "none";
650
+ interface StringifyDotenvOptions {
651
+ /** The quote style to use for the values (defaults to `"double"`) */
652
+ quoteStyle: QuoteStyle;
653
+ }
654
+ /**
655
+ * Converts an object into a string in .env file format.
656
+ *
657
+ * @param contents - The object to convert. Must be a record whose values are strings.
658
+ * @param options - Extra options to apply.
659
+ *
660
+ * @returns A string representation of the object in .env file format.
661
+ */
662
+ declare function stringifyDotenv(contents: Record<PropertyKey, string> | DotenvParseOutput, options?: StringifyDotenvOptions): string;
663
+ //#endregion
664
+ //#region src/root/functions/miscellaneous/stringListToArray.d.ts
665
+ /**
666
+ * Options to apply to the conversion of a string list to an array.
667
+ *
668
+ * @category Function Options
669
+ */
670
+ interface StringListToArrayOptions {
671
+ /** What each item in the list is separated by. */
672
+ separator?: string;
673
+ /** An option to trim any extra whitespace. */
674
+ trimWhitespace?: boolean;
675
+ }
676
+ /**
677
+ * Converts a stringly-typed list to a proper array.
678
+ *
679
+ * @category Miscellaneous
680
+ *
681
+ * @param stringList - The stringly-typed list to convert.
682
+ * @param options - The options to apply to the conversion.
683
+ * @param options.separator - What each item in the list is separated by.
684
+ * @param options.trimWhitespace - An option to trim any extra whitespace.
685
+ *
686
+ * @returns A new array with each item being an item from the given list.
687
+ */
688
+ declare function stringListToArray(stringList: string, {
689
+ separator,
690
+ trimWhitespace
691
+ }?: StringListToArrayOptions): Array<string>;
692
+ //#endregion
693
+ //#region src/root/functions/miscellaneous/wait.d.ts
694
+ /**
695
+ * Waits for the given number of seconds
696
+ *
697
+ * @category Miscellaneous
698
+ *
699
+ * @param seconds - The number of seconds to wait.
700
+ *
701
+ * @returns A Promise that resolves after the given number of seconds.
702
+ */
703
+ declare function wait(seconds: number): Promise<void>;
704
+ //#endregion
705
+ //#region src/root/functions/objectHelpers/getRecordKeys.d.ts
706
+ /**
707
+ * Gets the keys from a given record object, properly typed to be an array of the key of the input object's type.
708
+ *
709
+ * @category Object Helpers
710
+ *
711
+ * @template InputRecordType - The type of the input object.
712
+ *
713
+ * @param record - The record to get the keys from.
714
+ *
715
+ * @returns An array with all the keys of the input object in string form, but properly typed as `keyof InputRecordType`.
716
+ */
717
+ declare function getRecordKeys<InputRecordType extends Record<PropertyKey, unknown>>(record: InputRecordType & object): Array<keyof InputRecordType>;
718
+ //#endregion
719
+ //#region src/root/functions/objectHelpers/omitProperties.d.ts
720
+ /**
721
+ * Omits properties from a given object.
722
+ *
723
+ * @category Object Helpers
724
+ *
725
+ * @template ObjectType - The type of the input object.
726
+ * @template KeysToOmit - A type representing the keys to omit from the object.
727
+ *
728
+ * @param object - The object to omit properties from.
729
+ * @param keysToOmit - The keys to omit from the object. Can either be a single string to omit one, or an array to omit multiple.
730
+ *
731
+ * @returns An object with a new reference in memory, with the properties omitted.
732
+ */
733
+ declare function omitProperties<ObjectType extends object, KeysToOmit extends keyof ObjectType>(object: ObjectType, keysToOmit: KeysToOmit | ReadonlyArray<KeysToOmit>): Omit<ObjectType, KeysToOmit>;
734
+ //#endregion
735
+ //#region src/root/functions/objectHelpers/removeUndefinedFromObject.d.ts
736
+ type RemoveUndefined<RecordType extends Record<PropertyKey, unknown>> = { [Key in keyof RecordType]: Exclude<RecordType[Key], undefined> };
737
+ /**
738
+ * Removes entries whose values are `undefined` from a given object (not including null etc.).
739
+ *
740
+ * @param object - The object to remove undefined entries from.
741
+ *
742
+ * @returns An object with a new reference in memory, with undefined entries removed.
743
+ */
744
+ declare function removeUndefinedFromObject<RecordType extends Record<PropertyKey, unknown>>(object: RecordType): RemoveUndefined<RecordType>;
745
+ //#endregion
746
+ //#region src/root/functions/parsers/parseBoolean.d.ts
747
+ /**
748
+ * Takes a stringly-typed boolean and converts it to an actual boolean type.
749
+ *
750
+ * @category Parsers
751
+ *
752
+ * @param inputString - The string to parse.
753
+ *
754
+ * @throws {DataError} If the string is not either `true` or `false` (case insensitive).
755
+ *
756
+ * @returns The string parsed as an actual boolean.
757
+ */
758
+ declare function parseBoolean(inputString: string): boolean;
759
+ //#endregion
726
760
  //#region src/root/functions/parsers/parseEnv.d.ts
727
761
  /**
728
762
  * Represents the three common development environments.
@@ -1423,4 +1457,4 @@ interface ZodParsingErrorData {
1423
1457
  issues: Array<z.core.$ZodIssue>;
1424
1458
  }
1425
1459
  //#endregion
1426
- export { APIError, type ArrayElement, type CallReturnType, type CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, type KebabToCamelOptions, type NonNull, type NonUndefined, type NormaliseIndentsFunction, type NormaliseIndentsOptions, type NormalizeIndentsFunction, type NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, type ParallelTuple, type RecordKey, type RemoveUndefined, type StringListToArrayOptions, type ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, type ZodParsingErrorData, addDaysToDate, appendSemicolon, assertNotNull, assertNotNullable, assertNotUndefined, az, calculateMonthlyDifference, camelToKebab, containsKeys, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isNonNullableObject, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, objectContainsKeys, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
1460
+ export { APIError, type ArrayElement, type CallReturnType, type CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, type KebabToCamelOptions, type NonNull, type NonUndefined, type NormaliseIndentsFunction, type NormaliseIndentsOptions, type NormalizeIndentsFunction, type NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, type ParallelTuple, type RecordKey, type RemoveUndefined, SortDirection, type StringListToArrayOptions, type ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, type ZodParsingErrorData, addDaysToDate, appendSemicolon, assertNotNull, assertNotNullable, assertNotUndefined, az, calculateMonthlyDifference, camelToKebab, containsKeys, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, identity, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isNonNullableObject, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, objectContainsKeys, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, sortBy, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
package/dist/index.d.ts CHANGED
@@ -402,6 +402,18 @@ declare function createFormData<DataType extends Record<PropertyKey, unknown>>(d
402
402
  */
403
403
  declare function getRandomNumber(lowerBound: number, upperBound: number): number;
404
404
  //#endregion
405
+ //#region src/root/functions/miscellaneous/identity.d.ts
406
+ /**
407
+ * Gives back the original input.
408
+ *
409
+ * @template InputType - The input type.
410
+ *
411
+ * @param input - The input value.
412
+ *
413
+ * @returns The original input value.
414
+ */
415
+ declare function identity<InputType>(input: InputType): InputType;
416
+ //#endregion
405
417
  //#region src/root/functions/miscellaneous/isOrdered.d.ts
406
418
  /**
407
419
  * Checks to see if the given array is sorted in ascending order.
@@ -424,117 +436,22 @@ declare function isOrdered(array: ReadonlyArray<number>): boolean;
424
436
  */
425
437
  declare function sayHello(): string;
426
438
  //#endregion
427
- //#region src/root/functions/miscellaneous/stringifyDotenv.d.ts
428
- type QuoteStyle = "double" | "single" | "none";
429
- interface StringifyDotenvOptions {
430
- /** The quote style to use for the values (defaults to `"double"`) */
431
- quoteStyle: QuoteStyle;
432
- }
433
- /**
434
- * Converts an object into a string in .env file format.
435
- *
436
- * @param contents - The object to convert. Must be a record whose values are strings.
437
- * @param options - Extra options to apply.
438
- *
439
- * @returns A string representation of the object in .env file format.
440
- */
441
- declare function stringifyDotenv(contents: Record<PropertyKey, string> | DotenvParseOutput, options?: StringifyDotenvOptions): string;
442
- //#endregion
443
- //#region src/root/functions/miscellaneous/stringListToArray.d.ts
444
- /**
445
- * Options to apply to the conversion of a string list to an array.
446
- *
447
- * @category Function Options
448
- */
449
- interface StringListToArrayOptions {
450
- /** What each item in the list is separated by. */
451
- separator?: string;
452
- /** An option to trim any extra whitespace. */
453
- trimWhitespace?: boolean;
454
- }
455
- /**
456
- * Converts a stringly-typed list to a proper array.
457
- *
458
- * @category Miscellaneous
459
- *
460
- * @param stringList - The stringly-typed list to convert.
461
- * @param options - The options to apply to the conversion.
462
- * @param options.separator - What each item in the list is separated by.
463
- * @param options.trimWhitespace - An option to trim any extra whitespace.
464
- *
465
- * @returns A new array with each item being an item from the given list.
466
- */
467
- declare function stringListToArray(stringList: string, {
468
- separator,
469
- trimWhitespace
470
- }?: StringListToArrayOptions): Array<string>;
471
- //#endregion
472
- //#region src/root/functions/miscellaneous/wait.d.ts
473
- /**
474
- * Waits for the given number of seconds
475
- *
476
- * @category Miscellaneous
477
- *
478
- * @param seconds - The number of seconds to wait.
479
- *
480
- * @returns A Promise that resolves after the given number of seconds.
481
- */
482
- declare function wait(seconds: number): Promise<void>;
483
- //#endregion
484
- //#region src/root/functions/objectHelpers/getRecordKeys.d.ts
485
- /**
486
- * Gets the keys from a given record object, properly typed to be an array of the key of the input object's type.
487
- *
488
- * @category Object Helpers
489
- *
490
- * @template InputRecordType - The type of the input object.
491
- *
492
- * @param record - The record to get the keys from.
493
- *
494
- * @returns An array with all the keys of the input object in string form, but properly typed as `keyof InputRecordType`.
495
- */
496
- declare function getRecordKeys<InputRecordType extends Record<PropertyKey, unknown>>(record: InputRecordType & object): Array<keyof InputRecordType>;
497
- //#endregion
498
- //#region src/root/functions/objectHelpers/omitProperties.d.ts
499
- /**
500
- * Omits properties from a given object.
501
- *
502
- * @category Object Helpers
503
- *
504
- * @template ObjectType - The type of the input object.
505
- * @template KeysToOmit - A type representing the keys to omit from the object.
506
- *
507
- * @param object - The object to omit properties from.
508
- * @param keysToOmit - The keys to omit from the object. Can either be a single string to omit one, or an array to omit multiple.
509
- *
510
- * @returns An object with a new reference in memory, with the properties omitted.
511
- */
512
- declare function omitProperties<ObjectType extends object, KeysToOmit extends keyof ObjectType>(object: ObjectType, keysToOmit: KeysToOmit | ReadonlyArray<KeysToOmit>): Omit<ObjectType, KeysToOmit>;
513
- //#endregion
514
- //#region src/root/functions/objectHelpers/removeUndefinedFromObject.d.ts
515
- type RemoveUndefined<RecordType extends Record<PropertyKey, unknown>> = { [Key in keyof RecordType]: Exclude<RecordType[Key], undefined> };
439
+ //#region src/root/types/CreateEnumType.d.ts
516
440
  /**
517
- * Removes entries whose values are `undefined` from a given object (not including null etc.).
441
+ * Get the value types from a const object so the object can behave similarly to an enum.
518
442
  *
519
- * @param object - The object to remove undefined entries from.
443
+ * @category Types
520
444
  *
521
- * @returns An object with a new reference in memory, with undefined entries removed.
445
+ * @template ObjectType - The type of the object to get the value types for.
522
446
  */
523
- declare function removeUndefinedFromObject<RecordType extends Record<PropertyKey, unknown>>(object: RecordType): RemoveUndefined<RecordType>;
447
+ type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
524
448
  //#endregion
525
- //#region src/root/functions/parsers/parseBoolean.d.ts
526
- /**
527
- * Takes a stringly-typed boolean and converts it to an actual boolean type.
528
- *
529
- * @category Parsers
530
- *
531
- * @param inputString - The string to parse.
532
- *
533
- * @throws {DataError} If the string is not either `true` or `false` (case insensitive).
534
- *
535
- * @returns The string parsed as an actual boolean.
536
- */
537
- declare function parseBoolean(inputString: string): boolean;
449
+ //#region src/root/types/SortDirection.d.ts
450
+ declare const SortDirection: {
451
+ readonly DESC: "desc";
452
+ readonly ASC: "asc";
453
+ };
454
+ type SortDirection = CreateEnumType<typeof SortDirection>;
538
455
  //#endregion
539
456
  //#region src/root/types/VersionNumber.d.ts
540
457
  /**
@@ -648,16 +565,6 @@ type ArrayElement<ArrayType extends ReadonlyArray<unknown>> = ArrayType extends
648
565
  //#region src/root/types/CallReturnType.d.ts
649
566
  type CallReturnType<Function, Arguments> = Function extends ((arg: Arguments) => infer Return) ? Return : never;
650
567
  //#endregion
651
- //#region src/root/types/CreateEnumType.d.ts
652
- /**
653
- * Get the value types from a const object so the object can behave similarly to an enum.
654
- *
655
- * @category Types
656
- *
657
- * @template ObjectType - The type of the object to get the value types for.
658
- */
659
- type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
660
- //#endregion
661
568
  //#region src/root/types/DisallowUndefined.d.ts
662
569
  /**
663
570
  * Resolves to an error message type if the type argument could potentially be undefined.
@@ -723,6 +630,133 @@ type NullableOnCondition<Condition extends boolean, ResolvedTypeIfTrue> = Condit
723
630
  */
724
631
  type OptionalOnCondition<Condition extends boolean, ResolvedTypeIfTrue> = Condition extends true ? ResolvedTypeIfTrue : ResolvedTypeIfTrue | undefined;
725
632
  //#endregion
633
+ //#region src/root/functions/miscellaneous/sortBy.d.ts
634
+ type SortFunction<ItemType> = (first: ItemType, second: ItemType) => number;
635
+ /**
636
+ * Provides a function to pass to the `sort` or `toSorted` methods on arrays, which will allow those methods to sort the items by the chosen value in the chosen direction.
637
+ *
638
+ * @template ItemType - The type of each array item.
639
+ * @template SortValue - The type of the value to sort by.
640
+ *
641
+ * @param selector - A function that takes the item and returns the value to sort by.
642
+ * @param direction - The sort direction.
643
+ *
644
+ * @returns A function to pass into the `.sort()` or `.toSorted()` methods on arrays.
645
+ */
646
+ declare function sortBy<ItemType, SortValue extends string | number | Date>(selector: (item: ItemType) => SortValue, direction?: SortDirection): SortFunction<ItemType>;
647
+ //#endregion
648
+ //#region src/root/functions/miscellaneous/stringifyDotenv.d.ts
649
+ type QuoteStyle = "double" | "single" | "none";
650
+ interface StringifyDotenvOptions {
651
+ /** The quote style to use for the values (defaults to `"double"`) */
652
+ quoteStyle: QuoteStyle;
653
+ }
654
+ /**
655
+ * Converts an object into a string in .env file format.
656
+ *
657
+ * @param contents - The object to convert. Must be a record whose values are strings.
658
+ * @param options - Extra options to apply.
659
+ *
660
+ * @returns A string representation of the object in .env file format.
661
+ */
662
+ declare function stringifyDotenv(contents: Record<PropertyKey, string> | DotenvParseOutput, options?: StringifyDotenvOptions): string;
663
+ //#endregion
664
+ //#region src/root/functions/miscellaneous/stringListToArray.d.ts
665
+ /**
666
+ * Options to apply to the conversion of a string list to an array.
667
+ *
668
+ * @category Function Options
669
+ */
670
+ interface StringListToArrayOptions {
671
+ /** What each item in the list is separated by. */
672
+ separator?: string;
673
+ /** An option to trim any extra whitespace. */
674
+ trimWhitespace?: boolean;
675
+ }
676
+ /**
677
+ * Converts a stringly-typed list to a proper array.
678
+ *
679
+ * @category Miscellaneous
680
+ *
681
+ * @param stringList - The stringly-typed list to convert.
682
+ * @param options - The options to apply to the conversion.
683
+ * @param options.separator - What each item in the list is separated by.
684
+ * @param options.trimWhitespace - An option to trim any extra whitespace.
685
+ *
686
+ * @returns A new array with each item being an item from the given list.
687
+ */
688
+ declare function stringListToArray(stringList: string, {
689
+ separator,
690
+ trimWhitespace
691
+ }?: StringListToArrayOptions): Array<string>;
692
+ //#endregion
693
+ //#region src/root/functions/miscellaneous/wait.d.ts
694
+ /**
695
+ * Waits for the given number of seconds
696
+ *
697
+ * @category Miscellaneous
698
+ *
699
+ * @param seconds - The number of seconds to wait.
700
+ *
701
+ * @returns A Promise that resolves after the given number of seconds.
702
+ */
703
+ declare function wait(seconds: number): Promise<void>;
704
+ //#endregion
705
+ //#region src/root/functions/objectHelpers/getRecordKeys.d.ts
706
+ /**
707
+ * Gets the keys from a given record object, properly typed to be an array of the key of the input object's type.
708
+ *
709
+ * @category Object Helpers
710
+ *
711
+ * @template InputRecordType - The type of the input object.
712
+ *
713
+ * @param record - The record to get the keys from.
714
+ *
715
+ * @returns An array with all the keys of the input object in string form, but properly typed as `keyof InputRecordType`.
716
+ */
717
+ declare function getRecordKeys<InputRecordType extends Record<PropertyKey, unknown>>(record: InputRecordType & object): Array<keyof InputRecordType>;
718
+ //#endregion
719
+ //#region src/root/functions/objectHelpers/omitProperties.d.ts
720
+ /**
721
+ * Omits properties from a given object.
722
+ *
723
+ * @category Object Helpers
724
+ *
725
+ * @template ObjectType - The type of the input object.
726
+ * @template KeysToOmit - A type representing the keys to omit from the object.
727
+ *
728
+ * @param object - The object to omit properties from.
729
+ * @param keysToOmit - The keys to omit from the object. Can either be a single string to omit one, or an array to omit multiple.
730
+ *
731
+ * @returns An object with a new reference in memory, with the properties omitted.
732
+ */
733
+ declare function omitProperties<ObjectType extends object, KeysToOmit extends keyof ObjectType>(object: ObjectType, keysToOmit: KeysToOmit | ReadonlyArray<KeysToOmit>): Omit<ObjectType, KeysToOmit>;
734
+ //#endregion
735
+ //#region src/root/functions/objectHelpers/removeUndefinedFromObject.d.ts
736
+ type RemoveUndefined<RecordType extends Record<PropertyKey, unknown>> = { [Key in keyof RecordType]: Exclude<RecordType[Key], undefined> };
737
+ /**
738
+ * Removes entries whose values are `undefined` from a given object (not including null etc.).
739
+ *
740
+ * @param object - The object to remove undefined entries from.
741
+ *
742
+ * @returns An object with a new reference in memory, with undefined entries removed.
743
+ */
744
+ declare function removeUndefinedFromObject<RecordType extends Record<PropertyKey, unknown>>(object: RecordType): RemoveUndefined<RecordType>;
745
+ //#endregion
746
+ //#region src/root/functions/parsers/parseBoolean.d.ts
747
+ /**
748
+ * Takes a stringly-typed boolean and converts it to an actual boolean type.
749
+ *
750
+ * @category Parsers
751
+ *
752
+ * @param inputString - The string to parse.
753
+ *
754
+ * @throws {DataError} If the string is not either `true` or `false` (case insensitive).
755
+ *
756
+ * @returns The string parsed as an actual boolean.
757
+ */
758
+ declare function parseBoolean(inputString: string): boolean;
759
+ //#endregion
726
760
  //#region src/root/functions/parsers/parseEnv.d.ts
727
761
  /**
728
762
  * Represents the three common development environments.
@@ -1423,4 +1457,4 @@ interface ZodParsingErrorData {
1423
1457
  issues: Array<z$1.core.$ZodIssue>;
1424
1458
  }
1425
1459
  //#endregion
1426
- export { APIError, type ArrayElement, type CallReturnType, type CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, type KebabToCamelOptions, type NonNull, type NonUndefined, type NormaliseIndentsFunction, type NormaliseIndentsOptions, type NormalizeIndentsFunction, type NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, type ParallelTuple, type RecordKey, type RemoveUndefined, type StringListToArrayOptions, type ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, type ZodParsingErrorData, addDaysToDate, appendSemicolon, assertNotNull, assertNotNullable, assertNotUndefined, az, calculateMonthlyDifference, camelToKebab, containsKeys, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isNonNullableObject, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, objectContainsKeys, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
1460
+ export { APIError, type ArrayElement, type CallReturnType, type CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, type KebabToCamelOptions, type NonNull, type NonUndefined, type NormaliseIndentsFunction, type NormaliseIndentsOptions, type NormalizeIndentsFunction, type NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, type ParallelTuple, type RecordKey, type RemoveUndefined, SortDirection, type StringListToArrayOptions, type ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, type ZodParsingErrorData, addDaysToDate, appendSemicolon, assertNotNull, assertNotNullable, assertNotUndefined, az, calculateMonthlyDifference, camelToKebab, containsKeys, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, identity, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isNonNullableObject, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, objectContainsKeys, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, sortBy, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
package/dist/index.js CHANGED
@@ -803,6 +803,20 @@ function createFormData(data, options = {
803
803
  return formData;
804
804
  }
805
805
  //#endregion
806
+ //#region src/root/functions/miscellaneous/identity.ts
807
+ /**
808
+ * Gives back the original input.
809
+ *
810
+ * @template InputType - The input type.
811
+ *
812
+ * @param input - The input value.
813
+ *
814
+ * @returns The original input value.
815
+ */
816
+ function identity(input) {
817
+ return input;
818
+ }
819
+ //#endregion
806
820
  //#region src/root/functions/miscellaneous/isOrdered.ts
807
821
  /**
808
822
  * Checks to see if the given array is sorted in ascending order.
@@ -933,6 +947,33 @@ I'll commit to you!
933
947
  `;
934
948
  }
935
949
  //#endregion
950
+ //#region src/root/functions/miscellaneous/sortBy.ts
951
+ /**
952
+ * Provides a function to pass to the `sort` or `toSorted` methods on arrays, which will allow those methods to sort the items by the chosen value in the chosen direction.
953
+ *
954
+ * @template ItemType - The type of each array item.
955
+ * @template SortValue - The type of the value to sort by.
956
+ *
957
+ * @param selector - A function that takes the item and returns the value to sort by.
958
+ * @param direction - The sort direction.
959
+ *
960
+ * @returns A function to pass into the `.sort()` or `.toSorted()` methods on arrays.
961
+ */
962
+ function sortBy(selector, direction = "asc") {
963
+ return (first, second) => {
964
+ const firstValue = selector(first);
965
+ const secondValue = selector(second);
966
+ if (firstValue instanceof Date && secondValue instanceof Date) {
967
+ const firstTime = firstValue.getTime();
968
+ const secondTime = secondValue.getTime();
969
+ return direction === "asc" ? firstTime - secondTime : secondTime - firstTime;
970
+ }
971
+ if (firstValue < secondValue) return direction === "asc" ? -1 : 1;
972
+ if (firstValue > secondValue) return direction === "asc" ? 1 : -1;
973
+ return 0;
974
+ };
975
+ }
976
+ //#endregion
936
977
  //#region src/root/functions/miscellaneous/stringifyDotenv.ts
937
978
  /**
938
979
  * Converts an object into a string in .env file format.
@@ -1077,6 +1118,12 @@ function parseBoolean(inputString) {
1077
1118
  return normalisedString === "true";
1078
1119
  }
1079
1120
  //#endregion
1121
+ //#region src/root/types/SortDirection.ts
1122
+ const SortDirection = {
1123
+ DESC: "desc",
1124
+ ASC: "asc"
1125
+ };
1126
+ //#endregion
1080
1127
  //#region src/root/types/VersionNumber.ts
1081
1128
  /**
1082
1129
  * Represents a software version number, considered to be made up of a major, minor, and patch part.
@@ -1936,4 +1983,4 @@ var DataError = class DataError extends Error {
1936
1983
  }
1937
1984
  };
1938
1985
  //#endregion
1939
- export { APIError, DataError, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, ONE_DAY_IN_MILLISECONDS, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, VersionType, addDaysToDate, appendSemicolon, assertNotNull, assertNotNullable, assertNotUndefined, az, calculateMonthlyDifference, camelToKebab, containsKeys, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isNonNullableObject, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, objectContainsKeys, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
1986
+ export { APIError, DataError, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, ONE_DAY_IN_MILLISECONDS, SortDirection, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, VersionType, addDaysToDate, appendSemicolon, assertNotNull, assertNotNullable, assertNotUndefined, az, calculateMonthlyDifference, camelToKebab, containsKeys, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, identity, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isNonNullableObject, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, objectContainsKeys, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, sortBy, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alextheman/utility",
3
- "version": "5.20.0",
3
+ "version": "5.21.0",
4
4
  "description": "Helpful utility functions.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -41,8 +41,8 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "@alextheman/eslint-plugin": "5.17.0",
44
- "@types/node": "25.9.1",
45
- "alex-c-line": "2.9.1",
44
+ "@types/node": "25.9.3",
45
+ "alex-c-line": "2.9.2",
46
46
  "cross-env": "10.1.0",
47
47
  "dotenv-cli": "11.0.0",
48
48
  "eslint": "10.4.1",
@@ -50,7 +50,7 @@
50
50
  "husky": "9.1.7",
51
51
  "jsdom": "29.1.1",
52
52
  "markdownlint-cli2": "0.22.1",
53
- "prettier": "3.8.3",
53
+ "prettier": "3.8.4",
54
54
  "tempy": "3.2.0",
55
55
  "tsdown": "0.22.2",
56
56
  "tsx": "4.22.4",
@@ -58,7 +58,7 @@
58
58
  "typedoc-plugin-markdown": "4.12.0",
59
59
  "typedoc-rhineai-theme": "1.2.0",
60
60
  "typescript": "6.0.3",
61
- "typescript-eslint": "8.60.1",
61
+ "typescript-eslint": "8.61.0",
62
62
  "vite": "8.0.16",
63
63
  "vitest": "4.1.8",
64
64
  "zod": "4.4.3"