string.ts

Declarations
#

17 declarations

view source

count_graphemes
#

string.ts view source

(str: string): number

Returns the count of graphemes in a string, the individually rendered characters.

str

type string

returns

number

deindent
#

string.ts view source

(str: string): string

Removes leading and trailing spaces from each line of a string.

str

type string

returns

string

ensure_end
#

string.ts view source

(source: string, ensured: string): string

Adds the substring ensured to the end of the source string if it's not already present.

source

type string

ensured

type string

returns

string

ensure_start
#

string.ts view source

(source: string, ensured: string): string

Adds the substring ensured to the start of the source string if it's not already present.

source

type string

ensured

type string

returns

string

escape_js_string
#

string.ts view source

(value: string): string

Escapes a string for use inside a single-quoted JS string literal.

Uses a single-pass regex replacement to escape backslashes, single quotes, newlines, carriage returns, and Unicode line/paragraph separators.

value

type string

returns

string

levenshtein_distance
#

string.ts view source

(a: string, b: string): number

Calculates the Levenshtein distance between two strings. Useful for typo detection and fuzzy matching.

a

first string

type string

b

second string

type string

returns

number

the edit distance between the strings

pad_width
#

string.ts view source

(str: string, target_width: number, align?: "left" | "right"): string

Pad a string to a target display width (accounting for wide characters).

str

type string

target_width

type number

align

type "left" | "right"
default 'left'

returns

string

plural
#

string.ts view source

(count: number | null | undefined, suffix?: string): string

Returns a plural suffix based on a count.

count

type number | null | undefined

suffix

type string
default 's'

returns

string

string_display_width
#

string.ts view source

(str: string): number

Calculate the display width of a string in terminal columns. - Strips ANSI escape codes (they have 0 width) - Emojis and other wide characters take 2 columns - Tab characters take 4 columns - Newlines and other control characters take 0 columns - Uses Intl.Segmenter to properly handle grapheme clusters (e.g., family emoji "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ")

str

type string

returns

number

string_is_binary
#

string.ts view source

(content: string): boolean

Check if content appears to be binary.

Checks for null bytes in the first 8KB of content.

content

content to check

type string

returns

boolean

true if content appears to be binary

stringify
#

string.ts view source

(value: unknown): string

Stringifies a value like JSON.stringify but with some corner cases handled better.

value

type unknown

returns

string

strip_after
#

string.ts view source

(source: string, stripped: string): string

Removes characters inclusive of stripped.

source

type string

stripped

type string

returns

string

strip_ansi
#

string.ts view source

(str: string): string

Strips ANSI escape sequences from a string.

str

type string

returns

string

strip_before
#

string.ts view source

(source: string, stripped: string): string

Removes characters inclusive of stripped.

source

type string

stripped

type string

returns

string

strip_end
#

string.ts view source

(source: string, stripped: string): string

Removes characters inclusive of stripped.

source

type string

stripped

type string

returns

string

strip_start
#

string.ts view source

(source: string, stripped: string): string

Removes characters inclusive of stripped.

source

type string

stripped

type string

returns

string

truncate
#

string.ts view source

(str: string, maxLength: number, suffix?: string): string

Truncates a string to a maximum length, adding a suffix if needed that defaults to ....

str

type string

maxLength

type number

suffix

type string
default '...'

returns

string

Depends on
#

Imported by
#