# domain/loaders/anatomy.loader.ts

> 194 lines of code and 29 definitions.

Tree: Site tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/tree#file-domain-loaders-anatomy-loader-ts
Source text: https://banes-lab.com/assets/sources/source.ee372174a1bc88b0b1cf2f506e4d674c6410670497e4f5f8986bb3e5e0f38350.generated.txt

## Definitions

- `relation` (lexical_declaration, line 122)
- `edge` (lexical_declaration, line 107)
- `counted` (lexical_declaration, line 131)
- `countsOf` (lexical_declaration, line 135)
- `folderRecord` (lexical_declaration, line 180)
- `fileRecord` (lexical_declaration, line 163)
- `definitionRecord` (lexical_declaration, line 146)
- `collect` (lexical_declaration, line 76)
- `nodeRef` (lexical_declaration, line 102)
- `folderEdge` (lexical_declaration, line 126)
- `treeOfNode` (lexical_declaration, line 97)
- `lineOf` (lexical_declaration, line 111)
- `definitionEdge` (lexical_declaration, line 118)
- `anatomyReference` (lexical_declaration, line 200, exported)
- `resolveTarget` (lexical_declaration, line 54, exported)
- `citedSource` (lexical_declaration, line 62, exported)
- `index` (lexical_declaration, line 86)
- `Node` (type_alias_declaration, line 49)
- `nodes` (lexical_declaration, line 52)
- `cited` (lexical_declaration, line 58, exported)
- `location` (lexical_declaration, line 65, exported)
- `source` (lexical_declaration, line 66, exported)
- `found` (lexical_declaration, line 88)
- `suffix` (lexical_declaration, line 103)
- `local` (lexical_declaration, line 127)
- `requested` (lexical_declaration, line 201, exported)
- `target` (lexical_declaration, line 202, exported)
- `node` (lexical_declaration, line 203, exported)
- `definition` (lexical_declaration, line 210, exported)

## Uses

