UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

28 lines (26 loc) 712 B
import { dfdlT } from "@monstermann/dfdl"; import { cloneSet } from "@monstermann/remmi"; //#region src/set/addOr.ts /** * `addOr(target, value, or)` * * Adds a `value` to the `target` set and returns a new set. If the value already exists in the set, returns the `or` value instead. * * ```ts * addOr(new Set([1, 2]), 3, null); // Set([1, 2, 3]) * addOr(new Set([1, 2]), 2, null); // null * ``` * * ```ts * pipe(new Set([1, 2]), addOr(3, null)); // Set([1, 2, 3]) * pipe(new Set([1, 2]), addOr(2, null)); // null * ``` */ const addOr = dfdlT((target, value, or) => { if (target.has(value)) return or; const result = cloneSet(target); result.add(value); return result; }, 3); //#endregion export { addOr };