# core/formatters/diagram.sequence.formatter.ts

> 30 lines of code and 5 definitions.

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

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

## Definitions

- `emitSequence` (lexical_declaration, line 24, exported)
- `arrow` (lexical_declaration, line 7)
- `stepLine` (lexical_declaration, line 11)
- `INDENT` (lexical_declaration, line 5)
- `{ message }` (lexical_declaration, line 13)

## Contained in

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

## Uses

- [core/formatters/diagram.formatter.ts](https://banes-lab.com/source/docs/core/formatters/diagram.formatter.ts.md)
- [core/normalizers/diagram.normalizer.ts](https://banes-lab.com/source/docs/core/normalizers/diagram.normalizer.ts.md)

## Used by

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

## Linked from

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

## Source

```typescript
import type { MessageKind, SequenceModel, SequenceStep } from "#types/diagram.types";
import { idSanitize, label } from "#core/normalizers/diagram.normalizer";
import { accLines } from "#core/formatters/diagram.formatter";

const INDENT = "    ";

const arrow = function arrow(kind: MessageKind): string {
    return kind === "return" ? "-->>" : "->>";
};

const stepLine = function stepLine(step: SequenceStep): string[] {
    if (step.message) {
        const { message } = step;
        return [
            `${INDENT}${idSanitize(message.from)}${arrow(message.kind)}${idSanitize(message.to)}: ${label(message.text)}`,
        ];
    }
    if (step.note) {
        return [`${INDENT}Note over ${step.note.over.map(idSanitize).join(",")}: ${label(step.note.text)}`];
    }
    return [];
};

export const emitSequence = function emitSequence(model: SequenceModel): string {
    return [
        "sequenceDiagram",
        ...accLines(model),
        ...(model.autonumber === true ? [`${INDENT}autonumber`] : []),
        ...model.participants.map(
            (participant) => `${INDENT}participant ${idSanitize(participant.id)} as ${label(participant.label)}`,
        ),
        ...model.steps.flatMap(stepLine),
    ].join("\n");
};
```
