UNPKG

rc-js-util

Version:

A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.

33 lines (29 loc) 742 B
import { arrayIndex } from "./array-index.js"; import { fpIdentity } from "../../fp/impl/fp-identity.js"; /** * @public * Given two `ArrayLike`, provides the items common between them in an `Array`. * * @remarks * See {@link arrayIntersect}. */ export function arrayIntersect<TItem> ( a: ArrayLike<TItem>, b: ArrayLike<TItem>, getComparisonValue: (item: TItem, index: number) => unknown = fpIdentity, ) : TItem[] { const result: TItem[] = []; const bIndex = arrayIndex(b, getComparisonValue); for (let i = 0, iEnd = a.length; i < iEnd; ++i) { const item = a[i]; if (bIndex.has(getComparisonValue(item, i))) { result.push(item); } } return result; }