/** * Implementation of some functions of Set Theory * See: https://en.wikipedia.org/wiki/Set_theory */ // /** // * Calculate the complement set. // * See Complement (set theory) https://en.wikipedia.org/wiki/Complement_(set_theory) // * @param {any[]} universe The universe of available values // * @param {any[]} set A subset from the universe. // * @returns {any[]} The complementary set of A given the universe // */ export function complement(universe: any[], set: any[]): any[] { return universe.filter(el => !set.find(_el => el === _el)); } // /** // * Calculate intersection between two sets. // * See https://en.wikipedia.org/wiki/Intersection_(set_theory) // * @param {any[]} setA The set A // * @param {any[]} setB The set B // * @returns {any[]} The intersection between A and B // */ export function intersection(setA: any[], setB: any[]): any[] { return setA.filter( aElement => setB.find(bElement => bElement === aElement) !== undefined ); } // /** // * Test whether one set is a subset from another // * @param subSet The set that must be a subset to return true // * @param superSet The set that must contain the subset to return true // * @returns {boolean} True if all elements of subset are in superset, false otherwise // */ export function testSubset(subSet: any[], superSet: any[]): boolean { return subSet.reduce( (acc: boolean, el: any) => acc && superSet.indexOf(el) !== -1, true ); }