String
Index
Accessor
Codec
Combinator
Comparator
Constructor
Formatting
Predicate
Type
Other
Accessor
at
Return the character at the
indexpositionconst string = 'bar';String.at(string, 1) // Option.Some('a')String.at(string, -1) // Option.Some('r') i.e. the lastString.at(string, 99) // Option.None
size
Return the length of the string
String.size('') // 0String.size('foo bar') // 6
Codec
__decode__
Returns the decoded
input,Result.OkorResult.Error()interface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const input: unknown = ...;const decoded = Codec.decode(someCodec, input);
__encode__
Returns the encoded
inputinterface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const someObject: SomeObject = { foo: "bar" }const encoded = Codec.decode(someCodec, someObject);
__schema__
Returns the JSONSchema corresponding to the decoded type
const someCodec: Codec<unknown> = ...;const jsonSchema = Codec.schema(someCodec);
Combinator
concat
Joins the given array of strings.
String.concat(['a', 'b', 'c']) // 'abc'
join
Joins the given array of strings.
String.join('|', ['a', 'b', 'c']) // 'a|b|c'
Comparator
!=
"Not equal to" operator
const TEqual: Equal<T>;TEqual['!='](value, otherValue); // trueTEqual['!='](value, value); // false
<
"Less than" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare['<'](smallerT, smallerT); // falseTCompare['<'](smallerT, greaterT); // trueTCompare['<'](greaterT, smallerT); // false
<=
"Less than or equal to" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare['<='](smallerT, smallerT); // trueTCompare['<='](smallerT, greaterT); // trueTCompare['<='](greaterT, smallerT); // false
==
"Equal to" operator
type T = // ...const TEqual: Equal<T>;TEqual['=='](value, value); // trueTEqual['=='](value, otherValue); // false
>
"Greater than" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare['>'](smallerT, smallerT); // falseTCompare['>'](smallerT, greaterT); // falseTCompare['>'](greaterT, smallerT); // true
>=
"Greater than or equal to" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare['>='](smallerT, smallerT); // trueTCompare['>='](smallerT, greaterT); // falseTCompare['>='](greaterT, smallerT); // true
clamp
Clamp value between minValue and maxValue
type T;const TCompare: Comparable<T>;TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself
compare
equals
Alias to '=='
type T = // ...const TEqual: Equal<T>;TEqual.equals(value, value); // trueTEqual.equals(value, otherValue); // false
max
"maximum" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare.max(smallerT, greaterT); // greaterT
min
"minimum" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare.min(smallerT, greaterT); // smallerT
Constructor
of
Return a new string from all parts passed as arguments
String.of('a', 'b', 'c') // 'abc'
Formatting
asString
Converts the given value to a String.
Number.asString(123); // '123'
Predicate
endsWith
Returns true if string ends with searchString
String.endsWith('abc', 'bc'); // trueString.endsWith('abc', 'ab'); // false
includes
Returns the index of the last occurrence of
searchStringin a string.String.includes('abc', 'ab'); // trueString.includes('abc', 'absent'); // false
isEmpty
Return true if the size of the array is 0
String.isEmpty('');// trueString.isEmpty('abc');// false
startsWith
Returns true if string starts with searchString
String.startsWith('abc', 'ab'); // trueString.startsWith('abc', 'bc'); // false
Type
__inspect__
When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)
import { inspect } from 'node:util';
interface Foo {
foo: boolean;
}
const Foo = Struct.define<Foo>({
typeName: 'Foo',
__inspect__: (self) => `Foo { ${String(self.foo)} }`,
});
const myStruct = Struct.create(Foo, { foo: true });// 'Foo { true }'
inspect(myStruct);// 'Foo { true }'
asInstance
Try to convert anyValue to enum value or else returns
Option.Noneconst StringType: Type<string>;StringType.asInstance('foo'); // Option.Some('foo')StringType.asInstance(12); // Option.None
hasInstance
Return
trueif the given value is an instance of the class.const StringType: Type<string>;StringType.hasInstance('foo'); // trueStringType.hasInstance(42); // false
Other
typeName
indexOf
Returns the index of the first occurrence of
searchStringin a string.String.indexOf('aa', 'a'); // Option.Some(0)String.indexOf('aaa', 'a', 1); // Option.Some(1)String.indexOf('ab', 'absent'); // Option.None
lastIndexOf
Returns the index of the last occurrence of
searchStringin a string.String.lastIndexOf('aa', 'a'); // Option.Some(1)String.lastIndexOf('aaa', 'a', 1); // Option.Some(1)String.lastIndexOf('ab', 'absent'); // Option.None
padEnd
Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
String.padEnd('abc', 9, '1234'); // 'abcd1234'
padStart
Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the start (left) of the current string.
String.padStart('abc', 9, '1234'); // '1234abcd'
split
Split a string into substrings using the specified separator and return them as an array.
String.split('a|b|c', '|'); // ['a', 'b', 'c']String.split('a|b|c', '|', 2); // ['a', 'b']
truncate
Truncates a string to a specified length, adding '...' if necessary.
truncate('Hello World', { maxLength: 1 }); // 'Hello World'truncate('Hello World', { maxLength: 5 }); // 'He...'truncate('Hello World', { maxLength: 5, ellipsis: '[...]' }); // 'He[...]'
A collection of functions to manipulate
string