Files
webstudio/apps/builder/app/shared/shim.ts
T
Bogdan Chadkin 8632212278 refactor: support presets in style object model fixtures (#3725)
Refactored all left style object mode tests with new fixtures.
2024-07-15 13:33:34 +02:00

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));
};