domain/converters/check.converter.ts

domain/converters/check.converter.ts is a file in Bane's Lab Site. 68 lines of code and 10 definitions.

import {
    CHECK_AUTHORITY_LABEL,
    CHECK_BY_LABEL,
    CHECK_DEPENDS_ON_LABEL,
    CHECK_EVIDENCE_LABEL,
    CHECK_FRESHNESS_LABEL,
    CHECK_HEADING,
    CHECK_OBSERVATION_LABEL,
    CHECK_POPULATION_LABEL,
    CHECK_REFUSAL_LABEL,
    CHECK_SHAPE_LABEL,
    DECLARED_ABSENCE_MARK,
    NOT_ANSWERED,
    checkAnswerOf,
    evidenceAnswerOf,
} from "#configuration/strings/reference.strings";
import type { CheckRow, CheckView, RecordCheckView } from "#types/reference.types";
import { LIST_SEPARATOR, missingCheck } from "#configuration/strings/ontology.strings";
import type { RecordCheckEntryView, ResolutionView } from "#types/ontology.types";
import { glossary, linkOf, row, text } from "#domain/converters/ontology.converter";
import type { Block } from "#types/block.types";
import type { EdgeRef } from "#types/link.types";

const listRow = function listRow(label: string, edges: readonly EdgeRef[]): CheckRow {
    return { edges: edges.length === 0 ? null : edges, label, text: NOT_ANSWERED };
};

const answerRow = function answerRow(label: string, answer: string | null): CheckRow {
    return { edges: null, label, text: checkAnswerOf(answer) };
};

export const checkRowsOf = function checkRowsOf(check: RecordCheckView): readonly CheckRow[] {
    const { answers } = check;
    return [
        listRow(CHECK_BY_LABEL, check.by),
        answerRow(CHECK_POPULATION_LABEL, answers.population),
        answerRow(CHECK_FRESHNESS_LABEL, answers.freshness),
        answerRow(CHECK_REFUSAL_LABEL, answers.refusal),
        answerRow(CHECK_OBSERVATION_LABEL, answers.observation),
        { edges: null, label: CHECK_EVIDENCE_LABEL, text: evidenceAnswerOf(answers.evidence) },
        answerRow(CHECK_AUTHORITY_LABEL, answers.authority),
        listRow(CHECK_DEPENDS_ON_LABEL, check.dependsOn),
        listRow(CHECK_SHAPE_LABEL, check.shape),
    ];
};

export const canonOf = function canonOf(canon: readonly string[]): string {
    return canon
        .map((entry) => (entry.startsWith(DECLARED_ABSENCE_MARK) ? checkAnswerOf(entry) : entry))
        .join(LIST_SEPARATOR);
};

export const recordCheckOf = function recordCheckOf(resolution: ResolutionView, ref: string): RecordCheckView | null {
    const entry: RecordCheckEntryView | undefined = resolution.byRecord[ref];
    const answers: CheckView | undefined = entry === undefined ? undefined : resolution.answers[entry.answers];
    return entry === undefined || answers === undefined
        ? null
        : { answers, by: entry.by, dependsOn: entry.dependsOn, shape: entry.shape };
};

export const checkBlocks = function checkBlocks(resolution: ResolutionView, ref: string): readonly Block[] {
    const check = recordCheckOf(resolution, ref);
    if (check === null) {
        throw new Error(missingCheck(ref));
    }
    return [
        text(CHECK_HEADING),
        glossary(
            checkRowsOf(check).map((line) =>
                row(line.label, line.edges === null ? line.text : line.edges.map(linkOf).join(LIST_SEPARATOR)),
            ),
        ),
    ];
};