# domain/converters/anatomy.reference.converter.ts

> 160 lines of code and 21 definitions.

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

Listed in [domain/converters](https://banes-lab.com/api/source/tree/domain/converters.md), after [domain/converters/anatomy.fragment.converter.ts](https://banes-lab.com/source/tree/domain/converters/anatomy.fragment.converter.ts.md) and before [domain/converters/catalog.converter.ts](https://banes-lab.com/source/tree/domain/converters/catalog.converter.ts.md).

## Definitions

- `relation` (lexical_declaration, line 49)
- `edge` (lexical_declaration, line 44)
- `counted` (lexical_declaration, line 58)
- `countsOf` (lexical_declaration, line 62)
- `folderRecord` (lexical_declaration, line 140, exported)
- `fileRecord` (lexical_declaration, line 122, exported)
- `definitionRecord` (lexical_declaration, line 105, exported)
- `folderEdge` (lexical_declaration, line 53)
- `anatomyRecordsOf` (lexical_declaration, line 96, exported)
- `definitionEdge` (lexical_declaration, line 101, exported)
- `Place` (interface_declaration, line 38)
- `suffix` (lexical_declaration, line 45)
- `local` (lexical_declaration, line 54)
- `filesOf` (lexical_declaration, line 73)
- `foldersOf` (lexical_declaration, line 77)
- `anatomyIndexOf` (lexical_declaration, line 81, exported)
- `nodes` (lexical_declaration, line 83, exported)
- `sources` (lexical_declaration, line 87, exported)
- `files` (lexical_declaration, line 100, exported)
- `line` (lexical_declaration, line 102, exported)
- `place` (lexical_declaration, line 106, 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, DefinitionRef, DefinitionView } from "#types/anatomy.types";
import type { AnatomyIndex, AnatomyRecords, SourceEntry } from "#types/record.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 type { ReferenceRecord, ReferenceRelation } from "#types/reference.types";
import { fileId, folderId } from "#domain/converters/source.converter";
import { localPath, parentPath, treeOf } from "#core/converters/folder.converter";
import { ANATOMY_PAGE } from "#core/ids/page.ids";
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 { nounFor } from "#core/converters/text.converter";
import { tabLink } from "#core/assets/link.assets";

interface Place {
    readonly id: string;
    readonly line: number | null;
    readonly path: string;
}

const edge = function edge(label: string, place: Place): EdgeRef {
    const suffix = place.line === null ? "" : LINE_INFIX + String(place.line);
    return { label, ref: CHAPTER_FACE + FACE_SEPARATOR + tabLink(ANATOMY_PAGE, treeOf(place.path), place.id + suffix) };
};

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, { id: folderId(path), line: null, path });
};

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 filesOf = function filesOf(folder: AnatomyFolder): readonly AnatomyFile[] {
    return [...folder.files, ...folder.folders.flatMap(filesOf)];
};

const foldersOf = function foldersOf(folder: AnatomyFolder): readonly AnatomyFolder[] {
    return [folder, ...folder.folders.flatMap(foldersOf)];
};

export const anatomyIndexOf = function anatomyIndexOf(roots: readonly AnatomyFolder[]): AnatomyIndex {
    const files = roots.flatMap(filesOf);
    const nodes = [
        ...roots.flatMap(foldersOf).map((folder) => [folderId(folder.path), folder.records] as const),
        ...files.map((file) => [fileId(file.path), file.records] as const),
    ];
    const sources = files.flatMap((file): (readonly [string, SourceEntry])[] =>
        file.source === null ? [] : [[file.path, { references: file.references ?? null, source: file.source }]],
    );
    return {
        nodes: Object.fromEntries(nodes.flatMap(([id, records]) => (records === undefined ? [] : [[id, records]]))),
        sources: Object.fromEntries(sources),
    };
};

export const anatomyRecordsOf = function anatomyRecordsOf(roots: readonly AnatomyFolder[]): {
    readonly file: (file: AnatomyFile) => AnatomyRecords;
    readonly folder: (folder: AnatomyFolder) => AnatomyRecords;
} {
    const files = new Map(roots.flatMap(filesOf).map((file) => [file.path, file]));
    const definitionEdge = function definitionEdge(ref: DefinitionRef): EdgeRef {
        const line = files.get(ref.file)?.definitions.find((candidate) => candidate.name === ref.name)?.line ?? null;
        return edge(ref.name, { id: fileId(ref.file), line, path: ref.file });
    };
    const definitionRecord = function definitionRecord(file: AnatomyFile, definition: DefinitionView): ReferenceRecord {
        const place = { id: fileId(file.path), line: definition.line, path: file.path };
        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), place),
                ]),
                ...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, { id: fileId(file.path), line: definition.line, path: file.path }),
                    ),
                ),
            ],
            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, { id: folderId(child.path), line: null, path: child.path }),
                    ),
                ),
                ...relation(
                    FILES_RELATION,
                    folder.files.map((file) => edge(file.name, { id: fileId(file.path), line: null, path: file.path })),
                ),
            ],
            summary: counted(folder.stats.files, FILE_NOUN, FILES_NOUN) + COUNT_SEPARATOR + countsOf(folder),
        };
    };
    return {
        file: (file) => ({
            definitions: Object.fromEntries(
                file.definitions.map((definition) => [String(definition.line), definitionRecord(file, definition)]),
            ),
            node: fileRecord(file),
        }),
        folder: (folder) => ({ definitions: {}, node: folderRecord(folder) }),
    };
};
```
