Skip to content

intersectionBy

Edit

Returns the first value for each derived key shared by all arrays.

intersectionBy.ts
export type IntersectionBySelector<Source, Comparison, Key> = (
this: void,
value: Source | Comparison,
index: number,
array: readonly (Source | Comparison | undefined)[],
) => Key;
/** Returns the first distinct value for each derived key shared by all arrays. */
export const intersectionBy = <const Source, Key, const Comparison = Source>(
array: readonly Source[],
others: ReadonlyArray<readonly Comparison[]>,
getKey: IntersectionBySelector<Source, Comparison, Key>,
): Source[] => {
const sets: Array<Set<Key>> = [];
for (let otherIndex = 0; otherIndex < others.length; otherIndex += 1) {
if (!Object.hasOwn(others, otherIndex)) continue;
const other = others[otherIndex];
if (!Array.isArray(other)) {
throw new TypeError("others must contain only arrays");
}
const keys = new Set<Key>();
for (let index = 0; index < other.length; index += 1) {
if (!Object.hasOwn(other, index)) continue;
keys.add(getKey(other[index] as Comparison, index, other));
}
sets.push(keys);
}
const seen = new Set<Key>();
const result: Source[] = [];
for (let index = 0; index < array.length; index += 1) {
if (!Object.hasOwn(array, index)) continue;
const value = array[index] as Source;
const key = getKey(value, index, array);
if (seen.has(key) || !sets.every((set) => set.has(key))) continue;
seen.add(key);
result.push(value);
}
return result;
};
Terminal
wget -O src/lib/intersectionBy.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/intersectionBy.ts
intersectionBy.example.ts
import { intersectionBy } from "./intersectionBy";
const shared = intersectionBy(
[{ id: 1 }, { id: 2 }],
[[{ id: 2 }]],
(item) => item.id
);
  • array (readonly Source[]) — The ordered source array. Empty slots are skipped.
  • others (readonly (readonly Comparison[])[]) — Arrays that must contain the key. Empty slots in the list or its arrays are skipped.
  • getKey ((value: Source | Comparison, index, array: readonly (Source | Comparison | undefined)[]) => Key) — Produces comparison keys and is invoked without a receiver. value is always present; the current source or comparison array retains skipped holes, so reading one can produce undefined.

Returns Source[]; comparison values do not widen the result type.