- [core/assets/link.assets.ts](https://banes-lab.com/source/tree/core/assets/link.assets.ts.md)
- [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/source.converter.ts](https://banes-lab.com/source/tree/domain/converters/source.converter.ts.md)

## Source

```typescript
import type {
    AnatomyFile,
    AnatomyFolder,
    DefinitionLocation,
    DefinitionRef,
    DefinitionView,
} from "#types/anatomy.types";
import {
    CALLED_BY_RELATION,
    CALLS_RELATION,
    COUNT_END,
    COUNT_LAST_SEPARATOR,
    COUNT_SEPARATOR,
    DEFINITIONS_NOUN,
    DEFINITIONS_RELATION,
    DEFINITION_KIND,
    DEFINITION_NOUN,
    EDGES_NOUN,
    EDGE_NOUN,
    FILES_NOUN,
    FILES_RELATION,
    FILE_KIND,
    FILE_NOUN,
    FOLDERS_RELATION,
    FOLDER_RELATION,
    LINES_NOUN,
    LINE_MARK,
    LINE_NOUN,
    LOCATION_RELATION,
    ROOT_LABEL,
} from "#configuration/strings/folder.strings";
import { DEFINITION_ANCHOR, TREE_TAB } from "#core/ids/anatomy.ids";
import type { DefinitionBlock, SourceBlock } from "#types/block.types";
import type { ReferenceRecord, ReferenceRelation } from "#types/reference.types";
import { fileId, folderId, languageOf } from "#domain/converters/source.converter";
import { localPath, parentPath, targetOf, treeOf } from "#core/converters/folder.converter";
import { ANATOMY_PAGE } from "#core/ids/page.ids";
import { ANATOMY_TREES } from "#core/registries/anatomy.registry";
import { CHAPTER_FACE } from "#configuration/constants/vocabulary.constants";
import type { EdgeRef } from "#types/link.types";
import { FACE_SEPARATOR } from "@govlab/constants";
import { LINE_INFIX } from "#configuration/constants/anatomy.constants";
import type { NodeTarget } from "#types/folder.types";
import { citedDefinition } from "#core/loaders/definition.loader";
import { nounFor } from "#core/converters/text.converter";
import { sourceNameOf } from "#core/loaders/walk.loader";
import { tabLink } from "#assets/link.assets";

type Node =
    { readonly file: AnatomyFile; readonly kind: "file" } | { readonly folder: AnatomyFolder; readonly kind: "folder" };

let nodes: ReadonlyMap<string, Node> | null = null;

export const resolveTarget = function resolveTarget(target: NodeTarget): NodeTarget {
    if (!target.id.startsWith(DEFINITION_ANCHOR)) {
        return target;
    }
    const cited = citedDefinition(target.id.slice(DEFINITION_ANCHOR.length), target.text);
    return cited === null ? target : { id: fileId(cited.file), line: cited.line, text: null };
};

export const citedSource = function citedSource(
    block: DefinitionBlock,
): { readonly block: SourceBlock; readonly location: DefinitionLocation } | null {
    const location = citedDefinition(block.name, block.file);
    const source = location === null ? null : sourceNameOf(location.file);
    if (location === null || source === null) {
        return null;
    }
    return {
        block: { kind: "source", language: languageOf(location.file), path: location.file, source, title: block.title },
        location,
    };
};

const collect = function collect(folder: AnatomyFolder, into: Map<string, Node>): void {
    into.set(folderId(folder.path), { folder, kind: "folder" });
    for (const file of folder.files) {
        into.set(fileId(file.path), { file, kind: "file" });
    }
    for (const child of folder.folders) {
        collect(child, into);
    }
};

const index = function index(): ReadonlyMap<string, Node> {
    if (nodes === null) {
        const found = new Map<string, Node>();
        for (const tree of ANATOMY_TREES) {
            collect(tree.snapshot.tree, found);
        }
        nodes = found;
    }
    return nodes;
};

const treeOfNode = function treeOfNode(id: string): string {
    const node = index().get(id);
    return node === undefined ? TREE_TAB : treeOf(node.kind === "file" ? node.file.path : node.folder.path);
};

const nodeRef = function nodeRef(id: string, line: number | null): string {
    const suffix = line === null ? "" : LINE_INFIX + String(line);
    return CHAPTER_FACE + FACE_SEPARATOR + tabLink(ANATOMY_PAGE, treeOfNode(id), id + suffix);
};

const edge = function edge(label: string, id: string, line: number | null): EdgeRef {
    return { label, ref: nodeRef(id, line) };
};

const lineOf = function lineOf(ref: DefinitionRef): number | null {
    const node = index().get(fileId(ref.file));
    const definition =
        node?.kind === "file" ? node.file.definitions.find((candidate) => candidate.name === ref.name) : undefined;
    return definition === undefined ? null : definition.line;
};

const definitionEdge = function definitionEdge(ref: DefinitionRef): EdgeRef {
    return edge(ref.name, fileId(ref.file), lineOf(ref));
};

const relation = function relation(name: string, edges: readonly EdgeRef[]): ReferenceRelation[] {
    return edges.length === 0 ? [] : [{ edges, relation: name }];
};

const folderEdge = function folderEdge(path: string): EdgeRef {
    const local = localPath(path);
    return edge(local === "" ? ROOT_LABEL : local, folderId(path), null);
};

const counted = function counted(value: number, one: string, many: string): string {
    return `${String(value)} ${nounFor(value, one, many)}`;
};

const countsOf = function countsOf(node: AnatomyFile | AnatomyFolder): string {
    return (
        counted(node.stats.lines.code, LINE_NOUN, LINES_NOUN) +
        COUNT_SEPARATOR +
        counted(node.stats.definitions, DEFINITION_NOUN, DEFINITIONS_NOUN) +
        COUNT_LAST_SEPARATOR +
        counted(node.stats.edges, EDGE_NOUN, EDGES_NOUN) +
        COUNT_END
    );
};

const definitionRecord = function definitionRecord(file: AnatomyFile, definition: DefinitionView): ReferenceRecord {
    return {
        code: definition.flow,
        kind: definition.kind,
        layer: null,
        name: definition.name,
        relations: [
            ...relation(LOCATION_RELATION, [
                edge(localPath(file.path) + LINE_MARK + String(definition.line), fileId(file.path), definition.line),
            ]),
            ...relation(CALLS_RELATION, definition.callees.map(definitionEdge)),
            ...relation(CALLED_BY_RELATION, definition.callers.map(definitionEdge)),
        ],
        summary: DEFINITION_KIND,
    };
};

const fileRecord = function fileRecord(file: AnatomyFile): ReferenceRecord {
    return {
        code: file.layer,
        kind: file.slots === null ? FILE_KIND : file.slots.concern,
        layer: null,
        name: file.name,
        relations: [
            ...relation(FOLDER_RELATION, [folderEdge(parentPath(file.path))]),
            ...relation(
                DEFINITIONS_RELATION,
                file.definitions.map((definition) => edge(definition.name, fileId(file.path), definition.line)),
            ),
        ],
        summary: countsOf(file),
    };
};

const folderRecord = function folderRecord(folder: AnatomyFolder): ReferenceRecord {
    return {
        code: folder.layer,
        kind: folder.role,
        layer: null,
        name: localPath(folder.path) === "" ? ROOT_LABEL : folder.name,
        relations: [
            ...relation(
                FOLDERS_RELATION,
                folder.folders.map((child) => edge(child.name, folderId(child.path), null)),
            ),
            ...relation(
                FILES_RELATION,
                folder.files.map((file) => edge(file.name, fileId(file.path), null)),
            ),
        ],
        summary: counted(folder.stats.files, FILE_NOUN, FILES_NOUN) + COUNT_SEPARATOR + countsOf(folder),
    };
};

export const anatomyReference = function anatomyReference(fragment: string): ReferenceRecord | null {
    const requested = targetOf(`#${fragment}`);
    const target = requested === null ? null : resolveTarget(requested);
    const node = target === null ? undefined : index().get(target.id);
    if (node === undefined || target === null) {
        return null;
    }
    if (node.kind === "folder") {
        return folderRecord(node.folder);
    }
    const definition =
        target.line === null ? undefined : node.file.definitions.find((candidate) => candidate.line === target.line);
    return definition === undefined ? fileRecord(node.file) : definitionRecord(node.file, definition);
};
```
