Skip to main content Link Search Menu Expand Document (external link)

String overview

A simple extension to the Effect String module


Table of contents


Constructors

fromNonNullablePrimitive

Builds a string from a primitive value other than null and undefined. For numbers and bigints, base-10 conversion is assumed. We don’t use the .toString() method for numbers because it uses scientific notation for certain numbers

Signature

export declare const fromNonNullablePrimitive: (u: MTypes.NonNullablePrimitive) => string

fromNumber

Builds a string from a number using the passed radix

Signature

export declare const fromNumber: (radix?: number) => MTypes.NumberToString

fromPrimitive

Builds a string from a primitive value. null is converted to the string “null” and undefined to the string “undefined”. For numbers and bigints, base-10 conversion is assumed.

Signature

export declare const fromPrimitive: MTypes.OneArgFunction<MTypes.Primitive, string>

fromUnknown

Builds a string from an unknown value u. Calls fromPrimitive if u is a primitive value. Calls JSON.stringify otherwise.

Signature

export declare const fromUnknown: (u: unknown) => string

Destructors

capturedGroups

Same as matchAndGroups but returns only the captured groups.

Signature

export declare const capturedGroups: <N extends number>(
  regExp: RegExp,
  capturingGroupNumber: N
) => (self: string) => Option.Option<MTypes.Tuple<string, N>>

matchAndGroups

Same as match but also returns capturing groups.

Signature

export declare const matchAndGroups: <N extends number>(
  regExp: RegExp,
  capturingGroupNumber: N
) => (self: string) => Option.Option<[match: string, capturingGroups: MTypes.Tuple<string, N>]>

Models

FillPosition (namespace)

FillPosition namespace

SearchResult (namespace)

This namespace implements a type that represents the result of the search of a string in another string.

Type (interface)

Interface that represents a SearchResult

Signature

export interface Type extends Equal.Equal, Inspectable.Inspectable, Pipeable.Pipeable {
  /** The index where the match was found in the target string */
  readonly startIndex: number
  /** The index of the character following the match in the target string */
  readonly endIndex: number
  /** The match */
  readonly match: string
  /** @internal */
  readonly [_TypeId]: _TypeId
}

moduleTag

Module tag

Signature

export declare const moduleTag: "@parischap/effect-lib/String/"

Predicates

hasLength

Returns true if the length of self is l

Signature

export declare const hasLength: (l: number) => Predicate.Predicate<string>

isDigit

Returns true if a string represents a digit. False otherwise

Signature

export declare const isDigit: Predicate.Predicate<string>

isEmail

Returns true if self is an email

Signature

export declare const isEmail: Predicate.Predicate<string>

isSemVer

Returns true if self is a SemVer

Signature

export declare const isSemVer: Predicate.Predicate<string>

Utils

append

Appends s to self

Signature

export declare const append: (s: string) => MTypes.StringTransformer

count

Returns the number of occurences of regexp in self

Signature

export declare const count: (regexp: RegExp | string) => MTypes.NumberFromString

isMultiLine

Returns true if self contains at least an eol character

Signature

export declare const isMultiLine: Predicate.Predicate<string>

match

A slightly different version of match using RegExp.prototype.exec instead of String.prototype.match. This function will always return only the first match, even if the g flag is set. Good to use in a library when you have no control over the RegExp you receive.

Signature

export declare const match: (regExp: RegExp) => (self: string) => Option.Option<string>

matches

Returns true if self fulfills regExp. false otherwise. Does the same as RegExp.prototype.test but does not take the g flag into account even if it is set and so does not care for the lastIndex property of regExp

Signature

export declare const matches: (regExp: RegExp) => Predicate.Predicate<string>

pad

Pads a string to the left or to the right (depending on fillPosition) with up to length characters fillChar. length should be a positive integer. fillChar should be a one-character string. Does nothing if the string to pad has more than length characters.

Signature

export declare const pad: ({
  length,
  fillChar,
  fillPosition
}: {
  readonly length: number
  readonly fillChar: string
  readonly fillPosition: FillPosition
}) => MTypes.OneArgFunction<string>

prepend

Prepends s to self

Signature

export declare const prepend: (s: string) => MTypes.StringTransformer

removeNCharsEveryMCharsFromRight

Function that removes n chars every m chars starting from the right

Signature

export declare const removeNCharsEveryMCharsFromRight: ({
  m,
  n
}: {
  readonly m: number
  readonly n: number
}) => MTypes.StringTransformer

replaceBetween

Replaces the part of self between startIndex included and endIndex excluded by replacement. If startIndex equals endIndex, replacement is inserted at startIndex. If startIndex is strictly greater than endIndex, the part between endIndex and startIndex will be present before and after the replacement. If startIndex is strictly less than 0, it is taken equal to 0. Same for endIndex. If startIndex or strisctly superior to the length of self, it is taken equal to the length of self. Same

Signature

export declare const replaceBetween: (
  replacement: string,
  startIndex: number,
  endIndex: number
) => MTypes.StringTransformer

