domain/converters/anatomy.reference.converter.ts
domain/converters/anatomy.reference.converter.ts is a file in Bane's Lab Site. 160 lines of code and 21 definitions.
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,
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 { LOCATION_RELATION } from "#configuration/strings/walk.strings";
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) }),
};
};