# domain/converters/graph.converter.ts

> 47 lines of code and 12 definitions.

Tree: Site tree
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/tree#file-domain-converters-graph-converter-ts
Source text: https://banes-lab.com/assets/sources/source.7f00f02e180bd55f34d7b38cbc026f1713354f0fdb66527af35e6462209d89f2.generated.txt

## Definitions

- `folderLabel` (lexical_declaration, line 30)
- `structureDiagram` (lexical_declaration, line 47, exported)
- `structureLines` (lexical_declaration, line 36)
- `importsDiagram` (lexical_declaration, line 15, exported)
- `IMPORT_ARROW` (lexical_declaration, line 12)
- `IMPORT_ARROW_END` (lexical_declaration, line 13)
- `containers` (lexical_declaration, line 16, exported)
- `nodes` (lexical_declaration, line 19, exported)
- `edges` (lexical_declaration, line 20, exported)
- `layer` (lexical_declaration, line 31)
- `{ files }` (lexical_declaration, line 32)
- `children` (lexical_declaration, line 37)

## Uses

- [core/converters/text.converter.ts](https://banes-lab.com/source/tree/core/converters/text.converter.ts.md)
- [domain/converters/ontology.converter.ts](https://banes-lab.com/source/tree/domain/converters/ontology.converter.ts.md)

## Used by

- [domain/converters/anatomy.fragment.converter.ts](https://banes-lab.com/source/tree/domain/converters/anatomy.fragment.converter.ts.md)

## Source

```typescript
import type { AnatomyFolder, AnatomySnapshot } from "#types/anatomy.types";
import { FILES_NOUN, FILE_NOUN, IMPORTS_NOUN, IMPORT_NOUN, ROOT_LABEL } from "#configuration/strings/folder.strings";
import {
    IMPORT_DIAGRAM_HEADER,
    STATS_SEPARATOR,
    STRUCTURE_ARROW,
    STRUCTURE_DIAGRAM_HEADER,
} from "#configuration/constants/anatomy.constants";
import { diagram, edgeLine, node } from "#domain/converters/ontology.converter";
import { nounFor } from "#core/converters/text.converter";

const IMPORT_ARROW = " -- ";
const IMPORT_ARROW_END = " --> ";

export const importsDiagram = function importsDiagram(snapshot: AnatomySnapshot): string {
    const containers = [...new Set(snapshot.imports.flatMap((edge) => [edge.from, edge.to]))].sort((a, b) =>
        a.localeCompare(b),
    );
    const nodes = containers.map((name) => node(name, name));
    const edges = snapshot.imports.map((edge) =>
        edgeLine(
            edge.from,
            `${IMPORT_ARROW}${String(edge.count)} ${nounFor(edge.count, IMPORT_NOUN, IMPORTS_NOUN)}${IMPORT_ARROW_END}`,
            edge.to,
        ),
    );
    return diagram(IMPORT_DIAGRAM_HEADER, [...nodes, ...edges]);
};

const folderLabel = function folderLabel(folder: AnatomyFolder): string {
    const layer = folder.layer === null ? "" : STATS_SEPARATOR + folder.layer;
    const { files } = folder.stats;
    return `${folder.name}${STATS_SEPARATOR}${String(files)} ${nounFor(files, FILE_NOUN, FILES_NOUN)}${layer}`;
};

const structureLines = function structureLines(folder: AnatomyFolder): string[] {
    const children = folder.folders.filter((child) => child.stats.files > 0);
    return [
        ...children.map((child) => node(child.path, folderLabel(child))),
        ...children.map((child) =>
            edgeLine(folder.path === "" ? ROOT_LABEL : folder.path, STRUCTURE_ARROW, child.path),
        ),
        ...children.flatMap(structureLines),
    ];
};

export const structureDiagram = function structureDiagram(snapshot: AnatomySnapshot): string {
    return diagram(STRUCTURE_DIAGRAM_HEADER, [
        node(ROOT_LABEL, folderLabel({ ...snapshot.tree, name: ROOT_LABEL })),
        ...structureLines(snapshot.tree),
    ]);
};
```
