Files
webstudio/apps/builder/app/shared/array-utils.ts
T
Bogdan Chadkin 59b9c6fedc Support cross-project instance copy paste (#1326)
Here a little refactored copy paste instance plugin.

Now we copy all data including slots and tokens though paste write them
as is or do not write at all if already exist.

This let us to not do unnecessary patches for copy paste within project
while still have whole UI pasted in different project.

There is still a problem with pasting styles which always have different
breakpoint ids in each project. We will need to implement merging in
other PR.
2023-03-31 11:38:19 +07:00

41 lines
871 B
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: Value[] = [];
for (const key of keys) {
const value = map.get(key);
if (value !== undefined) {
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;
};