Skip to content

Str

const Str: object

Defined in: Data/Str.ts:43

capitalize: (s) => string

Converts the first character of a string to uppercase.

string

string

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

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

Returns true when the string ends with the given suffix.

string

(s) => boolean

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

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

Returns true when the string contains the given substring.

string

(s) => boolean

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

is: object

empty: (s) => boolean = isEmpty

Returns true when the string is empty.

string

boolean

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

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

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

string

s is NonEmptyString

isBlank: (s) => boolean

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

string

boolean

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

length: (s) => number

Returns the length of the string.

string

number

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

lines: (s) => 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"]

NonEmpty: object = StrNonEmptyConst

from: object

String: (s) => 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?) => (s) => string

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

number

string

(s) => string

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

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

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

number

string

(s) => 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: (s) => Maybe<number>

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

string

Maybe<number>

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

int: (s) => Maybe<number>

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

string

Maybe<number>

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

parseJson: (s) => Result<SyntaxError, unknown>

Safely parses a JSON string, returning a Result<SyntaxError, unknown>.

string

Result<SyntaxError, unknown>

Str.parseJson('{"a": 1}'); // Ok({ a: 1 })
Str.parseJson('invalid');  // Err(SyntaxError)

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

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

string | RegExp

string

(s) => string

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

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

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

string | RegExp

string

(s) => string

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

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

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

number

number

(s) => string

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

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

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

string | RegExp

(s) => readonly string[]

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

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

Returns true when the string starts with the given prefix.

string

(s) => boolean

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

toLowerCase: (s) => string

Converts a string to lowercase.

string

string

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

toUpperCase: (s) => string

Converts a string to uppercase.

string

string

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

trim: (s) => string

Removes leading and trailing whitespace from a string.

string

string

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

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

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

number

string

(s) => 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: (s) => string

Converts the first character of a string to lower case.

string

string

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

words: (s) => readonly string[]

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

string

readonly string[]

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