Files
webstudio/apps/builder/app/shared/array-utils.ts
T
Bogdan Chadkin 3cfa10ae22 refactor: compute props contextually (#2628)
Ref https://github.com/webstudio-is/webstudio/issues/2532

Here refactored props values computing to reuse in both canvas and
settings panel and support future collections which need to provide
different values for same variables.

Grouped whole logic and covered with tests.

## Testing

- check image src is working
- check link switch page in preview
2023-11-18 20:54:51 +03:00

59 lines
1.3 KiB
TypeScript

type Predicate<Item> = (value: Item) => boolean;
export const removeByMutable = <Item>(
array: Item[],
predicate: Predicate<Item>
) => {
// reversed order to splice without breaking index
for (let index = array.length - 1; index >= 0; index -= 1) {
if (predicate(array[index])) {
array.splice(index, 1);
}
}
};
export const getMapValuesByKeysSet = <Key, Value>(
map: Map<Key, Value>,
keys: Set<Key>
) => {
const values: NonNullable<Value>[] = [];
for (const key of keys) {
const value = map.get(key);
if (value != null) {
values.push(value);
}
}
return values;
};
export const getMapValuesBy = <Key, Value>(
map: Map<Key, Value>,
predicate: Predicate<Value>
) => {
const values: Value[] = [];
for (const value of map.values()) {
if (predicate(value)) {
values.push(value);
}
}
return values;
};
// @todo replace with builtin Map.groupBy when support
export const groupBy = <Item, Key>(
array: Item[] | IterableIterator<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;
};