feat: group all-account rows by instrument and day (WealthLedger/main#47) (#126)
This commit was merged in pull request #126.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
type GroupedItem<T>
|
||||
= | { item: T; type: "single" }
|
||||
| { items: [T, T, ...T[]]; key: string; type: "group" };
|
||||
|
||||
function groupItems<T>(
|
||||
items: readonly T[],
|
||||
keyOf: (item: T) => null | string,
|
||||
): GroupedItem<T>[] {
|
||||
const result: GroupedItem<T>[] = [];
|
||||
const indices = new Map<string, number>();
|
||||
|
||||
for (const item of items) {
|
||||
const key = keyOf(item);
|
||||
|
||||
if (key === null) {
|
||||
result.push({ item, type: "single" });
|
||||
continue;
|
||||
}
|
||||
|
||||
const index = indices.get(key);
|
||||
|
||||
if (index === undefined) {
|
||||
indices.set(key, result.length);
|
||||
result.push({ item, type: "single" });
|
||||
continue;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line security/detect-object-injection -- Map-owned index.
|
||||
const existing = result[index];
|
||||
|
||||
if (existing?.type === "single") {
|
||||
// eslint-disable-next-line security/detect-object-injection -- Map-owned index.
|
||||
result[index] = {
|
||||
items: [existing.item, item],
|
||||
key,
|
||||
type: "group",
|
||||
};
|
||||
}
|
||||
else if (existing) {
|
||||
existing.items.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export { groupItems };
|
||||
Reference in New Issue
Block a user