MCPcopy Create free account
hub / github.com/code-pushup/cli / formatAsciiTree

Function formatAsciiTree

packages/utils/src/lib/text-formats/ascii/tree.ts:12–36  ·  view source on GitHub ↗
(tree: Tree)

Source from the content-addressed store, hash-verified

10const COL_GAP = 2;
11
12export function formatAsciiTree(tree: Tree): string {
13 const nodes = flatten(tree.root);
14 const maxWidth = Math.max(
15 ...nodes.map(({ node, level }) => level * INDENT_CHARS + node.name.length),
16 );
17 const keysMaxWidths =
18 tree.type === 'coverage'
19 ? {}
20 : nodes.reduce<Record<string, number>>(
21 (acc, { node }) => ({
22 ...acc,
23 ...Object.fromEntries(
24 Object.entries(node.values ?? {}).map(([key, value]) => [
25 key,
26 Math.max(acc[key] ?? 0, `${value}`.length),
27 ]),
28 ),
29 }),
30 {},
31 );
32
33 return formatTreeNode(tree.root, '', maxWidth, keysMaxWidths)
34 .map(line => `${line}\n`)
35 .join('');
36}
37
38function formatTreeNode(
39 node: TreeNode,

Callers 2

tree.unit.test.tsFile · 0.85
treeSectionFunction · 0.85

Calls 2

flattenFunction · 0.85
formatTreeNodeFunction · 0.85

Tested by

no test coverage detected