# core/converters/definition.converter.ts

> 41 lines of code and 8 definitions.

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

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

## Definitions

- `sortedAt` (lexical_declaration, line 17)
- `definitionRecords` (lexical_declaration, line 21, exported)
- `adjacency` (lexical_declaration, line 5)
- `map` (lexical_declaration, line 9)
- `[key, value]` (lexical_declaration, line 11)
- `callees` (lexical_declaration, line 25, exported)
- `callers` (lexical_declaration, line 26, exported)
- `key` (lexical_declaration, line 28, exported)

## Contained in

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

## Used by

- [core/coordinators/package.coordinator.ts](https://banes-lab.com/source/patterns/core/coordinators/package.coordinator.ts.md)

## Linked from

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

## Source

```typescript
import type { CallEdge, CodeGraph, CodeInsight } from "#types/code.types";
import type { DefinitionRecord } from "#types/report.types";
import { keyOf } from "#core/formatters/definition.formatter";

const adjacency = function adjacency(
    edges: readonly CallEdge[],
    endpoints: (edge: CallEdge) => readonly [string, string],
): Map<string, string[]> {
    const map = new Map<string, string[]>();
    for (const edge of edges) {
        const [key, value] = endpoints(edge);
        map.set(key, [...(map.get(key) ?? []), value]);
    }
    return map;
};

const sortedAt = function sortedAt(map: ReadonlyMap<string, string[]>, key: string): string[] {
    return [...(map.get(key) ?? [])].sort((a, b) => a.localeCompare(b));
};

export const definitionRecords = function definitionRecords(
    insight: CodeInsight,
    graph: CodeGraph,
): DefinitionRecord[] {
    const callees = adjacency(graph.edges, (edge) => [edge.from, edge.to]);
    const callers = adjacency(graph.edges, (edge) => [edge.to, edge.from]);
    return insight.symbols.map((stat) => {
        const key = keyOf(stat.file, stat.name);
        return {
            callable: stat.callable,
            callees: sortedAt(callees, key),
            callers: sortedAt(callers, key),
            exported: stat.exported,
            file: stat.file,
            flow: stat.flow,
            inDegree: stat.inDegree,
            kind: stat.kind,
            line: stat.line,
            local: stat.local,
            name: stat.name,
            outDegree: stat.outDegree,
        };
    });
};
```
