| 33 | let __globalIndex__ = 0 |
| 34 | |
| 35 | const flatten = (node: T, path: string[], __levelIndex__: number, parent: T | null = null): void => { |
| 36 | const { [childrenKey]: children, ...restNode } = node |
| 37 | const nodeWithPath: T = { |
| 38 | ...restNode, |
| 39 | __raw__: node, |
| 40 | path: [...path, node[pathKey]] as string[], |
| 41 | __globalIndex__, |
| 42 | __levelIndex__ |
| 43 | } as T |
| 44 | nodeWithPath[childrenKey] = children |
| 45 | nodeWithPath.parent = parent ?? null |
| 46 | result.push(nodeWithPath) |
| 47 | __globalIndex__++ |
| 48 | |
| 49 | const list: T[] = (children || []) as unknown as T[] |
| 50 | list.forEach((child: T, childIndex: number) => flatten(child, nodeWithPath.path || [], childIndex, node)) |
| 51 | } |
| 52 | |
| 53 | tree.forEach((node, index) => flatten(node, [], index)) |
| 54 | return result |