21 lines
766 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { EntryOut } from '../../types/api';
export type EntryNode = EntryOut & { key: string; children?: EntryNode[] };
/** Дерево строк ОСВ по parent_entry_id; порядок — как пришёл (по row_number). */
export function buildEntryTree(entries: EntryOut[]): EntryNode[] {
const nodes = new Map<string, EntryNode>();
for (const entry of entries) nodes.set(entry.id, { ...entry, key: entry.id });
const roots: EntryNode[] = [];
for (const entry of entries) {
const node = nodes.get(entry.id) as EntryNode;
const parent = entry.parent_entry_id ? nodes.get(entry.parent_entry_id) : undefined;
if (parent) {
parent.children = [...(parent.children ?? []), node];
} else {
roots.push(node);
}
}
return roots;
}