UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

32 lines (30 loc) 889 B
import { dfdlT } from "@monstermann/dfdl"; //#region src/set/union.ts /** * `union(target, source)` * * Returns a new set containing all unique values from both the `target` set and the `source` set. * * ```ts * union(new Set([1, 2]), new Set([2, 3, 4])); // Set([1, 2, 3, 4]) * union(new Set([1, 2]), new Set([3, 4])); // Set([1, 2, 3, 4]) * ``` * * ```ts * pipe(new Set([1, 2]), union(new Set([2, 3, 4]))); // Set([1, 2, 3, 4]) * pipe(new Set([1, 2]), union(new Set([3, 4]))); // Set([1, 2, 3, 4]) * ``` */ const union = dfdlT((target, source) => { if (source.size === 0) return target; if (target.size === 0) return source; for (const element of source) if (!target.has(element)) { const result = /* @__PURE__ */ new Set(); for (const a of target) result.add(a); for (const b of source) result.add(b); return result; } return target; }, 2); //#endregion export { union };