UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

34 lines (32 loc) 941 B
import { dfdlT } from "@monstermann/dfdl"; //#region src/set/intersection.ts /** * `intersection(target, source)` * * Returns a new set containing only the values that exist in both the `target` set and the `source` set. * * ```ts * intersection(new Set([1, 2, 3]), new Set([2, 3, 4])); // Set([2, 3]) * intersection(new Set([1, 2]), new Set([3, 4])); // Set([]) * ``` * * ```ts * pipe(new Set([1, 2, 3]), intersection(new Set([2, 3, 4]))); // Set([2, 3]) * pipe(new Set([1, 2]), intersection(new Set([3, 4]))); // Set([]) * ``` */ const intersection = dfdlT((target, source) => { if (target.size === 0) return target; if (source.size === 0) return source; for (const value of target) if (!source.has(value)) { const result = /* @__PURE__ */ new Set(); for (const value$1 of target) { if (!source.has(value$1)) continue; result.add(value$1); } return result; } return target; }, 2); //#endregion export { intersection };