Aller au contenu principal

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(string: string): Int
  • Return the length of the string

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

Codec

codecDecode

  • 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);

codecEncode

  • codecEncode(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);

codecSchema

  • 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): number
  • Return a number that represents comparison

    @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

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

of

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

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

Predicate

endsWith

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

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

includes

  • includes(string: 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(string: string): boolean
  • Return true if the size of the array is 0

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

startsWith

  • startsWith(string: 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

@example

@param

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(string: 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(string: 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

split

  • split(string: 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']