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

Function flatten

frontend/packages/common/src/utils/postcat.tsx:35–51  ·  view source on GitHub ↗
(node: T, path: string[], __levelIndex__: number, parent: T | null = null)

Source from the content-addressed store, hash-verified

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

Callers 1

flattenTreeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected