mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-06 20:13:35 +09:00
Here moved our utility polyfill for Map.groupBy into shims module we are using for all stuff we will replace with native methods. Used it in marketplace to group by category.
41 lines
878 B
TypeScript
41 lines
878 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: 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;
|
|
};
|