Given two arrays, returns true if any elements intersect.
intersects([1, 2, 3], [4, 5, 6]);// false intersects([1, 0, 0], [0, 1], (n) => n > 1);// true
npx atmx add helper intersects
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
/*** Given two arrays, returns true if any elements intersect.** @example* intersects([1, 2, 3], [4, 5, 6])* // false** intersects([1, 0, 0], [0, 1], (n) => n > 1)* // true*/export function intersects<T, K>(listA: readonly T[],listB: readonly T[],identity?: (t: T) => K,): boolean {if (!listA || !listB) {return false;}if (identity) {const known = new Set(listA.map(identity));return listB.some((item) => known.has(identity(item)));}return listB.some((item) => listA.includes(item));}