Skip to main content

String

A collection of functions to manipulate string

Index

Accessor

at

  • Return the character at the index position

    @example
    const string = 'bar';
    String.at(string, 1) // Option.Some('a')
    String.at(string, -1) // Option.Some('r') i.e. the last
    String.at(string, 99) // Option.None

size

  • size(self: string): Int
  • Return the length of the string

    @example
    String.size('') // 0
    String.size('foo bar') // 6

Codec

__decode__

  • Returns the decoded input, Result.Ok or Result.Error()

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const input: unknown = ...;
    const decoded = Codec.decode(someCodec, input);

__encode__

  • __encode__(this: void, input: string): unknown
  • Returns the encoded input

    @example
    interface 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

    @example
    const someCodec: Codec<unknown> = ...;
    const jsonSchema = Codec.schema(someCodec);

Combinator

concat

  • concat(parts: string[]): string
  • Joins the given array of strings.

    @example
    String.concat(['a', 'b', 'c']) // 'abc'

join

  • join(separator: string, parts: string[]): string
  • Joins the given array of strings.

    @example
    String.join('|', ['a', 'b', 'c']) // 'a|b|c'

Comparator

!=

  • !=(this: void, left: string, right: string): boolean
  • "Not equal to" operator

    @example
    const TEqual: Equal<T>;
    TEqual['!='](value, otherValue); // true
    TEqual['!='](value, value); // false

  • (this: void, left: string, right: string): boolean
  • "Less than" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<'](smallerT, smallerT); // false
    TCompare['<'](smallerT, greaterT); // true
    TCompare['<'](greaterT, smallerT); // false

<=

  • <=(this: void, left: string, right: string): boolean
  • "Less than or equal to" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<='](smallerT, smallerT); // true
    TCompare['<='](smallerT, greaterT); // true
    TCompare['<='](greaterT, smallerT); // false

==

  • ==(this: void, left: string, right: string): boolean
  • "Equal to" operator

    @example
    type T = // ...
    const TEqual: Equal<T>;
    TEqual['=='](value, value); // true
    TEqual['=='](value, otherValue); // false

  • (this: void, left: string, right: string): boolean
  • "Greater than" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>'](smallerT, smallerT); // false
    TCompare['>'](smallerT, greaterT); // false
    TCompare['>'](greaterT, smallerT); // true

>=

  • >=(this: void, left: string, right: string): boolean
  • "Greater than or equal to" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>='](smallerT, smallerT); // true
    TCompare['>='](smallerT, greaterT); // false
    TCompare['>='](greaterT, smallerT); // true

clamp

  • clamp(this: void, value: string, minValue: string, maxValue: string): string
  • Clamp value between minValue and maxValue

    @example
    type T;
    const TCompare: Comparable<T>;
    TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself

compare

  • compare(this: void, left: string, right: string): Ordering
  • Return an Ordering that represents comparison result

    @see
    @example
    type T;
    const TCompare: Comparable<T>;
    const sorted = [3, 1, 1].sort(TCompare.compare);

equals

  • equals(this: void, left: string, right: string): boolean
  • Alias to '=='

    @example
    type T = // ...
    const TEqual: Equal<T>;
    TEqual.equals(value, value); // true
    TEqual.equals(value, otherValue); // false

max

  • max(this: void, left: string, right: string): string
  • "maximum" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.max(smallerT, greaterT); // greaterT

min

  • min(this: void, left: string, right: string): string
  • "minimum" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.min(smallerT, greaterT); // smallerT

Constructor

of

  • of(...args: string[]): string
  • Return a new string from all parts passed as arguments

    @example
    String.of('a', 'b', 'c') // 'abc'

Formatting

asString

  • asString(this: void, self: string): string
  • Converts the given value to a String.

    @example
    Number.asString(123); // '123'

Predicate

endsWith

  • endsWith(self: string, searchString: string): boolean
  • Returns true if string ends with searchString

    @example
    String.endsWith('abc', 'bc'); // true
    String.endsWith('abc', 'ab'); // false

includes

  • includes(self: string, searchString: string): boolean
  • Returns the index of the last occurrence of searchString in a string.

    @example
    String.includes('abc', 'ab'); // true
    String.includes('abc', 'absent'); // false

isEmpty

  • isEmpty(self: string): boolean
  • Return true if the size of the array is 0

    @example
    String.isEmpty('');// true
    String.isEmpty('abc');// false

startsWith

  • startsWith(self: string, searchString: string): boolean
  • Returns true if string starts with searchString

    @example
    String.startsWith('abc', 'ab'); // true
    String.startsWith('abc', 'bc'); // false

Type

__inspect__

__inspect__: Option<(anyValue: string, depth: number, options: InspectOptions, inspect: InspectFunction) => string>

When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)

@example
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 }'
@param

asInstance

  • asInstance(anyValue: unknown): Option<string>
  • Try to convert anyValue to enum value or else returns Option.None

    @example
    const StringType: Type<string>;
    StringType.asInstance('foo'); // Option.Some('foo')
    StringType.asInstance(12); // Option.None

hasInstance

  • hasInstance(anyValue: unknown): anyValue is string
  • Return true if the given value is an instance of the class.

    @example
    const StringType: Type<string>;
    StringType.hasInstance('foo'); // true
    StringType.hasInstance(42); // false

Other

typeName

Re-exports typeName

indexOf

  • indexOf(self: string, searchString: string, fromIndex?: number): Option<Int>
  • Returns the index of the first occurrence of searchString in a string.

    @example
    String.indexOf('aa', 'a'); // Option.Some(0)
    String.indexOf('aaa', 'a', 1); // Option.Some(1)
    String.indexOf('ab', 'absent'); // Option.None

lastIndexOf

  • lastIndexOf(self: string, searchString: string, fromIndex?: number): Option<Int>
  • Returns the index of the last occurrence of searchString in a string.

    @example
    String.lastIndexOf('aa', 'a'); // Option.Some(1)
    String.lastIndexOf('aaa', 'a', 1); // Option.Some(1)
    String.lastIndexOf('ab', 'absent'); // Option.None

padEnd

  • padEnd(self: string, maxLength: number, fillString?: string): string
  • 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.

    @example
    String.padEnd('abc', 9, '1234'); // 'abcd1234'

padStart

  • padStart(self: string, maxLength: number, fillString?: string): string
  • 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.

    @example
    String.padStart('abc', 9, '1234'); // '1234abcd'

split

  • split(self: string, separator: string | RegExp | { [split]: any }, limit?: number): string[]
  • Split a string into substrings using the specified separator and return them as an array.

    @example
    String.split('a|b|c', '|'); // ['a', 'b', 'c']
    String.split('a|b|c', '|', 2); // ['a', 'b']

truncate

  • truncate(self: string, options?: Options): string
  • Truncates a string to a specified length, adding '...' if necessary.

    @example
    truncate('Hello World', { maxLength: 1 }); // 'Hello World'
    truncate('Hello World', { maxLength: 5 }); // 'He...'
    truncate('Hello World', { maxLength: 5, ellipsis: '[...]' }); // 'He[...]'

defaultOptions

defaultOptions: { ellipsis: string; maxLength: Int } = ...