domain/converters/ontology.graph.converter.ts
domain/converters/ontology.graph.converter.ts is a file in Bane's Lab Site. 40 lines of code and 15 definitions.
import { SLUG_CHARS, UNDERSCORE } from "#configuration/constants/document.constants";
import type { EdgeRef } from "#types/link.types";
import { FACE_SEPARATOR } from "@govlab/constants";
import { LINE_BREAK } from "#configuration/constants/code.constants";
const QUOTE = '"';
const APOSTROPHE = "'";
const NODE_OPEN = '["';
const NODE_CLOSE = '"]';
const INDENT = " ";
const NODE_PREFIX = "n_";
export const nodeId = function nodeId(id: string): string {
let out = NODE_PREFIX;
for (const char of id.toLowerCase()) {
out += SLUG_CHARS.includes(char) ? char : UNDERSCORE;
}
return out;
};
export const nodeLabel = function nodeLabel(label: string): string {
return label.replaceAll(QUOTE, APOSTROPHE);
};
export const node = function node(id: string, label: string): string {
return INDENT + nodeId(id) + NODE_OPEN + nodeLabel(label) + NODE_CLOSE;
};
export const diagram = function diagram(header: string, lines: readonly string[]): string {
return [header, ...lines].join(LINE_BREAK);
};
export const edgeLine = function edgeLine(from: string, arrow: string, to: string): string {
return INDENT + nodeId(from) + arrow + nodeId(to);
};
export const localId = function localId(edge: EdgeRef, face: string, members: ReadonlySet<string>): string | null {
if (edge.ref === null) {
return null;
}
const prefix = face + FACE_SEPARATOR;
if (!edge.ref.startsWith(prefix)) {
return null;
}
const id = edge.ref.slice(prefix.length);
return members.has(id) ? id : null;
};