Skip to content

Commit cff38de

Browse files
authored
Merge pull request #13 from chandru415/4.9
[chandu] selectMatchingObjectsByKeys
2 parents c24abb4 + cd83a2b commit cff38de

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

packages/utils/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ npm i @slck/utils --save
3838
- isDateLike
3939
- hasDuplicateByKeys
4040
- generateMultiplesInRange
41+
- selectMatchingObjectsByKeys
4142

4243
# Types
4344

packages/utils/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@slck/utils",
3-
"version": "4.8.0",
3+
"version": "4.9.0",
44
"type": "commonjs",
55
"license": "MIT",
66
"publishConfig": {
@@ -46,6 +46,7 @@
4646
"isAnyRecordWithEmptyValues",
4747
"isDateLike",
4848
"hasDuplicateByKeys",
49-
"generateMultiplesInRange"
49+
"generateMultiplesInRange",
50+
"selectMatchingObjectsByKeys"
5051
]
5152
}

packages/utils/src/lib/utils.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)