domain/converters/lexicon.converter.ts

domain/converters/lexicon.converter.ts is a file in Bane's Lab Site. 78 lines of code and 3 definitions.

import {
    AFTER_TITLE,
    ALIASES_LABEL,
    BEFORE_TITLE,
    CATEGORY_LABEL,
    CONTRACT_LABEL,
    DISTINCT_FROM_LABEL,
    EXAMPLE_LABEL,
    KIND_LABEL,
    LABEL_SEPARATOR,
    LAYER_LABEL,
    PRINCIPLE_LABEL,
    REFERENCED_BY_LABEL,
} from "#configuration/strings/ontology.strings";
import { DEFINITION_LABEL, LEXICON_INTRO } from "#configuration/strings/lexicon.strings";
import { FACE_SEPARATOR, LEX_FACE } from "@govlab/constants";
import { LEX_ANCHOR, LEX_CATEGORY_ANCHOR } from "#core/ids/ontology.ids";
import type { ResolutionView, TermCategoryView, TermView } from "#types/ontology.types";
import type { Section, Subsection } from "#types/document.types";
import {
    chips,
    exemplarBlocks,
    glossary,
    linkOf,
    linkRow,
    linked,
    plainList,
    row,
    wideRow,
} from "#domain/converters/ontology.converter";
import { ONTOLOGY_LEXICON_ICON } from "#configuration/icons/ontology.icons";
import { checkBlocks } from "#domain/converters/check.converter";
import { groupTitleOf } from "#configuration/strings/catalog.strings";

export const termSubsection = function termSubsection(term: TermView, resolution: ResolutionView): Subsection {
    return {
        blocks: [
            chips([
                linked(KIND_LABEL, term.kind),
                linked(CATEGORY_LABEL, term.category),
                linked(LAYER_LABEL, term.layer),
            ]),
            glossary([
                wideRow(DEFINITION_LABEL, term.definition),
                ...(term.example === null ? [] : [row(EXAMPLE_LABEL, term.example)]),
                ...(term.aliases.length === 0 ? [] : [row(ALIASES_LABEL, plainList(term.aliases))]),
                ...linkRow(PRINCIPLE_LABEL, term.principle === null ? [] : [term.principle]),
                ...linkRow(CONTRACT_LABEL, term.contract === null ? [] : [term.contract]),
                ...linkRow(REFERENCED_BY_LABEL, term.referencedBy),
                ...term.distinctFrom.map((entry) =>
                    row(DISTINCT_FROM_LABEL, linkOf(entry.record) + LABEL_SEPARATOR + entry.reason),
                ),
            ]),
            ...exemplarBlocks(term.exemplar, BEFORE_TITLE, AFTER_TITLE),
            ...checkBlocks(resolution, LEX_FACE + FACE_SEPARATOR + term.id),
        ],
        id: LEX_ANCHOR + term.id,
        title: term.name,
    };
};

const categorySection = function categorySection(group: TermCategoryView, resolution: ResolutionView): Section {
    return {
        icon: ONTOLOGY_LEXICON_ICON,
        id: LEX_CATEGORY_ANCHOR + group.id,
        intro: LEXICON_INTRO,
        subsections: [...group.terms]
            .sort((a, b) => a.name.localeCompare(b.name))
            .map((term) => termSubsection(term, resolution)),
        title: groupTitleOf(group.id, group.category),
    };
};

export const lexiconSections = function lexiconSections(
    groups: readonly TermCategoryView[],
    resolution: ResolutionView,
): readonly Section[] {
    return [...groups]
        .sort((a, b) => a.category.localeCompare(b.category))
        .map((group) => categorySection(group, resolution));
};