Skip to content

Commit

Permalink
feat(intersects): let identity return any value
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jun 24, 2024
1 parent bac48d1 commit ce474e8
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,17 @@ export const flat = <T>(lists: readonly T[][]): T[] => {
* Given two arrays, returns true if any
* elements intersect
*/
export const intersects = <T, K extends string | number | symbol>(
export const intersects = <T, K>(
listA: readonly T[],
listB: readonly T[],
identity?: (t: T) => K
): boolean => {
if (!listA || !listB) return false
const ident = identity ?? ((x: T) => x as unknown as K)
const dictB = listB.reduce((acc, item) => {
acc[ident(item)] = true
return acc
}, {} as Record<string | number | symbol, boolean>)
return listA.some(value => dictB[ident(value)])
if (identity) {
const known = new Set(listA.map(identity))
return listB.some(item => known.has(identity(item)))
}
return listB.some(item => listA.includes(item))
}

/**
Expand Down

0 comments on commit ce474e8

Please sign in to comment.