fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 52 lines 1.4 kB view raw
1import type { Graph } from '../graph'; 2 3const analyzeStructure = (graph: Graph) => { 4 let maxDepth = 0; 5 let maxChildren = 0; 6 7 const computeDepth = (pointer: string, depth: number): void => { 8 maxDepth = Math.max(maxDepth, depth); 9 10 const children = Array.from(graph.nodes.entries()) 11 .filter(([, nodeInfo]) => nodeInfo.parentPointer === pointer) 12 .map(([childPointer]) => childPointer); 13 14 maxChildren = Math.max(maxChildren, children.length); 15 16 for (const childPointer of children) { 17 computeDepth(childPointer, depth + 1); 18 } 19 }; 20 21 const totalNodes = graph.nodes.size; 22 if (graph.nodes.has('#')) { 23 computeDepth('#', 1); 24 } 25 26 return { maxChildren, maxDepth, totalNodes }; 27}; 28 29const exportForVisualization = (graph: Graph) => { 30 const childrenMap = new Map<string, string[]>(); 31 32 for (const [pointer, nodeInfo] of graph.nodes) { 33 if (!nodeInfo.parentPointer) continue; 34 if (!childrenMap.has(nodeInfo.parentPointer)) { 35 childrenMap.set(nodeInfo.parentPointer, []); 36 } 37 childrenMap.get(nodeInfo.parentPointer)!.push(pointer); 38 } 39 40 const nodes = Array.from(graph.nodes.keys()).map((pointer) => ({ 41 children: childrenMap.get(pointer)?.length ?? 0, 42 childrenPointers: childrenMap.get(pointer) || [], 43 pointer, 44 })); 45 46 return nodes; 47}; 48 49export const graph = { 50 analyzeStructure, 51 exportForVisualization, 52} as const;