# core/formatters/graph.formatter.ts

> 29 lines of code and 3 definitions.

Tree: Bane's Lab Build Scripts
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/build-scripts#file-build-scripts-core-formatters-graph-formatter-ts
Source text: https://banes-lab.com/source/build-scripts/core/formatters/graph.formatter.ts.txt

Listed in [core/formatters](https://banes-lab.com/api/source/build-scripts/core/formatters.md), after [core/formatters/diagram.formatter.ts](https://banes-lab.com/source/build-scripts/core/formatters/diagram.formatter.ts.md) and before [core/formatters/icons.formatter.ts](https://banes-lab.com/source/build-scripts/core/formatters/icons.formatter.ts.md).

## Definitions

- `renderGraphChunk` (lexical_declaration, line 4, exported)
- `renderGraphLoader` (lexical_declaration, line 13, exported)
- `entries` (lexical_declaration, line 14, exported)

## Contained in

- [core/formatters](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-formatters.md)

## Used by

- [core/persistence/graph.persistence.ts](https://banes-lab.com/source/build-scripts/core/persistence/graph.persistence.ts.md)

## Linked from

- [core/persistence](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-persistence.md)

## Source

```typescript
import { ASSET_SPECIFIER, CHUNK_JOINER, CHUNK_STEM, GENERATED_TAG } from "#configuration/constants/graph.constants";
import type { GraphChunk } from "@banes-lab/web/types/graph.types.js";

export const renderGraphChunk = function renderGraphChunk(chunk: GraphChunk): string {
    return [
        'import type { GraphChunk } from "#types/graph.types";',
        "",
        `export const GRAPH: GraphChunk = JSON.parse(${JSON.stringify(JSON.stringify(chunk))});`,
        "",
    ].join("\n");
};

export const renderGraphLoader = function renderGraphLoader(collections: readonly string[]): string {
    const entries = collections.map(
        (collection) =>
            `    [${JSON.stringify(collection)}, () => import(${JSON.stringify(ASSET_SPECIFIER + CHUNK_STEM + CHUNK_JOINER + collection + GENERATED_TAG)})],`,
    );
    return [
        'import type { GraphChunk } from "#types/graph.types";',
        "",
        "const LOADERS: ReadonlyMap<string, () => Promise<{ readonly GRAPH: GraphChunk }>> = new Map([",
        ...entries,
        "]);",
        "",
        "export const loadGraph = async function loadGraph(collection: string): Promise<GraphChunk | null> {",
        "    const load = LOADERS.get(collection);",
        "    return load === undefined ? null : (await load()).GRAPH;",
        "};",
        "",
    ].join("\n");
};
```
