Skip to content

intersection

Edit

Returns distinct values shared by every provided array.

intersection.ts
/** Returns distinct values present in every provided array. */
export const intersection = <const T>(
array: readonly T[],
...others: ReadonlyArray<readonly unknown[]>
): T[] => {
const sets = others.map((other) => {
if (!Array.isArray(other)) {
throw new TypeError("comparison values must be arrays");
}
const set = new Set<unknown>();
for (let index = 0; index < other.length; index += 1) {
if (Object.hasOwn(other, index)) set.add(other[index]);
}
return set;
});
const seen = new Set<T>();
const result: T[] = [];
for (let index = 0; index < array.length; index += 1) {
if (!Object.hasOwn(array, index)) continue;
const value = array[index] as T;
if (seen.has(value) || !sets.every((set) => set.has(value))) continue;
seen.add(value);
result.push(value);
}
return result;
};
Terminal
wget -O src/lib/intersection.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/intersection.ts
intersection.example.ts
import { intersection } from "./intersection";
const shared = intersection(["read", "write"], ["write", "delete"]);
// ["write"]
  • array (readonly T[]) — The ordered source array. Empty slots are skipped.
  • ...others (readonly (readonly unknown[])[]) — Arrays that must contain each result. Empty slots are skipped, not treated as undefined, and their element types do not widen the result.
  • Returns distinct values in first-seen order.