String.ts overview
This module provides utility functions and type class instances for working with the string
type in TypeScript. It includes functions for basic string manipulation, as well as type class instances for Equivalence
and Order
.
Since v2.0.0
Exports Grouped by Category
- guards
- instances
- utils
- Concat (type alias)
- Trim (type alias)
- TrimEnd (type alias)
- TrimStart (type alias)
- at
- camelToSnake
- capitalize
- charAt
- charCodeAt
- codePointAt
- concat
- empty
- endsWith
- includes
- indexOf
- isEmpty
- isNonEmpty
- kebabToSnake
- lastIndexOf
- length
- linesIterator
- linesWithSeparators
- localeCompare
- match
- matchAll
- normalize
- padEnd
- padStart
- pascalToSnake
- repeat
- replace
- replaceAll
- search
- slice
- snakeToCamel
- snakeToKebab
- snakeToPascal
- split
- startsWith
- stripMargin
- stripMarginWith
- substring
- takeLeft
- takeRight
- toLocaleLowerCase
- toLocaleUpperCase
- toLowerCase
- toUpperCase
- trim
- trimEnd
- trimStart
- uncapitalize
guards
isString
Tests if a value is a string
.
Example
import * as assert from "node:assert"
import { String } from "effect"
assert.deepStrictEqual(String.isString("a"), true)
assert.deepStrictEqual(String.isString(1), false)
Signature
declare const isString: Refinement<unknown, string>
Since v2.0.0
instances
Equivalence
Signature
declare const Equivalence: equivalence.Equivalence<string>
Since v2.0.0
Order
Signature
declare const Order: order.Order<string>
Since v2.0.0
utils
Concat (type alias)
Concatenates two strings at the type level.
Signature
type`${A}${B}` = `${A}${B}`
Since v2.0.0
Trim (type alias)
Signature
type Trim<A> = TrimEnd<TrimStart<A>>
Since v2.0.0
TrimEnd (type alias)
Signature
type TrimEnd<A> = A extends `${infer B}${" " | "\n" | "\t" | "\r"}` ? TrimEnd<B> : A
Since v2.0.0
TrimStart (type alias)
Signature
type TrimStart<A> = A extends `${" " | "\n" | "\t" | "\r"}${infer B}` ? TrimStart<B> : A
Since v2.0.0
at
Example
import * as assert from "node:assert"
import { pipe, String, Option } from "effect"
assert.deepStrictEqual(pipe("abc", String.at(1)), Option.some("b"))
assert.deepStrictEqual(pipe("abc", String.at(4)), Option.none())
Signature
declare const at: {
(index: number): (self: string) => Option.Option<string>
(self: string, index: number): Option.Option<string>
}
Since v2.0.0
camelToSnake
Signature
declare const camelToSnake: (self: string) => string
Since v2.0.0
capitalize
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("abc", String.capitalize), "Abc")
Signature
declare const capitalize: <T extends string>(self: T) => Capitalize<T>
Since v2.0.0
charAt
Example
import * as assert from "node:assert"
import { pipe, String, Option } from "effect"
assert.deepStrictEqual(pipe("abc", String.charAt(1)), Option.some("b"))
assert.deepStrictEqual(pipe("abc", String.charAt(4)), Option.none())
Signature
declare const charAt: {
(index: number): (self: string) => Option.Option<string>
(self: string, index: number): Option.Option<string>
}
Since v2.0.0
charCodeAt
Example
import * as assert from "node:assert"
import { pipe, String, Option } from "effect"
assert.deepStrictEqual(pipe("abc", String.charCodeAt(1)), Option.some(98))
assert.deepStrictEqual(pipe("abc", String.charCodeAt(4)), Option.none())
Signature
declare const charCodeAt: {
(index: number): (self: string) => Option.Option<number>
(self: string, index: number): Option.Option<number>
}
Since v2.0.0
codePointAt
Example
import * as assert from "node:assert"
import { pipe, String, Option } from "effect"
assert.deepStrictEqual(pipe("abc", String.codePointAt(1)), Option.some(98))
Signature
declare const codePointAt: {
(index: number): (self: string) => Option.Option<number>
(self: string, index: number): Option.Option<number>
}
Since v2.0.0
concat
Concatenates two strings at runtime.
Signature
declare const concat: {
<B extends string>(that: B): <A extends string>(self: A) => Concat<A, B>
<A extends string, B extends string>(self: A, that: B): Concat<A, B>
}
Since v2.0.0
empty
The empty string ""
.
Signature
declare const empty: ""
Since v2.0.0
endsWith
Signature
declare const endsWith: (searchString: string, position?: number) => (self: string) => boolean
Since v2.0.0
includes
Returns true
if searchString
appears as a substring of self
, at one or more positions that are greater than or equal to position
; otherwise, returns false
.
Signature
declare const includes: (searchString: string, position?: number) => (self: string) => boolean
Since v2.0.0
indexOf
Example
import * as assert from "node:assert"
import { pipe, String, Option } from "effect"
assert.deepStrictEqual(pipe("abbbc", String.indexOf("b")), Option.some(1))
Signature
declare const indexOf: (searchString: string) => (self: string) => Option.Option<number>
Since v2.0.0
isEmpty
Test whether a string
is empty.
Example
import * as assert from "node:assert"
import { String } from "effect"
assert.deepStrictEqual(String.isEmpty(""), true)
assert.deepStrictEqual(String.isEmpty("a"), false)
Signature
declare const isEmpty: (self: string) => self is ""
Since v2.0.0
isNonEmpty
Test whether a string
is non empty.
Signature
declare const isNonEmpty: (self: string) => boolean
Since v2.0.0
kebabToSnake
Signature
declare const kebabToSnake: (self: string) => string
Since v2.0.0
lastIndexOf
Example
import * as assert from "node:assert"
import { pipe, String, Option } from "effect"
assert.deepStrictEqual(pipe("abbbc", String.lastIndexOf("b")), Option.some(3))
assert.deepStrictEqual(pipe("abbbc", String.lastIndexOf("d")), Option.none())
Signature
declare const lastIndexOf: (searchString: string) => (self: string) => Option.Option<number>
Since v2.0.0
length
Calculate the number of characters in a string
.
Example
import * as assert from "node:assert"
import { String } from "effect"
assert.deepStrictEqual(String.length("abc"), 3)
Signature
declare const length: (self: string) => number
Since v2.0.0
linesIterator
Returns an IterableIterator
which yields each line contained within the string, trimming off the trailing newline character.
Signature
declare const linesIterator: (self: string) => LinesIterator
Since v2.0.0
linesWithSeparators
Returns an IterableIterator
which yields each line contained within the string as well as the trailing newline character.
Signature
declare const linesWithSeparators: (s: string) => LinesIterator
Since v2.0.0
localeCompare
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("a", String.localeCompare("b")), -1)
assert.deepStrictEqual(pipe("b", String.localeCompare("a")), 1)
assert.deepStrictEqual(pipe("a", String.localeCompare("a")), 0)
Signature
declare const localeCompare: (
that: string,
locales?: Intl.LocalesArgument,
options?: Intl.CollatorOptions
) => (self: string) => Ordering.Ordering
Since v2.0.0
match
It is the pipe
-able version of the native match
method.
Signature
declare const match: (regexp: RegExp | string) => (self: string) => Option.Option<RegExpMatchArray>
Since v2.0.0
matchAll
It is the pipe
-able version of the native matchAll
method.
Signature
declare const matchAll: (regexp: RegExp) => (self: string) => IterableIterator<RegExpMatchArray>
Since v2.0.0
normalize
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
const str = "\u1E9B\u0323"
assert.deepStrictEqual(pipe(str, String.normalize()), "\u1E9B\u0323")
assert.deepStrictEqual(pipe(str, String.normalize("NFC")), "\u1E9B\u0323")
assert.deepStrictEqual(pipe(str, String.normalize("NFD")), "\u017F\u0323\u0307")
assert.deepStrictEqual(pipe(str, String.normalize("NFKC")), "\u1E69")
assert.deepStrictEqual(pipe(str, String.normalize("NFKD")), "\u0073\u0323\u0307")
Signature
declare const normalize: (form?: "NFC" | "NFD" | "NFKC" | "NFKD") => (self: string) => string
Since v2.0.0
padEnd
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("a", String.padEnd(5)), "a ")
assert.deepStrictEqual(pipe("a", String.padEnd(5, "_")), "a____")
Signature
declare const padEnd: (maxLength: number, fillString?: string) => (self: string) => string
Since v2.0.0
padStart
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("a", String.padStart(5)), " a")
assert.deepStrictEqual(pipe("a", String.padStart(5, "_")), "____a")
Signature
declare const padStart: (maxLength: number, fillString?: string) => (self: string) => string
Since v2.0.0
pascalToSnake
Signature
declare const pascalToSnake: (self: string) => string
Since v2.0.0
repeat
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("a", String.repeat(5)), "aaaaa")
Signature
declare const repeat: (count: number) => (self: string) => string
Since v2.0.0
replace
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("abc", String.replace("b", "d")), "adc")
Signature
declare const replace: (searchValue: string | RegExp, replaceValue: string) => (self: string) => string
Since v2.0.0
replaceAll
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("ababb", String.replaceAll("b", "c")), "acacc")
assert.deepStrictEqual(pipe("ababb", String.replaceAll(/ba/g, "cc")), "accbb")
Signature
declare const replaceAll: (searchValue: string | RegExp, replaceValue: string) => (self: string) => string
Since v2.0.0
search
Example
import * as assert from "node:assert"
import { pipe, String, Option } from "effect"
assert.deepStrictEqual(pipe("ababb", String.search("b")), Option.some(1))
assert.deepStrictEqual(pipe("ababb", String.search(/abb/)), Option.some(2))
assert.deepStrictEqual(pipe("ababb", String.search("d")), Option.none())
Signature
declare const search: {
(regexp: RegExp | string): (self: string) => Option.Option<number>
(self: string, regexp: RegExp | string): Option.Option<number>
}
Since v2.0.0
slice
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("abcd", String.slice(1, 3)), "bc")
Signature
declare const slice: (start?: number, end?: number) => (self: string) => string
Since v2.0.0
snakeToCamel
Signature
declare const snakeToCamel: (self: string) => string
Since v2.0.0
snakeToKebab
Signature
declare const snakeToKebab: (self: string) => string
Since v2.0.0
snakeToPascal
Signature
declare const snakeToPascal: (self: string) => string
Since v2.0.0
split
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("abc", String.split("")), ["a", "b", "c"])
assert.deepStrictEqual(pipe("", String.split("")), [""])
Signature
declare const split: {
(separator: string | RegExp): (self: string) => NonEmptyArray<string>
(self: string, separator: string | RegExp): NonEmptyArray<string>
}
Since v2.0.0
startsWith
Signature
declare const startsWith: (searchString: string, position?: number) => (self: string) => boolean
Since v2.0.0
stripMargin
For every line in this string, strip a leading prefix consisting of blanks or control characters followed by the "|"
character from the line.
Signature
declare const stripMargin: (self: string) => string
Since v2.0.0
stripMarginWith
For every line in this string, strip a leading prefix consisting of blanks or control characters followed by the character specified by marginChar
from the line.
Signature
declare const stripMarginWith: {
(marginChar: string): (self: string) => string
(self: string, marginChar: string): string
}
Since v2.0.0
substring
Example
import * as assert from "node:assert"
import { pipe, String, Option } from "effect"
assert.deepStrictEqual(pipe("abcd", String.substring(1)), "bcd")
assert.deepStrictEqual(pipe("abcd", String.substring(1, 3)), "bc")
Signature
declare const substring: (start: number, end?: number) => (self: string) => string
Since v2.0.0
takeLeft
Keep the specified number of characters from the start of a string.
If n
is larger than the available number of characters, the string will be returned whole.
If n
is not a positive number, an empty string will be returned.
If n
is a float, it will be rounded down to the nearest integer.
Example
import * as assert from "node:assert"
import { String } from "effect"
assert.deepStrictEqual(String.takeLeft("Hello World", 5), "Hello")
Signature
declare const takeLeft: { (n: number): (self: string) => string; (self: string, n: number): string }
Since v2.0.0
takeRight
Keep the specified number of characters from the end of a string.
If n
is larger than the available number of characters, the string will be returned whole.
If n
is not a positive number, an empty string will be returned.
If n
is a float, it will be rounded down to the nearest integer.
Example
import * as assert from "node:assert"
import { String } from "effect"
assert.deepStrictEqual(String.takeRight("Hello World", 5), "World")
Signature
declare const takeRight: { (n: number): (self: string) => string; (self: string, n: number): string }
Since v2.0.0
toLocaleLowerCase
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
const str = "\u0130"
assert.deepStrictEqual(pipe(str, String.toLocaleLowerCase("tr")), "i")
Signature
declare const toLocaleLowerCase: (locale?: Intl.LocalesArgument) => (self: string) => string
Since v2.0.0
toLocaleUpperCase
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
const str = "i\u0307"
assert.deepStrictEqual(pipe(str, String.toLocaleUpperCase("lt-LT")), "I")
Signature
declare const toLocaleUpperCase: (locale?: Intl.LocalesArgument) => (self: string) => string
Since v2.0.0
toLowerCase
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("A", String.toLowerCase), "a")
Signature
declare const toLowerCase: <T extends string>(self: T) => Lowercase<T>
Since v2.0.0
toUpperCase
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("a", String.toUpperCase), "A")
Signature
declare const toUpperCase: <S extends string>(self: S) => Uppercase<S>
Since v2.0.0
trim
Example
import * as assert from "node:assert"
import { String } from "effect"
assert.deepStrictEqual(String.trim(" a "), "a")
Signature
declare const trim: <A extends string>(self: A) => Trim<A>
Since v2.0.0
trimEnd
Example
import * as assert from "node:assert"
import { String } from "effect"
assert.deepStrictEqual(String.trimEnd(" a "), " a")
Signature
declare const trimEnd: <A extends string>(self: A) => TrimEnd<A>
Since v2.0.0
trimStart
Example
import * as assert from "node:assert"
import { String } from "effect"
assert.deepStrictEqual(String.trimStart(" a "), "a ")
Signature
declare const trimStart: <A extends string>(self: A) => TrimStart<A>
Since v2.0.0
uncapitalize
Example
import * as assert from "node:assert"
import { pipe, String } from "effect"
assert.deepStrictEqual(pipe("ABC", String.uncapitalize), "aBC")
Signature
declare const uncapitalize: <T extends string>(self: T) => Uncapitalize<T>
Since v2.0.0