ts-uf

string

validation

isString

function isString<T>(value: CheckT<T>): value is SmartExtract<T, string>;

Checks whether a value is a string.

Example
function foo(value: string | string[]) {
	if (isString(value)) {
		value; // string
	}
}

isNotString

function isNotString<T>(value: CheckT<T>): value is Exclude<T, string>;

Checks whether a value is not a string.

Example
function foo(value: string | string[]) {
	if (isNotString(value)) {
		value; // string[]
	}
}

isEmptyString

function isEmptyString<T>(value: CheckT<T>): value is Extract<"", T>;

Checks whether a value is an empty string.

Example
function foo(value: string) {
	if (isEmptyString(value)) {
		value; // ""
	}
}

isNonEmptyString

function isNonEmptyString<T>(
	value: CheckT<T>,
): value is Exclude<SmartExtract<T, string>, "">;

Checks whether a value is a non-empty string.

Example
function foo(value: string | string[]) {
	if (isNonEmptyString(value)) {
		value; // type: string // value !== ""
	}
}

isBlank

function isBlank<T>(value: CheckT<T>): value is SmartExtract<T, string>;

Checks whether a value is a string that's empty or empty-like.

Example
isBlank(""); // true
isBlank("  "); // true
isBlank("\t\n"); // true

isBlank("hello"); // false
isBlank("   h"); // false

isNonBlank

function isNonBlank<T>(value: CheckT<T>): value is SmartExtract<T, string>;

Checks whether a value is a string that's neither empty nor empty-like.

Example
isNonBlank(""); // false
isNonBlank("  "); // false
isNonBlank("\t\n"); // false

isNonBlank("hello"); // true
isNonBlank("   h"); // true

case

capitalize

function capitalize<T extends string>(value: T): string extends T ? string : Capitalize<T>;

Capitalizes the first character of a string.

Example
capitalize("hello"); // "Hello"

uncapitalize

function uncapitalize<T extends string>(
	value: T,
): string extends T ? string : Uncapitalize<T>;

Uncapitalizes the first character of a string.

Example
uncapitalize("Hello"); // "hello"

char

getFirstChar

function getFirstChar<T extends string>(value: T): string extends T ? string : FirstChar<T>;

Returns the first character of a string.

Example
getFirstChar("hello"); // "h"

getLastChar

function getLastChar<T extends string>(value: T): string extends T ? string : LastChar<T>;

Returns the last character of a string.

Example
getLastChar("hello"); // "o"

firstCharIsUppercase

function firstCharIsUppercase(value: string): boolean;

Checks whether the first character of a string is uppercase.

Example
firstCharIsUppercase("hello"); // false
firstCharIsUppercase("Hello"); // true

firstCharIsLowercase

function firstCharIsLowercase(value: string): boolean;

Checks whether the first character of a string is lowercase.

Example
firstCharIsLowercase("hello"); // true
firstCharIsLowercase("Hello"); // false

format

formatString

function formatString<P extends string>(pattern: P, context: ExtractContext<P>): string;

Formats a string by replacing placeholders with values from a context object.

Supports {name} and {name=default} placeholders.

Matching starts at the first { and ends at the first } and fails if there's another { in between.

Example
formatString("Hello {name}", { name: "InZero" }); // "Hello InZero"
formatString("Hello {name=user}", {}); // "Hello user"

formatString("Hello {na{me}", { me: "<<<" }); // "Hello {na<<<"

formatString("Hello, {name}! Your age is {age=unknown}.", { name: "John" }); // "Hello, John! Your age is unknown."
formatString("{{a}-{b}-{c}}", {a: 1, b: 2, c: 3}); // "{1-2-3}"

TYPES

type ExtractContext<T extends string>

prefix

prefix

function prefix<T extends string, P extends string>(
	string: T,
	prefix: P,
): string extends T | P ? string : Prefix<T, P>;

Adds a prefix to a string.

Example
prefix("key", "$"); // "$key"

ensurePrefix

function ensurePrefix<T extends string, P extends string>(
	string: T,
	prefix: P,
): string extends T | P ? string : EnsurePrefix<T, P>;

Adds a prefix to a string if it doesn't already have it.

Example
ensurePrefix("key", "$"); // "$key"
ensurePrefix("$key", "$"); // "$key"

unPrefix

function unPrefix<T extends string, P extends string>(
	string: T,
	prefix: P,
): string extends T | P ? string : UnPrefix<T, P>;

Removes a prefix from a string if it has it.

Example
unPrefix("$key", "$"); // "key"
unPrefix("key", "$"); // "key"

suffix

function suffix<T extends string, S extends string>(
	string: T,
	suffix: S,
): string extends T | S ? string : Suffix<T, S>;

Adds a suffix to a string.

Example
suffix("key", "$"); // "key$"

ensureSuffix

function ensureSuffix<T extends string, S extends string>(
	string: T,
	suffix: S,
): string extends T | S ? string : EnsureSuffix<T, S>;

Adds a suffix to a string if it doesn't already have it.

Example
ensureSuffix("key", "$"); // "key$"
ensureSuffix("key$", "$"); // "key$"

unSuffix

function unSuffix<T extends string, S extends string>(
	string: T,
	suffix: S,
): string extends T | S ? string : UnSuffix<T, S>;

Removes a suffix from a string if it has it.

Example
unSuffix("key$", "$"); // "key"
unSuffix("key", "$"); // "key"

replace

replaceSubstring

function replaceSubstring(string: string, ...updates: ReplaceUpdate[]): string;

type ReplaceUpdate = [start: number, end: number, replacement: string];

Replaces a substring using its coordinates.

You don't have to worry about adjusting the ranges.

Throws a RangeError if:

  • start > end or start < 0.
  • some ranges overlap.
Example
replaceSubstring("Hello World!", [6, 11, "Universe"]); // "Hello Universe!"
replaceSubstring("012345678", [0, 2, "x"], [3, 5, "y"], [6, 8, "z"]); // "x2y5z8"

deleteSubstring

function deleteSubstring(string: string, ...ranges: RORange[]): string;

type RORange = readonly [start: number, end: number];

Deletes a substring using its coordinates.

Automatically adjusts the ranges of subsequent deletions to account for changes in string length caused by earlier deletions, ensuring each deletion targets the intended substring.

Throws a RangeError if:

  • start > end or start < 0.
  • some ranges overlap.
Example
deleteSubstring("Hello World And Welcome!", [5, 11], [23, 24]); // "Hello And Welcome"

insertSubstring

function insertSubstring(string: string, ...updates: InsertUpdate[]): string;

type InsertUpdate = [index: number, insertion: string];

Inserts a substring at a given index.

Automatically adjusts the ranges of subsequent insertions to account for changes in string length caused by earlier insertions, ensuring each insertion occurs at the intended position.

Throws a RangeError if:

  • index < 0.
  • some ranges overlap.
Example
insertSubstring("Hello World!", [6, "("], [11, ")"]); // "Hello (World)!"

On this page