# core/converters/text.converter.ts

> 44 lines of code and 12 definitions.

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

Listed in [core/converters](https://banes-lab.com/api/source/deploy/core/converters.md), after [core/converters/search.converter.ts](https://banes-lab.com/source/deploy/core/converters/search.converter.ts.md).

## Definitions

- `formatBytes` (lexical_declaration, line 18, exported)
- `formatDuration` (lexical_declaration, line 25, exported)
- `formatClock` (lexical_declaration, line 33, exported)
- `stamp` (lexical_declaration, line 40, exported)
- `STAMP_PUNCTUATION` (lexical_declaration, line 16)
- `padded` (lexical_declaration, line 29)
- `whole` (lexical_declaration, line 34, exported)
- `hours` (lexical_declaration, line 35, exported)
- `minutes` (lexical_declaration, line 36, exported)
- `iso` (lexical_declaration, line 41, exported)
- `out` (lexical_declaration, line 42, exported)
- `truncate` (lexical_declaration, line 49, exported)

## Contained in

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

## Used by

- [core/formatters/journal.formatter.ts](https://banes-lab.com/source/deploy/core/formatters/journal.formatter.ts.md)
- [core/reporters/notification.reporter.ts](https://banes-lab.com/source/deploy/core/reporters/notification.reporter.ts.md)
- [core/steps/recovery.step.ts](https://banes-lab.com/source/deploy/core/steps/recovery.step.ts.md)
- [runtime/entrypoints/deployment.entrypoint.ts](https://banes-lab.com/source/deploy/runtime/entrypoints/deployment.entrypoint.ts.md)

## Linked from

- [core/formatters](https://banes-lab.com/anatomy/deploy/folder-deploy-core-formatters.md)
- [core/reporters](https://banes-lab.com/anatomy/deploy/folder-deploy-core-reporters.md)
- [core/steps](https://banes-lab.com/anatomy/deploy/folder-deploy-core-steps.md)
- [runtime/entrypoints](https://banes-lab.com/anatomy/deploy/folder-deploy-runtime-entrypoints.md)

## Source

```typescript
import {
    BYTES_PER_GB,
    BYTES_PER_MB,
    CLOCK_FILL,
    CLOCK_PAD,
    CLOCK_SEPARATOR,
    DECIMALS,
    MS_PER_SECOND,
    SECONDS_PER_HOUR,
    SECONDS_PER_MINUTE,
    STAMP_LENGTH,
    STAMP_SEPARATOR,
} from "#configuration/constants/deployment.constants";
import { GIGABYTE, MEGABYTE } from "#configuration/strings/notification.strings";

const STAMP_PUNCTUATION = new Set([":", ".", "T"]);

export const formatBytes = function formatBytes(bytes: number): string {
    if (bytes >= BYTES_PER_GB) {
        return (bytes / BYTES_PER_GB).toFixed(DECIMALS) + GIGABYTE;
    }
    return (bytes / BYTES_PER_MB).toFixed(DECIMALS) + MEGABYTE;
};

export const formatDuration = function formatDuration(milliseconds: number): string {
    return (milliseconds / MS_PER_SECOND).toFixed(DECIMALS);
};

const padded = function padded(value: number): string {
    return String(value).padStart(CLOCK_PAD, CLOCK_FILL);
};

export const formatClock = function formatClock(seconds: number): string {
    const whole = Math.max(0, Math.round(seconds));
    const hours = Math.floor(whole / SECONDS_PER_HOUR);
    const minutes = Math.floor((whole % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
    return [hours, minutes, whole % SECONDS_PER_MINUTE].map(padded).join(CLOCK_SEPARATOR);
};

export const stamp = function stamp(): string {
    const iso = new Date().toISOString().slice(0, STAMP_LENGTH);
    let out = "";
    for (const character of iso) {
        out += STAMP_PUNCTUATION.has(character) ? STAMP_SEPARATOR : character;
    }
    return out;
};

export const truncate = function truncate(text: string, limit: number, marker: string): string {
    return text.length > limit ? text.slice(0, limit) + marker : text;
};
```
