iniquity

A re-imagining of the iconic BBS software.

View on GitHub

Module: core/string-utils

String Utilities

Summary

Opt-in string manipulation utilities (no global pollution)

Table of contents

Functions

Functions

addNewlines

addNewlines(text, count?): string

Add newlines before text

Parameters

Name Type Default value Description
text string undefined Text to prepend newlines to
count number 1 Number of newlines (defaults to 1)

Returns

string

Text with newlines prepended

Defined in

core/src/string-utils.ts:77


centerText

centerText(text, width?): string

Center text within specified width

Example

import { centerText } from '@iniquitybbs/core/utils'
const centered = centerText("Hello", 80)

Parameters

Name Type Default value Description
text string undefined Text to center
width number 80 Target width (defaults to 80)

Returns

string

Centered text with padding

Defined in

core/src/string-utils.ts:37


colorText

colorText(text, color): string

Add ANSI color to text

Example

import { colorText } from '@iniquitybbs/core/utils'
const colored = colorText("Hello", "cyan")

Parameters

Name Type Description
text string Text to colorize
color string Color name (e.g., ‘cyan’, ‘bright red’)

Returns

string

Colorized text with ANSI codes

Defined in

core/src/string-utils.ts:21


gotoxyText

gotoxyText(text, x, y): string

Position cursor at coordinates before displaying text

Parameters

Name Type Description
text string Text to display
x number X coordinate (1-based)
y number Y coordinate (1-based)

Returns

string

Text with cursor positioning

Defined in

core/src/string-utils.ts:167


leftAlign

leftAlign(text, width?): string

Left-align text within specified width

Parameters

Name Type Default value Description
text string undefined Text to align
width number 80 Target width (defaults to 80)

Returns

string

Left-aligned text with right padding

Defined in

core/src/string-utils.ts:51


lowerText

lowerText(text): string

Convert text to lowercase

Parameters

Name Type Description
text string Text to convert

Returns

string

Lowercase text

Defined in

core/src/string-utils.ts:185


padText

padText(text, length, char?, side?): string

Pad text to specified length

Example

import { padText } from '@iniquitybbs/core/utils'
const padded = padText("Hello", 10, ' ', 'right')  // "Hello     "

Parameters

Name Type Default value Description
text string undefined Text to pad
length number undefined Target length
char string " " Character to use for padding (defaults to space)
side "left" | "right" | "both" "right" Side to pad (‘left’, ‘right’, or ‘both’)

Returns

string

Padded text

Defined in

core/src/string-utils.ts:143


repeatText

repeatText(text, count): string

Repeat text multiple times

Parameters

Name Type Description
text string Text to repeat
count number Number of repetitions

Returns

string

Repeated text

Defined in

core/src/string-utils.ts:204


rightAlign

rightAlign(text, width?): string

Right-align text within specified width

Parameters

Name Type Default value Description
text string undefined Text to align
width number 80 Target width (defaults to 80)

Returns

string

Right-aligned text with left padding

Defined in

core/src/string-utils.ts:64


stripAnsi

stripAnsi(text): string

Strip ANSI escape codes from text

Example

import { stripAnsi } from '@iniquitybbs/core/utils'
const plain = stripAnsi("\x1b[1;37mHello\x1b[0m")  // "Hello"

Parameters

Name Type Description
text string Text containing ANSI codes

Returns

string

Text with ANSI codes removed

Defined in

core/src/string-utils.ts:96


titleText

titleText(text): string

Convert text to title case

Parameters

Name Type Description
text string Text to convert

Returns

string

Title-cased text

Defined in

core/src/string-utils.ts:194


truncateText

truncateText(text, maxLength, suffix?): string

Truncate text to maximum length

Parameters

Name Type Default value Description
text string undefined Text to truncate
maxLength number undefined Maximum length
suffix string "..." Suffix to add if truncated (defaults to ‘…’)

Returns

string

Truncated text

Defined in

core/src/string-utils.ts:122


upperText

upperText(text): string

Convert text to uppercase

Parameters

Name Type Description
text string Text to convert

Returns

string

Uppercase text

Defined in

core/src/string-utils.ts:176


visibleLength

visibleLength(text): number

Get visible length of text (excluding ANSI codes)

Example

import { visibleLength } from '@iniquitybbs/core/utils'
const len = visibleLength("\x1b[1;37mHello\x1b[0m")  // 5

Parameters

Name Type Description
text string Text to measure

Returns

number

Visible character count

Defined in

core/src/string-utils.ts:111