# shared/registries/taxonomy.registry.ts

> 84 lines of code and 17 definitions.

Tree: Governance tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/governance#file-governance-shared-registries-taxonomy-registry-ts
Source text: https://banes-lab.com/source/governance/shared/registries/taxonomy.registry.ts.txt

Listed in [shared/registries](https://banes-lab.com/api/source/governance/shared/registries.md), after [shared/registries/location.registry.ts](https://banes-lab.com/source/governance/shared/registries/location.registry.ts.md).

## Definitions

- `assertHost` (lexical_declaration, line 48)
- `buildJurisdiction` (lexical_declaration, line 72)
- `importEntries` (lexical_declaration, line 25)
- `RootEntry` (interface_declaration, line 6)
- `hostEntries` (lexical_declaration, line 14)
- `declaredSpecial` (lexical_declaration, line 15)
- `vocabulary` (lexical_declaration, line 26)
- `foreign` (lexical_declaration, line 27)
- `mirrorEntries` (lexical_declaration, line 37)
- `byRoot` (lexical_declaration, line 41)
- `entry` (lexical_declaration, line 43)
- `host` (lexical_declaration, line 73)
- `declared` (lexical_declaration, line 74)
- `sources` (lexical_declaration, line 76)
- `entries` (lexical_declaration, line 78)
- `containers` (lexical_declaration, line 79)
- `JURISDICTION` (lexical_declaration, line 91, exported)

## Uses

- [shared/factories/vocabulary.factory.ts](https://banes-lab.com/source/governance/shared/factories/vocabulary.factory.ts.md)
- [shared/validators/taxonomy.validator.ts](https://banes-lab.com/source/governance/shared/validators/taxonomy.validator.ts.md)

## Source

```typescript
import type { Jurisdiction, TaxonomySource, Vocabulary } from "../../types/taxonomy.types.ts";
import { assertMirrors, assertTaxonomy } from "../validators/taxonomy.validator.ts";
import { createVocabulary, importedRootsOf } from "../factories/vocabulary.factory.ts";
import { imports, taxonomy } from "../configs/taxonomy.config.ts";

interface RootEntry {
    readonly containers: ReadonlySet<string>;
    readonly foreign: ReadonlySet<string>;
    readonly root: string;
    readonly special: ReadonlySet<string>;
    readonly vocabulary: Vocabulary;
}

const hostEntries = function hostEntries(host: Vocabulary): readonly RootEntry[] {
    const declaredSpecial: Readonly<Record<string, readonly string[]>> = taxonomy.specialContainers;
    return Object.entries(taxonomy.containers).map(([root, names]) => ({
        containers: new Set<string>(names),
        foreign: new Set<string>(),
        root,
        special: new Set<string>(declaredSpecial[root]),
        vocabulary: host,
    }));
};

const importEntries = function importEntries(base: string, source: TaxonomySource): readonly RootEntry[] {
    const vocabulary = createVocabulary(source);
    const foreign = source.foreignContainers ?? {};
    return [...importedRootsOf(base, source)].map(([root, key]) => ({
        containers: new Set<string>(source.containers[key]),
        foreign: new Set<string>(foreign[key]),
        root,
        special: new Set<string>(source.specialContainers[key]),
        vocabulary,
    }));
};

const mirrorEntries = function mirrorEntries(
    mirrors: Readonly<Record<string, string>>,
    sources: readonly RootEntry[],
): readonly RootEntry[] {
    const byRoot = new Map(sources.map((entry) => [entry.root, entry]));
    return Object.entries(mirrors).flatMap(([mirror, source]) => {
        const entry = byRoot.get(source);
        return entry === undefined ? [] : [{ ...entry, root: mirror }];
    });
};

const assertHost = function assertHost(host: Vocabulary, entries: readonly RootEntry[]): void {
    assertTaxonomy(
        {
            byFolder: host.byFolder,
            byTag: host.byTag,
            compoundMarkers: host.compoundMarkers,
            containers: new Map(entries.map((entry) => [entry.root, entry.containers])),
            declaredContainers: taxonomy.containers,
            declaredSpecial: taxonomy.specialContainers,
            dialects: host.dialects,
            generatedFolder: host.generatedFolder,
            ignoredNames: host.ignoredNames,
            markerFolders: host.markerFolders,
            maxDepth: host.maxDepth,
            special: new Map(entries.map((entry) => [entry.root, entry.special])),
            splitters: taxonomy.grammar.splitters,
            subjects: host.subjects,
            fixtureMarkers: host.fixtureMarkers,
            testMarkers: host.testMarkers,
        },
        taxonomy.concerns.length,
    );
};

const buildJurisdiction = function buildJurisdiction(): Jurisdiction {
    const host = createVocabulary(taxonomy);
    const declared = hostEntries(host);
    assertHost(host, declared);
    const sources = [...declared, ...Object.entries(imports).flatMap(([base, source]) => importEntries(base, source))];
    assertMirrors(taxonomy.testMirrors, new Set(sources.map((entry) => entry.root)));
    const entries = [...sources, ...mirrorEntries(taxonomy.testMirrors, sources)];
    const containers = new Map(entries.map((entry) => [entry.root, entry.containers]));
    return {
        containers,
        foreign: new Map(entries.map((entry) => [entry.root, entry.foreign])),
        host,
        mirrors: new Map(Object.entries(taxonomy.testMirrors)),
        roots: [...containers.keys()].toSorted((a, b) => b.length - a.length),
        special: new Map(entries.map((entry) => [entry.root, entry.special])),
        vocabularies: new Map(entries.map((entry) => [entry.root, entry.vocabulary])),
    };
};

export const JURISDICTION: Jurisdiction = buildJurisdiction();
```