Searches for the first occurence of regexp in self and returns a SearchResult. You can optionnally provide the index from which to start searching. ‘g’ flag needs not be set if you pass a regular expression. As opposed to String.search, regexp special characters need not be escaped when passing a string regexp

Signature

export declare const search: (
  regexp: RegExp | string,
  startIndex?: number
) => (self: string) => Option.Option<SearchResult.Type>

searchAll

Searches for all occurences of regexp in self and returns an array of SearchResults. ‘g’ flag needs not be set if you pass a regular expression.

Signature

export declare const searchAll: (regexp: RegExp | string) => (self: string) => Array<SearchResult.Type>

searchRight

Searches for the last occurence of regexp in self and returns a SearchResult. ‘g’ flag needs not be set if you pass a regular expression.

Signature

export declare const searchRight: (regexp: RegExp | string) => (self: string) => Option.Option<SearchResult.Type>

splitAt

Splits self in two parts at position n. The length of the first string is n (characters 0 to n-1). If n is strictly less than 0, it is taken equal to 0. If n is greater than the length of self, it is taken equal to the length of self.

Signature

export declare const splitAt: (n: number) => (self: string) => [left: string, right: string]

splitAtFromRight

Splits self in two parts at position n from the end of self. The length of the second string is n. If n is strictly less than 0, it is taken equal to 0. If n is greater than the length of self, it is taken equal to the length of self.

Signature

export declare const splitAtFromRight: (n: number) => (self: string) => [left: string, right: string]

splitEquallyRestAtEnd

Splits self in substrings of bitSize characters. The length of the last string, if any, is comprised between 1 and bitSize characters. bitSize must be a strictly positive integer.

Signature

export declare const splitEquallyRestAtEnd: (bitSize: number) => MTypes.OneArgFunction<string, MTypes.OverOne<string>>

splitEquallyRestAtStart

Splits self in substrings of bitSize characters. The length of the first string, if any, is comprised between 1 and bitSize characters. bitSize must be a strictly positive integer.

Signature

export declare const splitEquallyRestAtStart: (bitSize: number) => MTypes.OneArgFunction<string, Array<string>>

stripLeft

If self starts with s, returns self stripped of s. Otherwise, returns self

Signature

export declare const stripLeft: (s: string) => MTypes.StringTransformer

stripLeftOption

If self starts with s, returns a some of self stripped of s. Otherwise, returns a none

Signature

export declare const stripLeftOption: (s: string) => MTypes.OneArgFunction<string, Option.Option<string>>

stripRight

If self ends with s, returns self stripped of s. Otherwise, returns self

Signature

export declare const stripRight: (s: string) => MTypes.StringTransformer

stripRightOption

If self ends with s, returns a some of self stripped of s. Otherwise, returns a none

Signature

export declare const stripRightOption: (s: string) => MTypes.OneArgFunction<string, Option.Option<string>>

tabify

Adds string tabChar count times at the beginning of each new line of self

Signature

export declare const tabify: (tabChar: string, count?: number) => MTypes.StringTransformer

takeLeftBut

Takes all characters from self except the n last characters

Signature

export declare const takeLeftBut: (n: number) => MTypes.StringTransformer

takeLeftTo

Looks from the left for the first substring of self that matches regexp and returns all characters before that substring. If no occurence is found, returns self. ‘g’ flag has no incidence if you pass a regular expression.

Signature

export declare const takeLeftTo: (regexp: RegExp | string) => MTypes.StringTransformer

takeRightBut

Takes all characters from self except the n first characters

Signature

export declare const takeRightBut: (n: number) => MTypes.StringTransformer

takeRightFrom

Looks from the right for the first substring of self that matches regexp and returns all characters after that substring. If no occurence is found, returns self. ‘g’ flag needs not be set if you pass a regular expression.

Signature

export declare const takeRightFrom: (regexp: RegExp | string) => MTypes.StringTransformer

trim

Trims a string to the left or to the right (depending on fillPosition) from character fillChar. If disallowEmptyString is true and the result of trimming is an empty string, the fillChar is returned instead of an empty string. This is useful, for instance, if you have numbers padded with 0’s and you prefer the result of unpadding a string containing only 0’s to be ‘0’ rather than an empty string. fillChar should be a one-character string. length should be a positive integer.

Signature

export declare const trim: ({
  fillChar,
  fillPosition,
  disallowEmptyString
}: {
  readonly fillChar: string
  readonly fillPosition: FillPosition
  readonly disallowEmptyString: boolean
}) => MTypes.OneArgFunction<string, string>

trimEnd

Same as String.trimEnd but the character to remove can be specified. charToRemove must be a one-character string

Signature

export declare const trimEnd: (charToRemove: string) => MTypes.StringTransformer

trimStart

Same as String.trimStart but the character to remove can be specified. charToRemove must be a one-character string

Signature

export declare const trimStart: (charToRemove: string) => MTypes.StringTransformer