The @perfective/common/string
package works with the standard JS
String
type.
It provides the following functions and additional types.
-
Type guards:
-
isString<T>(value: T | string): value is string
— returnstrue
if a given value is astring
. -
isNotString<T>(value: T | string): value is T
— returnstrue
if a given value is not astring
.
-
-
Properties:
-
length(value: string): number
— returns the length of a given string.
-
-
Predicates:
-
isEmpty(value: string): boolean
— returnstrue
if a given string is empty. -
isNotEmpty(value: string): boolean
— returnstrue
if a given string is not empty.
-
-
Operators:
-
lines(value: string): string[]
— splits a given string into an array of string based on the line separator (\n
,\r\n
, and\r
). -
lowerCase(value: string): string
— converts a given string to lower case. -
upperCase(value: string): string
— converts a given string to upper case. -
trim(value: string): string
— removes whitespace from both ends of a given string.
-
-
Curried functions:
-
charAt(index: number): Unary<string, string>
— creates a function that returns a UTF-16 code unit at a given zero-basedindex
in the input string. -
concat(…strings: string[]): Unary<string, string>
— creates a function that returns a string built from the input string and concatenated with givenstrings
. -
concatTo(value: string): Unary<string | string[], string>
— creates a function that returns a string built from the input string(s) concatenated to a givenstring
. -
endsWith(search: string, endPosition?: number): Unary<string, boolean>
— creates a function that returnstrue
if a givensearch
string is found at a givenendPosition
index of the input string.If
endPosition
is omitted, the input string length is used. -
includes(search: string, position: number = 0): Unary<string, boolean>
— creates a function that returnstrue
if a givensearch
string is found in the input string starting at a givenposition
index. -
indexOf(search: string, from: number = 0): Unary<string, number | -1>
— creates a function that returns the index of the first occurrence of a givensearch
string in the input string, starting at a givenfrom
index; or returns-1
if the givensearch
string is not found. -
lastIndexOf(search: string, from?: number): Unary<string, number | -1>
— creates a function that returns the index of the first occurrence of a givensearch
string in the input string, starting at a givenfrom
index; or returns-1
if the givensearch
string is not found. -
padEnd(length: number, fill?: string): Unary<string, string>
— creates a function that pads the of end the input string with a givenfill
string up to the targetlength
. -
padStart(length: number, fill?: string): Unary<string, string>
— creates a function that pads the of start the input string with a givenfill
string up to the targetlength
. -
repeat(count: number): Unary<string, string>
— creates a function that creates a string consisting of a givencount
of copies of the input string. -
replace(search: string | RegExp, replacement: string): Unary<string, string>
— creates a function that replaces givensearch
substrings in the input string with a givenreplacement
. -
replaceWith(search: string | RegExp, replacement: Replacement): Unary<string, string>
— creates a function that replaces givensearch
substrings in the input string with a result of thereplacement
function (invoked on every match). -
search(search: RegExp): Unary<string, number | -1>
— creates a function that returns the index of the first occurrence of a given regular expression in the input string; or returns-1
if the given expression is not found. -
slice(start: number, end?: number): Unary<string, string>
— creates a function that returns a section of the input string from a givenstart
index to the end of the string, or to a givenend
index (exclusive). -
split(separator: string | RegExp, limit?: number): Unary<string, string[]>
— creates a function that creates an ordered list of substrings by splitting the input string using a givenseparator
and up to an optionallimit
. -
startsWith(search: string, from: number = 0): Unary<string, boolean>
— creates a function that returnstrue
if the input string begins with a givensearch
substring at a givenfrom
index.
-
-
Utf16CodeUnit
: — an integer between0
and65535
(0xFFFF
) representing a UTF-16 code unit.-
charCodeAt(index: number): Unary<string, Utf16CodeUnit>
— creates a function that returns a UTF-16 code unit value at a given zero-basedindex
in the input string.
-
-
CodePoint
: — an integer between0
and0x10FFFF
(inclusive) representing a Unicode code point.-
codePointAt(position: number): Unary<string, CodePoint | undefined>
— creates a function that returns a code point value of the character at a givenindex
in the input string; or returnsundefined
if the givenindex
is out of range.
-
-
UnicodeNormalizationForm
:-
normalize(form: UnicodeNormalizationForm = 'NFC'): Unary<string, string>
— creates a function that returns a string containing the Unicode Normalization Form of the input string for a given normalizationform
.
-
-
Format
— represents a template with tokens that can be turned into a string.-
format(template: string, tokens: Tokens | unknown[] = {}): Format
— creates a {@link Format} record with the giventemplate
andtokens
. -
formatted(input: Format): string
— replacesFormat.tokens
in theFormat.template
and returns the resulting string.Each token is wrapped in the double curly braces. For example, a template with a token
{{foo}}
will be replaced by the string value of the tokenfoo
.
-
-
Tokens
: — a mapping between a token and its string value.-
tokens(tokens: unknown[] | Tokens): Tokens
— createsTokens
record from a given array of positional tokens, where each token is an index of each value in the given array.If given a
Tokens
object returns the given object.
-
-
Implement with the
@perfective/common/locale
package: -
Implement with the
@perfective/common/regexp
package: -
Implement with a polyfill: