domain/converters/anatomy.converter.ts

domain/converters/anatomy.converter.ts is a file in Bane's Lab Site. 199 lines of code and 25 definitions.

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,
    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, LAYER_LABEL, LIST_SEPARATOR, NONE_LABEL } from "#configuration/strings/ontology.strings";
import type { Section, Subsection, Tab } from "#types/document.types";
import { chips, glossary, labeled, 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 { holdsFiles, isPagedFile } from "#core/predicates/folder.predicate";
import type { Block } from "#types/block.types";
import { STATS_SEPARATOR } from "#configuration/constants/anatomy.constants";
import { anchorMarkup } from "#core/formatters/link.formatter";
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 refList = function refList(refs: readonly DefinitionRef[]): string {
    const links = refs.map((ref) => anchorMarkup(nodeHref(ref.file, null), ref.name));
    return refs.length === 0 ? NONE_LABEL : links.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 : labeled(SUBJECT_LABEL, file.slots.subject),
        file.slots === null ? null : labeled(CONCERN_LABEL, file.slots.concern),
        file.slots === null ? null : labeled(VARIANT_LABEL, file.slots.variant),
        labeled(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[] {
    const references = file.references === undefined ? {} : { references: file.references };
    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,
                      ...references,
                      source: file.source,
                      title: DOCUMENT_TITLE,
                  },
              ]),
        ...(file.source === null
            ? []
            : [
                  {
                      kind: "source" as const,
                      language: languageOf(file.name),
                      path: file.path,
                      ...references,
                      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)];
};

const repositoryEntries = function repositoryEntries(folder: AnatomyFolder): (readonly [string, string])[] {
    return [
        ...(folder.repository === undefined ? [] : [[folderId(folder.path), folder.repository] as const]),
        ...folder.files.flatMap((file) =>
            file.repository === undefined ? [] : [[fileId(file.path), file.repository] as const],
        ),
        ...folder.folders.flatMap(repositoryEntries),
    ];
};

const folderEntries = function folderEntries(folder: AnatomyFolder): (readonly [string, readonly string[]])[] {
    return [
        [folderId(folder.path), folder.folders.map((child) => folderId(child.path))],
        ...folder.folders.flatMap(folderEntries),
    ];
};

const pagedNodes = function pagedNodes(folder: AnatomyFolder): string[] {
    return [
        folderId(folder.path),
        ...folder.files.filter(isPagedFile).map((file) => fileId(file.path)),
        ...folder.folders.filter(holdsFiles).flatMap(pagedNodes),
    ];
};

export const treeTab = function treeTab(tree: AnatomyTree): Tab {
    const repository = repositoryEntries(tree.snapshot.tree);
    return {
        folders: Object.fromEntries(folderEntries(tree.snapshot.tree)),
        icon: ANATOMY_CONTAINER_ICON,
        id: tree.tab,
        label: treeLabelOf(tree.tab),
        layout: "tree",
        pages: pagedNodes(tree.snapshot.tree),
        ...(repository.length === 0 ? {} : { repository: Object.fromEntries(repository) }),
        sections: folderSections(tree.snapshot.tree),
    };
};