StringHelper
Helper for string manipulation.
Functions
12
Functions
12HTML
This method decodes HTML entities and returns the original HTML.
See also encodeHtml.
| Parameter | Type | Description |
|---|---|---|
str | String |
This method encodes HTML entities and returns a string that can be placed in the document and produce the original text rather than be interpreted as HTML. Using this method with user-entered values prevents those values from executing as HTML (i.e., a cross-site scripting or "XSS" security issue).
See also decodeHtml.
| Parameter | Type | Description |
|---|---|---|
str | String | Number |
Removes all HTML tags from the passed string and decodes HTML entities.
| Parameter | Type | Description |
|---|---|---|
str | String | The string from which to remove HTML tags. |
The string stripped of HTML tags.
JSON
Parses JSON inside a try-catch block. Returns null if the string could not be parsed.
| Parameter | Type | Description |
|---|---|---|
string | String | String to parse |
Resulting object or null if parse failed
Stringifies an object inside a try-catch block. Returns null if an exception is encountered.
See JSON.stringify on MDN for more information on the arguments.
| Parameter | Type | Description |
|---|---|---|
object | Object | The object to stringify |
replacer | function | circular | String[] | Number[] | A function or array of string/number used to determine
properties to include in the JSON string. Also accepts the string 'circular' to use a built-in replacer handling
circular references, by replacing them with |
space | String | Number | Number of spaces to indent or string used as whitespace |
Resulting object or null if stringify failed
Other
Escapes " and \ in CSS attribute selectors, e.g. [data-id="somevalue"]
Usage:
document.querySelector(StringHelper.cssAttributeQuery('data-id', 'id with & \\ chars'))
| Parameter | Type | Description |
|---|---|---|
attr | String | |
value | String | Number |
Escapes special RegExp characters.
| Parameter | Type | Description |
|---|---|---|
string | String | String to escape |
Escaped string
Generates a UUID. Uses Crypto.randomUUID() if available, otherwise generates a random UUID using
Crypto.getRandomValues().
Converts letters with diacritics to their base letter equivalents. For example, "Ångström" becomes "Angstrom".
| Parameter | Type | Description |
|---|---|---|
str | String | The string to process. |
The processed string with diacritics removed.
This is a tagged template function that performs HTML encoding on replacement values to avoid XSS (Cross-Site Scripting) attacks.
For example:
eventRenderer(eventRecord) {
return StringHelper.xss`<span class="${eventRecord.attrib}">${eventRecord.name}</span>`;
}
| Parameter | Type | Description |
|---|---|---|
strings | TemplateStringsArray | The template string array |
values | any | The interpolated values in the template string |
The encoded string See encodeHtml.
String formatting
Capitalizes the first letter of a string, "myString" -> "MyString".
| Parameter | Type | Description |
|---|---|---|
string | String | The string to capitalize |
The capitalized string or the value of string if falsy.
Makes the first letter of a string lowercase, "MyString" -> "myString".
| Parameter | Type | Description |
|---|---|---|
string | String | The string to un-capitalize. |
The un-capitalized string or the value of string if falsy.