Str module
📖 Description
The Str
module contains helper functions related to strings.
The functions in this module are primitive and probably widely available in many other open-source packages on npm. So why are they included in the Tripetto Runner library? That's because the Tripetto Runner library doesn't have any dependency on other npm packages. So all the code needed to run a Tripetto form is included and these functions are simply required. Since they are part of the package, they are exported so you can use them too. For example when building custom blocks.
▶️ Functions
🔧 capitalize
Capitalizes the first character of a string or the first character of each word.
Signature
capitalize(
s: string,
mode?: "first-character" | "each-word" | "each-sentence",
lowercase?: boolean
): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
mode | "first-character" | "each-word" | "each-sentence" | Yes | Specifies the mode (default is "first-character" ). |
lowercase | boolean | Yes | Converts the string to lowercase before capitalizing it (default is false ). |
Return value
Returns the capitalized string.
Example
import { Str } from "@tripetto/runner";
Str.capitalize("lorem ipsum. dolor"); // Returns `Lorem ipsum. dolor`
Str.capitalize("lorem ipsum. dolor", "each-word"); // Returns `Lorem Ipsum. Dolor`
Str.capitalize("lorem ipsum. dolor", "each-sentence"); // Returns `Lorem ipsum. Dolor`
🔧 CRLFToHTML
Converts carriage returns and/or newlines to HTML breaks.
Signature
CRLFToHTML(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the string to be converted. |
Return value
Returns the converted string.
Example
import { Str } from "@tripetto/runner";
Str.CRLFToHTML("Row 1\r\nRow 2"); // Returns `Row 1<br />Row 2`
🔧 djb2Hash
Creates a simple hash for the supplied string using the djb2-algorithm written by Dan Bernstein. This hash function is similar to a linear congruential generator and is absolutely not collision resistant.
Signature
djb2Hash(s: string, prefix?: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
prefix | string | Yes | Specifies a prefix for the hash result. |
Return value
Returns the hash string.
Example
import { Str } from "@tripetto/runner";
Str.djb2Hash("Abc"); // Returns `ABCAA`
Str.djb2Hash("Abc", "prefix-"); // Returns `prefix-ABCAA`
🔧 extract
Extracts the part of the string between the first occurrence of left
and the optional occurrence of right
.
Signature
extract<T>(
s: string,
options: {
left: string;
right?: string;
fromEnd?: boolean;
ignoreCase?: boolean;
}
): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the string to be extracted (variable will cast to a string if necessary). |
options | object | No | Specifies the extract options. Supports the following options: - left : Left string selector:- right : Right string selector:- fromEnd : Specifies the search direction of the right string selector. If false is specified, the function searches for the first occurrence of Right directly after the position of Left .- ignoreCase : Specifies if a case insensitive check should be performed. |
Return value
Returns the string between left
and right
or an empty string if an error occurs.
Example
import { Str } from "@tripetto/runner";
Str.extract("Lorem ipsum dolor", { left: "Lo", right: "m" }); // Returns `re`
Str.extract("Lorem ipsum dolor", { left: "Lo", right: "m", fromEnd: true }); // Returns `rem ipsu`
🔧 fill
Fills a string for the specified times with the specified string or characters.
Signature
fill(s: string, count: number): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
count | number | No | Specifies the number of copies to insert. |
Return value
Returns the filled string.
Example
import { Str } from "@tripetto/runner";
Str.fill("A", 5); // Returns `AAAAA`
🔧 iterateToString
Converts an array or object list to a concatenated string.
Signature
iterateToString<T>(
list: TList<T>,
separator?: string,
fn?: (item: T, ...arguments: any[]) => string,
...arguments: any[]
): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
list | T[] | undefined | No | Specifies the array, collection or enumerable object to iterate through. |
separator | string | Yes | Optional parameter which specifies the separator string or char. |
fn | (item: T, ...arguments: any[]) => string | Yes | Optional function to be invoked for each element (instead of automatic casting to strings). The element value will be exposed to the function as the first argument of the argument list. Additional arguments can be specified and will be pushed to the function. |
arguments | any[] | Yes | Optional arguments which should be passed to the callee. |
Return value
Returns the concatenated string.
Example
import { Str } from "@tripetto/runner";
Str.iterateToString(["a", "b", "c"]); // Returns `abc`
Str.iterateToString(["a", "b", "c"], "-"); // Returns `a-b-c`
Str.iterateToString(["a", "b", "c"], "/", (sValue: string) => sValue + sValue); // Returns `aa/bb/cc`
🔧 limit
Limits a string that is greater than the specified number of characters and appends an optional string. If an append string is specified any trailing spaces in the trimmed string are removed.
Signature
limit(s: string, max: number, append?: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
max | number | No | Maximum string length. |
append | string | Yes | Optional string which is appended to a limited string. |
Return value
Returns the limited string.
Example
import { Str } from "@tripetto/runner";
Str.limit("Lorem ipsum", 5, "..."); // Returns `Lorem...`
Str.limit("Lorem ipsum", 6, "..."); // Returns `Lorem...`
Str.limit("Lorem ipsum", 6); // Returns `Lorem `
🔧 lowercase
Converts a string to lowercase.
Signature
lowercase(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
Return value
Returns the converted string.
Example
import { Str } from "@tripetto/runner";
Str.lowercase("HELLO"); // Returns `hello`
🔧 makeHTMLSafe
Converts HTML brackets to HTML entities so it is safe to display without HTML parsing.
Careful, only the brackets (<
and >
) are converted to HTML entities.
Signature
makeHTMLSafe(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the string to be converted. |
Return value
Returns the converted string.
Example
import { Str } from "@tripetto/runner";
Str.makeHTMLSafe("<b>Hello</b>"); // Returns `<b>Hello<b>`
🔧 padLeft
Converts source variable to a string and pads the string on the left with the supplied number of characters.
Signature
padLeft(
value: string | number,
fill: string,
length: number,
crop?: boolean,
treatAsNumber?: boolean
): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
value | string | number | No | Specifies the input string or number. |
fill | string | No | Contains the string which will be used as fill string. |
length | number | No | Specifies the desired string length. |
crop | boolean | Yes | Optional boolean value which enables string cropping if the source string length is larger than the desired string length. |
treatAsNumber | boolean | Yes | Optional boolean value which specifies the input string should be treated as a number. |
Return value
Returns the padded string.
Example
import { Str } from "@tripetto/runner";
Str.padLeft("ABC", "A", 5); // Returns `AAABC`
🔧 padRight
Converts source variable to a string and pads the string on the right with the supplied number of characters.
Signature
padRight(value: string | number, fill: string, length: number, crop?: boolean): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
value | string | number | No | Specifies the input string or number. |
fill | string | No | Contains the string which will be used as fill string. |
length | number | No | Specifies the desired string length. |
crop | boolean | Yes | Optional boolean value which enables string cropping if the source string length is larger than the desired string length. |
Return value
Returns the padded string.
Example
import { Str } from "@tripetto/runner";
Str.padRight("ABC", "A", 5); // Returns `ABCAA`
🔧 removeWhitespaces
Removes all white spaces from the specified string (_ab__cd_
-> abcd
).
Signature
removeWhitespaces(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
Return value
Returns the sanitized string.
Example
import { Str } from "@tripetto/runner";
Str.removeWhitespaces(" ab cd "); // Returns `abcd`
🔧 replace
Replaces all occurrences of what
with with
in the specified string.
Signature
replace(s: string, what: string, with?: string, ignoreCase?: boolean): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string (haystack). |
what | string | No | Specifies the string to search for (needle). |
with | string | Yes | Specifies the replace string. If omitted an empty string will be used. |
ignoreCase | boolean | Yes | Specifies if the string replace should be case insensitive (default is false ). |
Return value
Returns the replaced string.
Example
import { Str } from "@tripetto/runner";
Str.replace("Hello", "l", "-"); // Returns `He--o`
🔧 replaceMultiple
Replaces all occurrences of strings in what
with with
in the specified string.
Signature
replaceMultiple(s: string, what: string[], with?: string, ignoreCase?: boolean): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string (haystack). |
what | string[] | No | Specifies the strings to search for (needles). |
with | string | Yes | Specifies the replace string. If omitted an empty string will be used. |
ignoreCase | boolean | Yes | Specifies if the string replace should be case insensitive (default is false ). |
Return value
Returns the replaced string.
Example
import { Str } from "@tripetto/runner";
Str.replaceMultiple("Hello", ["ll", "o"], "-"); // Returns `He---`
🔧 sanitize
Sanitize a string by removing all leading, trailing and multiple whitespaces (_ab__cd_
-> ab_cd
).
Signature
sanitize(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
Return value
Returns the sanitized string.
Example
import { Str } from "@tripetto/runner";
Str.sanitize(" ab cd "); // Returns `ab cd`
🔧 trim
Trims a string by removing all leading and trailing whitespaces (_ab__cd_
-> ab__cd
).
Signature
trim(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
Return value
Returns the trimmed string.
Example
import { Str } from "@tripetto/runner";
Str.trim(" ab cd "); // Returns `ab cd`
🔧 trimLeft
Trims a string at the left side by removing all leading whitespaces (_ab__cd_
-> ab__cd_
).
Signature
trimLeft(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
Return value
Returns the trimmed string.
Example
import { Str } from "@tripetto/runner";
Str.trimLeft(" ab cd "); // Returns `ab cd `
🔧 trimMultiple
Trims a string by removing all multiple whitespaces (_ab__cd_
-> _ab_cd_
).
Signature
trimMultiple(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
Return value
Returns the trimmed string.
Example
import { Str } from "@tripetto/runner";
Str.trimMultiple(" ab cd "); // Returns ` ab cd `
🔧 trimRight
Trims a string at the right by removing all trailing whitespaces (_ab__cd_
-> _ab__cd
).
Signature
trimRight(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
Return value
Returns the trimmed string.
Example
import { Str } from "@tripetto/runner";
Str.trimRight(" ab cd "); // Returns ` ab cd`
🔧 uppercase
Converts a string to uppercase.
Signature
uppercase(s: string): string
Parameters
Name | Type | Optional | Description |
---|---|---|---|
s | string | No | Specifies the input string. |
Return value
Returns the converted string.
Example
import { Str } from "@tripetto/runner";
Str.uppercase("Hello"); // Returns `HELLO`