# domain/converters/anatomy.converter.ts

> 164 lines of code and 20 definitions.

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

## Definitions

- `fileBlocks` (lexical_declaration, line 100)
- `folderSection` (lexical_declaration, line 150)
- `fileChips` (lexical_declaration, line 79)
- `folderSections` (lexical_declaration, line 166)
- `distributionBlocks` (lexical_declaration, line 91)
- `folderIntro` (lexical_declaration, line 133)
- `fileRecord` (lexical_declaration, line 125)
- `treeTab` (lexical_declaration, line 170, exported)
- `refLink` (lexical_declaration, line 53)
- `count` (lexical_declaration, line 129)
- `BREAK` (lexical_declaration, line 51)
- `refList` (lexical_declaration, line 57)
- `mark` (lexical_declaration, line 61)
- `definitionRow` (lexical_declaration, line 65)
- `head` (lexical_declaration, line 66)
- `description` (lexical_declaration, line 71)
- `kinds` (lexical_declaration, line 92)
- `rare` (lexical_declaration, line 93)
- `layer` (lexical_declaration, line 134)
- `counts` (lexical_declaration, line 138)

## Uses

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

## Source

```typescript
import { ANATOMY_CONTAINER_ICON, FOLDER_SECTION_ICON } from "#configuration/icons/anatomy.icons";
import type { AnatomyFile, AnatomyFolder, AnatomyTree, DefinitionRef, DefinitionView } from "#types/anatomy.types";
import {
    CALLED_BY_LABEL,
    CALLS_LABEL,
    CONCERN_LABEL,
    COUNT_END,
    COUNT_LAST_SEPARATOR,
    COUNT_SEPARATOR,
    DEFINITIONS_NOUN,
    DEFINITION_NOUN,
    DOCUMENT_TITLE,
    EDGES_NOUN,
    EDGE_NOUN,
    FILES_NOUN,
    FILE_NOUN,
    FILE_WALK_CAPTION,
    FOLDER_INTRO_HOLDING,
    FOLDER_INTRO_LAYER_TAIL,
    FOLDER_INTRO_LEAD,
    FOLDER_INTRO_ON_LAYER,
    FOLDER_WALK_CAPTION,
    GENERATED_LABEL,
    INHERITED_LABEL,
    IN_LABEL,
    LAYER_LABEL,
    LINES_NOUN,
    LINE_MARK,
    LINE_NOUN,
    OUT_LABEL,
    RARE_SYNTAX_LABEL,
    ROOT_LABEL,
    SOURCE_TITLE,
    STATS_LABEL,
    SUBJECT_LABEL,
    SYNTAX_LABEL,
    UNPARSED_LABEL,
    VARIANT_LABEL,
} from "#configuration/strings/folder.strings";
import { LABEL_SEPARATOR, LIST_SEPARATOR, NONE_LABEL } from "#configuration/strings/ontology.strings";
import type { Section, Subsection, Tab } from "#types/document.types";
import { chips, glossary, labelled, row } from "#domain/converters/ontology.converter";
import { documentBlocks, documentChips } from "#domain/converters/document.converter";
import { fileId, folderId, languageOf, nodeHref, treeLabelOf } from "#domain/converters/source.converter";
import type { Block } from "#types/block.types";
import { STATS_SEPARATOR } from "#configuration/constants/anatomy.constants";
import { localPath } from "#core/converters/folder.converter";
import { nounFor } from "#core/converters/text.converter";
import { statsMetrics } from "#domain/converters/anatomy.fragment.converter";

const BREAK = "<br>";

const refLink = function refLink(ref: DefinitionRef): string {
    return `<a href="${nodeHref(ref.file, null)}">${ref.name}</a>`;
};

const refList = function refList(refs: readonly DefinitionRef[]): string {
    return refs.length === 0 ? NONE_LABEL : refs.map(refLink).join(LIST_SEPARATOR);
};

const mark = function mark(label: string, value: string): string {
    return `<code>${label} ${value}</code>`;
};

const definitionRow = function definitionRow(definition: DefinitionView): ReturnType<typeof row> {
    const head = [
        `<strong>${definition.flow}</strong>`,
        mark(IN_LABEL, String(definition.inDegree)),
        mark(OUT_LABEL, String(definition.outDegree)),
    ].join(STATS_SEPARATOR);
    const description = [
        head,
        CALLS_LABEL + LABEL_SEPARATOR + refList(definition.callees),
        CALLED_BY_LABEL + LABEL_SEPARATOR + refList(definition.callers),
    ].join(BREAK);
    return row(definition.name + LINE_MARK + String(definition.line), description);
};

const fileChips = function fileChips(file: AnatomyFile): Block {
    return chips([
        file.slots === null ? UNPARSED_LABEL : labelled(SUBJECT_LABEL, file.slots.subject),
        file.slots === null ? null : labelled(CONCERN_LABEL, file.slots.concern),
        file.slots === null ? null : labelled(VARIANT_LABEL, file.slots.variant),
        labelled(LAYER_LABEL, file.layer),
        file.generated ? GENERATED_LABEL : null,
        file.inherited ? INHERITED_LABEL : null,
        ...documentChips(file.document),
    ]);
};

const distributionBlocks = function distributionBlocks(file: AnatomyFile): Block[] {
    const kinds = file.distribution.invariants.map((stat) => ({ label: stat.type, value: String(stat.count) }));
    const rare = file.distribution.variants.map((stat) => ({ label: stat.type, value: String(stat.count) }));
    return [
        ...(kinds.length === 0 ? [] : [{ caption: SYNTAX_LABEL, kind: "metric" as const, metrics: kinds }]),
        ...(rare.length === 0 ? [] : [{ caption: RARE_SYNTAX_LABEL, kind: "metric" as const, metrics: rare }]),
    ];
};

const fileBlocks = function fileBlocks(file: AnatomyFile): Block[] {
    return [
        fileChips(file),
        { caption: STATS_LABEL, kind: "metric" as const, metrics: statsMetrics(file.stats) },
        ...(file.definitions.length === 0 ? [] : [glossary(file.definitions.map(definitionRow))]),
        ...(file.document === null ? [] : documentBlocks(file.document)),
        ...(file.walk === null ? [] : [{ caption: FILE_WALK_CAPTION, kind: "walk" as const, walk: file.walk }]),
        ...distributionBlocks(file),
        ...(file.source === null || file.document === null
            ? []
            : [{ kind: "markdown" as const, path: file.path, source: file.source, title: DOCUMENT_TITLE }]),
        ...(file.source === null
            ? []
            : [
                  {
                      kind: "source" as const,
                      language: languageOf(file.name),
                      path: file.path,
                      source: file.source,
                      title: SOURCE_TITLE,
                  },
              ]),
    ];
};

const fileRecord = function fileRecord(file: AnatomyFile): Subsection {
    return { blocks: fileBlocks(file), id: fileId(file.path), title: file.name };
};

const count = function count(value: number, one: string, many: string): string {
    return `<strong>${String(value)}</strong> ${nounFor(value, one, many)}`;
};

const folderIntro = function folderIntro(folder: AnatomyFolder): string {
    const layer =
        folder.layer === null
            ? ""
            : `${FOLDER_INTRO_ON_LAYER}<strong>${folder.layer}</strong>${FOLDER_INTRO_LAYER_TAIL}`;
    const counts =
        [
            count(folder.stats.files, FILE_NOUN, FILES_NOUN),
            count(folder.stats.lines.code, LINE_NOUN, LINES_NOUN),
            count(folder.stats.definitions, DEFINITION_NOUN, DEFINITIONS_NOUN),
        ].join(COUNT_SEPARATOR) +
        COUNT_LAST_SEPARATOR +
        count(folder.stats.edges, EDGE_NOUN, EDGES_NOUN) +
        COUNT_END;
    return `${FOLDER_INTRO_LEAD}<strong>${folder.role}</strong>${layer}${FOLDER_INTRO_HOLDING}${counts}`;
};

const folderSection = function folderSection(folder: AnatomyFolder): Section {
    return {
        blocks: [
            { caption: STATS_LABEL, kind: "metric" as const, metrics: statsMetrics(folder.stats) },
            ...(folder.walk === null
                ? []
                : [{ caption: FOLDER_WALK_CAPTION, kind: "walk" as const, walk: folder.walk }]),
        ],
        icon: FOLDER_SECTION_ICON,
        id: folderId(folder.path),
        intro: folderIntro(folder),
        subsections: folder.files.map(fileRecord),
        title: localPath(folder.path) === "" ? ROOT_LABEL : localPath(folder.path),
    };
};

const folderSections = function folderSections(folder: AnatomyFolder): Section[] {
    return [folderSection(folder), ...folder.folders.flatMap(folderSections)];
};

export const treeTab = function treeTab(tree: AnatomyTree): Tab {
    return {
        icon: ANATOMY_CONTAINER_ICON,
        id: tree.tab,
        label: treeLabelOf(tree.tab),
        layout: "tree",
        sections: folderSections(tree.snapshot.tree),
    };
};
```
