Skip to content

Str

const Str: object

Defined in: Data/Str.ts:62

capitalize: (text) => string

Converts the first character of a string to uppercase.

string

string

uncapitalize for converting the first character to lowercase.

pipe("hello", Str.capitalize); // "Hello"

endsWith: (suffix) => (text) => boolean

Returns true when the string ends with the given suffix.

string

(text) => boolean

startsWith for checking prefix matches.

pipe("hello world", Str.endsWith("world")); // true
pipe("hello world", Str.endsWith("hello")); // false

graphemeSize: (text) => number

Returns the number of user-perceived characters (grapheme clusters) in the string, correctly counting emojis and complex Unicode sequences.

string

number

size for fast UTF-16 code-unit length.

pipe("👨‍👩‍👧‍👦", Str.graphemeSize); // 1
pipe("👨‍👩‍👧‍👦", Str.size);         // 11

includes: (substring) => (text) => boolean

Returns true when the string contains the given substring.

string

(text) => boolean

pipe("hello world", Str.includes("world")); // true
pipe("hello world", Str.includes("xyz"));   // false

is: object

Predicates and type guards for strings.

blank: (text) => boolean = isBlank

Returns true when the string is empty or contains only whitespace.

string

boolean

empty for checking zero length without trimming whitespace.

pipe("   ", Str.is.blank); // true
pipe("hi", Str.is.blank);  // false

empty: (text) => boolean = isEmpty

Returns true when the string is empty.

string

boolean

nonEmpty for checking if a string contains at least one character.

pipe("", Str.is.empty);   // true
pipe("hi", Str.is.empty); // false

nonEmpty: (text) => text is NonEmptyString = isNonEmpty

Type guard to check if a string is non-empty.

string

text is NonEmptyString

empty for checking if a string has zero length.

lines: (text) => readonly string[]

Splits a string into lines, normalising \r\n and \r line endings.

string

readonly string[]

Str.lines("one\ntwo\nthree"); // ["one", "two", "three"]
Str.lines("a\r\nb");         // ["a", "b"]

match: (pattern) => (text) => Maybe<RegExpMatchArray>

Matches a string against a regular expression. Pure and safe: resets pattern.lastIndex = 0 to prevent bugs with stateful /g and /y regexes.

RegExp

(text) => Maybe<RegExpMatchArray>

test for boolean validation without extracting capture groups.

pipe("hello 42", Str.match(/\d+/)); // Some(["42"])
pipe("hello", Str.match(/\d+/));    // None

NonEmpty: object = StrNonEmptyConst

Utilities for working with guaranteed non-empty strings (NonEmptyString).

from: object

string: (text) => Maybe<NonEmptyString>

Returns Some containing NonEmptyString if the string is not empty, None otherwise.

string

Maybe<NonEmptyString>

Str.NonEmpty.from.string("hello"); // Some("hello")
Str.NonEmpty.from.string("");      // None

padEnd: (maxLength, fillString?) => (text) => string

Pads the end of a string to a specified length. Data-last: use in pipe.

number

string

(text) => string

padStart for padding the left side of a string.

pipe("hi", Str.padEnd(5, "."));  // "hi..."
pipe("hi", Str.padEnd(5));       // "hi   "

padStart: (maxLength, fillString?) => (text) => string

Pads the start of a string to a specified length. Data-last: use in pipe.

number

string

(text) => string

padEnd for padding the right side of a string.

pipe("5", Str.padStart(3, "0")); // "005"
pipe("hi", Str.padStart(5));     // "   hi"

parse: object

Safe number parsers that return Maybe instead of NaN.

float: (text) => Maybe<number>

Parses a string as a floating-point number. Returns None if the result is NaN.

string

Maybe<number>

int for parsing base-10 integers.

Str.parse.float("3.14"); // Some(3.14)
Str.parse.float("42");   // Some(42)
Str.parse.float("abc");  // None

int: (text) => Maybe<number>

Parses a string as an integer (base 10). Returns None if the result is NaN.

string

Maybe<number>

float for parsing floating-point numbers.

Str.parse.int("42");   // Some(42)
Str.parse.int("3.7");  // Some(3)
Str.parse.int("abc");  // None

replace: (pattern, replacement) => (text) => string

Replaces the first occurrence of a pattern in a string. Data-last: use in pipe.

string | RegExp

string

(text) => string

replaceAll for substituting every occurrence instead of only the first.

pipe("foo foo foo", Str.replace("foo", "bar")); // "bar foo foo"
pipe("Hello World", Str.replace(/world/i, "Earth")); // "Hello Earth"

replaceAll: (pattern, replacement) => (text) => string

Replaces all occurrences of a pattern in a string. Data-last: use in pipe.

string | RegExp

string

(text) => string

replace for substituting only the first occurrence.

pipe("foo foo foo", Str.replaceAll("foo", "bar")); // "bar bar bar"
pipe("aAbBaA", Str.replaceAll(/a/gi, "x")); // "xxBBxx"

size: (text) => number

Returns the UTF-16 code-unit length of the string.

string

number

graphemeSize for counting user-perceived characters/emojis.

pipe("hello", Str.size); // 5
pipe("", Str.size);      // 0

slice: (start, end?) => (text) => string

Extracts a substring between two indices. Data-last: use in pipe.

number

number

(text) => string

pipe("hello", Str.slice(1, 3)); // "el"
pipe("hello", Str.slice(2));    // "llo"

split: (separator) => (text) => readonly string[]

Splits a string by a separator. Data-last: use in pipe.

string | RegExp

(text) => readonly string[]

pipe("a,b,c", Str.split(",")); // ["a", "b", "c"]

startsWith: (prefix) => (text) => boolean

Returns true when the string starts with the given prefix.

string

(text) => boolean

endsWith for checking suffix matches.

pipe("hello world", Str.startsWith("hello")); // true
pipe("hello world", Str.startsWith("world")); // false

test: (pattern) => (text) => boolean

Tests whether a string matches a regular expression. Pure and safe: resets pattern.lastIndex = 0 to prevent bugs with stateful /g and /y regexes.

RegExp

(text) => boolean

match for extracting capture groups into a Maybe.

pipe("user@example.com", Str.test(/^[^@]+@[^@]+$/)); // true
pipe("invalid-email", Str.test(/^[^@]+@[^@]+$/));    // false

toLowerCase: (text) => string

Converts a string to lowercase.

string

string

toUpperCase for uppercasing characters.

pipe("HELLO", Str.toLowerCase); // "hello"

toUpperCase: (text) => string

Converts a string to uppercase.

string

string

toLowerCase for lowercasing characters.

pipe("hello", Str.toUpperCase); // "HELLO"

trim: (text) => string

Removes leading and trailing whitespace from a string.

string

string

pipe("  hello  ", Str.trim); // "hello"

truncate: (options) => (text) => string

Truncates a string to a maximum length, appending an optional suffix (default "..."). Data-last curried signature.

number

string

(text) => string

pipe("Hello, world!", Str.truncate({ length: 8 })); // "Hello..."
pipe("Hello", Str.truncate({ length: 10 }));        // "Hello"
pipe("Hello, world!", Str.truncate({ length: 8, suffix: "…" })); // "Hello, w…"

uncapitalize: (text) => string

Converts the first character of a string to lower case.

string

string

capitalize for converting the first character to uppercase.

pipe("Hello", Str.uncapitalize); // "hello"
pipe("", Str.uncapitalize);      // ""

words: (text) => readonly string[]

Splits a string into words on any whitespace boundary, filtering out empty strings.

string

readonly string[]

Str.words("  hello   world  "); // ["hello", "world"]