# core/converters/stage.converter.ts

> 26 lines of code and 6 definitions.

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

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

## Definitions

- `stepCount` (lexical_declaration, line 3, exported)
- `labelsOf` (lexical_declaration, line 7, exported)
- `planUnits` (lexical_declaration, line 11, exported)
- `flat` (lexical_declaration, line 12, exported)
- `to` (lexical_declaration, line 17, exported)
- `stageLabels` (lexical_declaration, line 24, exported)

## Contained in

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

## Source

```typescript
import type { PlannedStep, Stage, StagePlan, Step, Unit } from "#types/stage.types";

export const stepCount = function stepCount(step: Step): number {
    return step.parallel ? step.parallel.length : 1;
};

export const labelsOf = function labelsOf(step: Step): string[] {
    return step.parallel ? step.parallel.map((sub) => sub.label) : [step.label ?? ""];
};

export const planUnits = function planUnits(active: readonly Stage[]): Unit[] {
    const flat: PlannedStep[] = active.flatMap((stage) =>
        stage.steps.map((step, stepIndex) => ({ firstInStage: stepIndex === 0, stage, step })),
    );
    return flat.reduce<{ units: Unit[]; cursor: number }>(
        (acc, entry) => {
            const to = acc.cursor + stepCount(entry.step);
            return { cursor: to, units: [...acc.units, { ...entry, from: acc.cursor + 1, to }] };
        },
        { cursor: 0, units: [] },
    ).units;
};

export const stageLabels = function stageLabels(plan: StagePlan): string[] {
    return plan.stages.flatMap((stage) => [
        stage.label,
        stage.slug,
        ...stage.steps.flatMap(labelsOf).filter((label) => label.length > 0),
    ]);
};
```
