UNPKG

predictype

Version:

PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.

39 lines (38 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setIntersection = setIntersection; const sets_js_1 = require("../../enums/sets.js"); /** * Checks intersection properties between two sets using the specified operation. * * @param source The first set. * @param oper The intersection operation to perform (e.g. 'disjoint', 'intersects'). * @param target The second set. * @returns True if the intersection check is valid according to the operator, otherwise false. * * @throws {Error} If the operation is not recognized. * * @example * const a = new Set([1, 2]); * const b = new Set([2, 3]); * const c = new Set([4, 5]); * * setIntersection(a, 'intersects', b); // true * setIntersection(a, 'disjoint', c); // true * * @remarks * Supported Operators: * - **DISJOINT**: Sets have no elements in common * - **INTERSECTS**: Sets have at least one element in common */ function setIntersection(source, oper, target) { const operators = { [sets_js_1.SetIntersectionEnum.DISJOINT]: (a, b) => ![...a].some((v) => b.has(v)), [sets_js_1.SetIntersectionEnum.INTERSECTS]: (a, b) => [...a].some((v) => b.has(v)), }; const enumOper = typeof oper === 'string' ? oper : oper; const fn = operators[enumOper]; if (!fn) throw new Error(`Unknown SetIntersection operation: ${oper}`); return fn(source, target); }