string.ts

view source

Declarations
#

17 declarations

count_graphemes
#

string.ts view source

(str: string): number import {count_graphemes} from '@fuzdev/fuz_util/string.js';

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 import {deindent} from '@fuzdev/fuz_util/string.js';

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 import {ensure_end} from '@fuzdev/fuz_util/string.js';

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 import {ensure_start} from '@fuzdev/fuz_util/string.js';

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 import {escape_js_string} from '@fuzdev/fuz_util/string.js';

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 import {levenshtein_distance} from '@fuzdev/fuz_util/string.js';

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

a

type string

b

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 import {pad_width} from '@fuzdev/fuz_util/string.js';

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 import {plural} from '@fuzdev/fuz_util/string.js';

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 import {string_display_width} from '@fuzdev/fuz_util/string.js';

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 import {string_is_binary} from '@fuzdev/fuz_util/string.js';

Check if content appears to be binary.

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

content

type string

returns

boolean

stringify
#

string.ts view source

(value: unknown): string import {stringify} from '@fuzdev/fuz_util/string.js';

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 import {strip_after} from '@fuzdev/fuz_util/string.js';

Removes characters inclusive of stripped.

source

type string

stripped

type string

returns

string

strip_ansi
#

string.ts view source

(str: string): string import {strip_ansi} from '@fuzdev/fuz_util/string.js';

Strips ANSI escape sequences from a string.

str

type string

returns

string

strip_before
#

string.ts view source

(source: string, stripped: string): string import {strip_before} from '@fuzdev/fuz_util/string.js';

Removes characters inclusive of stripped.

source

type string

stripped

type string

returns

string

strip_end
#

string.ts view source

(source: string, stripped: string): string import {strip_end} from '@fuzdev/fuz_util/string.js';

Removes characters inclusive of stripped.

source

type string

stripped

type string

returns

string

strip_start
#

string.ts view source

(source: string, stripped: string): string import {strip_start} from '@fuzdev/fuz_util/string.js';

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 import {truncate} from '@fuzdev/fuz_util/string.js';

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
#