( tree: T[] = [], childrenKey: keyof T = 'children', pathKey: keyof T = 'id' )
| 25 | * each enhanced with a path property representing its location within the tree. |
| 26 | */ |
| 27 | export function flattenTree<T extends TreeNode<T>>( |
| 28 | tree: T[] = [], |
| 29 | childrenKey: keyof T = 'children', |
| 30 | pathKey: keyof T = 'id' |
| 31 | ): T[] { |
| 32 | const result: T[] = [] |
| 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 |
| 55 | } |
| 56 | |
| 57 | export function byteToString(inputByteLength: number): string { |
| 58 | inputByteLength = inputByteLength || 0 |
no test coverage detected