blob: de9ef968327f125e3148f9726f7f0239ebf4643b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/**
* Split an array into two groups based on the result {@linkcode predicate}
*
* Items for which {@linkcode predicate} returns `true` will be in the "left"
* result, and the others in the "right" one
*/
export function partition<T>(
input: Array<T>,
predicate: (element: T) => boolean,
): [Array<T>, Array<T>] {
const left: Array<T> = [];
const right: Array<T> = [];
for (const element of input) {
if (predicate(element)) {
left.push(element);
} else {
right.push(element);
}
}
return [left, right];
}
/**
* Deduplicate the elements of {@linkcode items} by their `id` property
*/
export function uniqueById<T extends { id: string }>(items: T[]): T[] {
const entries = items.map((item) => [item.id, item] as const);
const mapById = new Map<string, T>(entries);
return Array.from(mapById.values());
}
|