@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
33 lines (32 loc) • 1.09 kB
JavaScript
export function intersection(a, b, ...rest) {
if (arguments.length < 2)
return a ? a.slice() : [];
if (rest.length)
return intersection(a, intersection(b, ...rest));
return a.filter(Set.prototype.has, new Set(b));
}
/** Create an array with unique values from each of the passed arrays */
export function union(...rest) {
const set = new Set();
rest.forEach((item) => item.forEach((i) => set.add(i)));
return [...set];
}
export function complement(a, b, ...rest) {
if (!b)
return a ? a.slice() : [];
if (rest.length > 1)
return complement(a, complement(b, ...rest));
const setB = new Set(b);
return a.filter((element) => !setB.has(element));
}
/**
* @returns if the passed arrays have a full intersection
* Expect uniq values in collections
*/
export function fullIntersection(a, b) {
if (a.length === 0 && b.length === 0)
return true;
const [larger, smaller] = a.length >= b.length ? [a, b] : [b, a];
const set = new Set(larger);
return !smaller.filter((item) => !set.has(item)).length;
}