MCPcopy Create free account
hub / github.com/APIParkLab/APIPark / flattenTree

Function flattenTree

frontend/packages/common/src/utils/postcat.tsx:27–55  ·  view source on GitHub ↗
(
  tree: T[] = [],
  childrenKey: keyof T = 'children',
  pathKey: keyof T = 'id'
)

Source from the content-addressed store, hash-verified

25 * each enhanced with a path property representing its location within the tree.
26 */
27export 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
57export function byteToString(inputByteLength: number): string {
58 inputByteLength = inputByteLength || 0

Callers 3

ApiPreviewFunction · 0.90
ValueEnumFunction · 0.90
MessageDataGridFunction · 0.90

Calls 1

flattenFunction · 0.85

Tested by

no test coverage detected