# core/converters/source.segment.converter.ts

> 42 lines of code and 13 definitions.

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

Listed in [core/converters](https://banes-lab.com/api/source/build-scripts/core/converters.md), after [core/converters/source.route.converter.ts](https://banes-lab.com/source/build-scripts/core/converters/source.route.converter.ts.md) and before [core/converters/specifier.converter.ts](https://banes-lab.com/source/build-scripts/core/converters/specifier.converter.ts.md).

## Definitions

- `partPathOf` (lexical_declaration, line 20, exported)
- `sourcePartsOf` (lexical_declaration, line 24, exported)
- `LINE_BREAK` (lexical_declaration, line 4)
- `PART_MARK` (lexical_declaration, line 5)
- `partSlicesOf` (lexical_declaration, line 7, exported)
- `start` (lexical_declaration, line 9, exported)
- `cut` (lexical_declaration, line 11, exported)
- `lineEnd` (lexical_declaration, line 12, exported)
- `end` (lexical_declaration, line 13, exported)
- `slices` (lexical_declaration, line 25, exported)
- `{ subject }` (lexical_declaration, line 29, exported)
- `count` (lexical_declaration, line 30, exported)
- `links` (lexical_declaration, line 31, exported)

## Contained in

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

## Uses

- [configuration/strings/page.strings.ts](https://banes-lab.com/source/build-scripts/configuration/strings/page.strings.ts.md)

## Used by

- [core/converters/section.page.converter.ts](https://banes-lab.com/source/build-scripts/core/converters/section.page.converter.ts.md)
- [core/coordinators/page.coordinator.ts](https://banes-lab.com/source/build-scripts/core/coordinators/page.coordinator.ts.md)

## Linked from

- [configuration/strings](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-configuration-strings.md)
- [core/converters](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-converters.md)
- [core/coordinators](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-coordinators.md)

## Source

```typescript
import { partLabel, partTitle, sourcePartDescription } from "#configuration/strings/page.strings";
import type { SourceRoute } from "#types/source.types";

const LINE_BREAK = "\n";
const PART_MARK = "~";

export const partSlicesOf = function partSlicesOf(text: string, limit: number): readonly (readonly [number, number])[] {
    const slices: (readonly [number, number])[] = [];
    let start = 0;
    while (start < text.length) {
        const cut = start + limit;
        const lineEnd = cut >= text.length ? text.length : text.lastIndexOf(LINE_BREAK, cut);
        const end = cut >= text.length ? text.length : lineEnd > start ? lineEnd + 1 : cut;
        slices.push([start, end]);
        start = end;
    }
    return slices;
};

export const partPathOf = function partPathOf(path: string, index: number): string {
    return index === 0 ? path : path + PART_MARK + String(index + 1);
};

export const sourcePartsOf = function sourcePartsOf(route: SourceRoute, text: string, limit: number): readonly SourceRoute[] {
    const slices = partSlicesOf(text, limit);
    if (slices.length < 2) {
        return [route];
    }
    const { subject } = route;
    const count = slices.length;
    const links = slices.map((_, index) => ({
        label: partLabel(index + 1, count),
        path: partPathOf(subject.path, index),
    }));
    return slices.map((slice, index) => ({
        ...route,
        children: links,
        slice,
        subject: {
            ...subject,
            description: index === 0 ? subject.description : sourcePartDescription(subject.name, index + 1, count),
            path: partPathOf(subject.path, index),
            title: index === 0 ? subject.title : partTitle(subject.title, index + 1, count),
        },
    }));
};
```
