# core/converters/diagram.converter.ts

> 43 lines of code and 9 definitions.

Tree: GovLab Docs
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/docs#file-docs-core-converters-diagram-converter-ts
Source text: https://banes-lab.com/source/docs/core/converters/diagram.converter.ts.txt

Listed in [core/converters](https://banes-lab.com/api/source/docs/core/converters.md), after [core/converters/dependency.converter.ts](https://banes-lab.com/source/docs/core/converters/dependency.converter.ts.md) and before [core/converters/document.converter.ts](https://banes-lab.com/source/docs/core/converters/document.converter.ts.md).

## Definitions

- `shapeForKind` (lexical_declaration, line 9, exported)
- `weightForKind` (lexical_declaration, line 22, exported)
- `classForKind` (lexical_declaration, line 35, exported)
- `dedupEdges` (lexical_declaration, line 39, exported)
- `ENDPOINT_KINDS` (lexical_declaration, line 5)
- `DECISION_KINDS` (lexical_declaration, line 6)
- `DEPENDENCY_KINDS` (lexical_declaration, line 7)
- `byKey` (lexical_declaration, line 40, exported)
- `key` (lexical_declaration, line 42, exported)

## Contained in

- [core/converters](https://banes-lab.com/anatomy/docs/folder-docs-core-converters.md)

## Used by

- [core/plugins/diagram.dependency.plugin.ts](https://banes-lab.com/source/docs/core/plugins/diagram.dependency.plugin.ts.md)
- [core/plugins/diagram.invocation.plugin.ts](https://banes-lab.com/source/docs/core/plugins/diagram.invocation.plugin.ts.md)

## Linked from

- [core/plugins](https://banes-lab.com/anatomy/docs/folder-docs-core-plugins.md)

## Source

```typescript
import { CLASS_BY_KIND, DEFAULT_CLASS } from "#configuration/constants/graph.constants";
import type { CodeEdgeKind, CodeNodeKind } from "#types/graph.types";
import type { EdgeWeight, GraphEdge, NodeShape } from "#types/diagram.types";

const ENDPOINT_KINDS: ReadonlySet<CodeNodeKind> = new Set<CodeNodeKind>(["entry", "factory", "exit"]);
const DECISION_KINDS: ReadonlySet<CodeNodeKind> = new Set<CodeNodeKind>(["decision", "gate"]);
const DEPENDENCY_KINDS: ReadonlySet<CodeEdgeKind> = new Set<CodeEdgeKind>(["data-flow", "dependency"]);

export const shapeForKind = function shapeForKind(kind: CodeNodeKind): NodeShape {
    if (ENDPOINT_KINDS.has(kind)) {
        return "endpoint";
    }
    if (kind === "collaborator") {
        return "collaborator";
    }
    if (DECISION_KINDS.has(kind)) {
        return "decision";
    }
    return kind === "store" ? "store" : "rect";
};

export const weightForKind = function weightForKind(kind: CodeEdgeKind): EdgeWeight {
    if (kind === "hook-register") {
        return "hook";
    }
    if (kind === "teardown") {
        return "teardown";
    }
    if (DEPENDENCY_KINDS.has(kind)) {
        return "dependency";
    }
    return kind === "branch" ? "branch" : "call";
};

export const classForKind = function classForKind(kind: CodeNodeKind): string {
    return CLASS_BY_KIND.get(kind) ?? DEFAULT_CLASS;
};

export const dedupEdges = function dedupEdges(edges: readonly GraphEdge[]): GraphEdge[] {
    const byKey = new Map<string, GraphEdge>();
    for (const edge of edges) {
        const key = `${edge.from} ${edge.to} ${edge.weight ?? ""} ${edge.label ?? ""}`;
        if (!byKey.has(key)) {
            byKey.set(key, edge);
        }
    }
    return [...byKey.values()];
};
```
