Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EAKRDNzXtRrwmWz9z6PZu3
21 lines
766 B
TypeScript
21 lines
766 B
TypeScript
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;
|
||
}
|