export const setDifference = (current: Set, other: Set) => { const result = new Set(current); if (current.size <= other.size) { for (const item of current) { if (other.has(item)) { result.delete(item); } } } else { for (const item of other) { if (current.has(item)) { result.delete(item); } } } return result; }; export const setUnion = (current: Set, other: Set) => { const result = new Set(current); for (const item of other) { result.add(item); } return result; }; export const mapGroupBy = ( array: Item[] | Iterable, getKey: (item: Item) => Key ) => { const groups = new Map(); for (const item of array) { const key = getKey(item); let group = groups.get(key); if (group === undefined) { group = []; groups.set(key, group); } group.push(item); } return groups; }; export const objectGroupBy = ( array: Item[] | Iterable, getKey: (item: Item) => Key ) => { return Object.fromEntries(mapGroupBy(array, getKey)); };