mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-03 10:37:48 +09:00
50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
export const setDifference = <Item>(current: Set<Item>, other: Set<Item>) => {
|
|
const result = new Set<Item>(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 = <Item>(current: Set<Item>, other: Set<Item>) => {
|
|
const result = new Set<Item>(current);
|
|
for (const item of other) {
|
|
result.add(item);
|
|
}
|
|
return result;
|
|
};
|
|
|
|
export const mapGroupBy = <Item, Key>(
|
|
array: Item[] | Iterable<Item>,
|
|
getKey: (item: Item) => Key
|
|
) => {
|
|
const groups = new Map<Key, Item[]>();
|
|
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 = <Item, Key>(
|
|
array: Item[] | Iterable<Item>,
|
|
getKey: (item: Item) => Key
|
|
) => {
|
|
return Object.fromEntries(mapGroupBy(array, getKey));
|
|
};
|