domain/converters/coverage.converter.ts

domain/converters/coverage.converter.ts is a file in Bane's Lab Site. 62 lines of code and 7 definitions.

import {
    ALGORITHMS_LABEL,
    DECLARED_ABSENT_LABEL,
    FIELD_SEPARATOR,
    GRAMMAR_LABEL,
    LABEL_SEPARATOR,
    LEXICON_LABEL,
    LIST_SEPARATOR,
    OF_LABEL,
    PRINCIPLES_LABEL,
    REASONING_LABEL,
    unlabeledCoverage,
} from "#configuration/strings/ontology.strings";
import { ALGO_FACE, ARCH_FACE, LEX_FACE, PAG_FACE, REASON_FACE } from "@govlab/constants";
import {
    CHECK_AUTHORITY_LABEL,
    CHECK_BY_LABEL,
    CHECK_DEPENDS_ON_LABEL,
    CHECK_EVIDENCE_LABEL,
    CHECK_FRESHNESS_LABEL,
    CHECK_OBSERVATION_LABEL,
    CHECK_POPULATION_LABEL,
    CHECK_REFUSAL_LABEL,
    CHECK_SHAPE_LABEL,
} from "#configuration/strings/reference.strings";
import type { CoverageView } from "#types/coverage.types";
import type { GlossaryEntry } from "#types/block.types";
import { row } from "#domain/converters/ontology.converter";

const COLLECTION_LABELS: ReadonlyMap<string, string> = new Map([
    [ARCH_FACE, PRINCIPLES_LABEL],
    [LEX_FACE, LEXICON_LABEL],
    [ALGO_FACE, ALGORITHMS_LABEL],
    [REASON_FACE, REASONING_LABEL],
    [PAG_FACE, GRAMMAR_LABEL],
]);

const QUESTION_LABELS: ReadonlyMap<string, string> = new Map([
    ["by", CHECK_BY_LABEL],
    ["population", CHECK_POPULATION_LABEL],
    ["freshness", CHECK_FRESHNESS_LABEL],
    ["refusal", CHECK_REFUSAL_LABEL],
    ["observation", CHECK_OBSERVATION_LABEL],
    ["evidence", CHECK_EVIDENCE_LABEL],
    ["authority", CHECK_AUTHORITY_LABEL],
    ["shape", CHECK_SHAPE_LABEL],
    ["dependsOn", CHECK_DEPENDS_ON_LABEL],
]);

const labelOf = function labelOf(labels: ReadonlyMap<string, string>, id: string): string {
    const label = labels.get(id);
    if (label === undefined) {
        throw new Error(unlabeledCoverage(id));
    }
    return label;
};

export const coverageRow = function coverageRow(entry: CoverageView): GlossaryEntry {
    const parts = entry.questions.map((question) => {
        const counted = `${labelOf(QUESTION_LABELS, question.question).toLowerCase()}${LABEL_SEPARATOR}${String(question.answered)} ${OF_LABEL} ${String(entry.records)}`;
        return question.declaredAbsent === 0
            ? counted
            : `${counted}${LIST_SEPARATOR}${String(question.declaredAbsent)} ${DECLARED_ABSENT_LABEL}`;
    });
    return row(labelOf(COLLECTION_LABELS, entry.collection), parts.join(FIELD_SEPARATOR));
};