@@ -802,7 +802,6 @@ export const hasDuplicateByKeys = (arr: any[], ...keys: string[]): boolean => {
802802 return false ; // No duplicates
803803} ;
804804
805-
806805/**
807806 * Returns an array of numbers within a specified range [start, end]
808807 * that are exact multiples of a given number.
@@ -829,3 +828,41 @@ export const generateMultiplesInRange = (
829828
830829 return result ; // Return the array of multiples
831830} ;
831+
832+ /**
833+ * Filters objects from a source array based on matching key values found in another array.
834+ *
835+ * @template TSource - Type of the source array objects.
836+ * @template TMatch - Type of the matching array objects.
837+ * @template TSourceKey - Key in the source objects to match.
838+ * @template TMatchKey - Key in the matching objects to compare.
839+ * @template KeyType - The shared type of values in both keys (enforced for type safety).
840+ *
841+ * @param sourceArray - The array of objects to filter (e.g., full list of users).
842+ * @param selectionArray - The array of objects whose key values determine what to keep.
843+ * @param sourceKey - The key in `sourceArray` used for matching.
844+ * @param matchKey - The key in `selectionArray` used for matching.
845+ * @returns A filtered array of source objects whose key values are found in the selection array.
846+ */
847+ export function selectMatchingObjectsByKeys <
848+ TSource ,
849+ TMatch ,
850+ TSourceKey extends keyof TSource ,
851+ TMatchKey extends keyof TMatch ,
852+ KeyType extends TSource [ TSourceKey ] & TMatch [ TMatchKey ]
853+ > (
854+ sourceArray : TSource [ ] ,
855+ selectionArray : TMatch [ ] ,
856+ sourceKey : TSourceKey ,
857+ matchKey : TMatchKey
858+ ) : TSource [ ] {
859+ // Build a Set of values from selectionArray to use for fast lookups
860+ const selectedValues = new Set < KeyType > (
861+ selectionArray . map ( ( item ) => item [ matchKey ] as KeyType )
862+ ) ;
863+
864+ // Filter sourceArray to only include items with matching key values
865+ return sourceArray . filter ( ( item ) =>
866+ selectedValues . has ( item [ sourceKey ] as KeyType )
867+ ) ;
868+ }
0 commit comments