(raw: unknown, prefix: string, depth = 0, maxDepth = 6)
| 41 | }; |
| 42 | |
| 43 | const normalizeNode = (raw: unknown, prefix: string, depth = 0, maxDepth = 6): MindMapTreeNode => { |
| 44 | if (!raw || typeof raw !== 'object') { |
| 45 | return { id: prefix, label: 'Topic', summary: '', children: [] }; |
| 46 | } |
| 47 | |
| 48 | const rawNode = raw as Record<string, unknown>; |
| 49 | const label = coerceText(rawNode.label ?? rawNode.title ?? rawNode.name, 'Topic'); |
| 50 | const summary = coerceText(rawNode.summary ?? rawNode.note ?? rawNode.description, ''); |
| 51 | const childrenInput = Array.isArray(rawNode.children) ? rawNode.children : []; |
| 52 | const children = |
| 53 | depth >= maxDepth |
| 54 | ? [] |
| 55 | : childrenInput |
| 56 | .slice(0, 8) |
| 57 | .map((child, index) => normalizeNode(child, `${prefix}_${index + 1}`, depth + 1, maxDepth)); |
| 58 | |
| 59 | return { |
| 60 | id: coerceText(rawNode.id, prefix), |
| 61 | label, |
| 62 | summary, |
| 63 | children, |
| 64 | }; |
| 65 | }; |
| 66 | |
| 67 | export const normalizeMindMapTree = (raw: unknown, maxDepth = 6): MindMapTreeNode => { |
| 68 | const root = normalizeNode(raw, 'root', 0, maxDepth); |
no test coverage detected