StringHelper

Helper for string manipulation.

Functions

12

HTML

This method decodes HTML entities and returns the original HTML.

See also encodeHtml.

ParameterTypeDescription
strString
Returns: 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.

ParameterTypeDescription
strString | Number
Returns: String

Removes all HTML tags from the passed string and decodes HTML entities.

ParameterTypeDescription
strString

The string from which to remove HTML tags.

Returns: String -

The string stripped of HTML tags.

JSON

Parses JSON inside a try-catch block. Returns null if the string could not be parsed.

ParameterTypeDescription
stringString

String to parse

Returns: Object -

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.

ParameterTypeDescription
objectObject

The object to stringify

replacerfunction | 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 '[Circular]'. See MDN for more information.

spaceString | Number

Number of spaces to indent or string used as whitespace

Returns: String -

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'))
ParameterTypeDescription
attrString
valueString | Number
Returns: String

Escapes special RegExp characters.

ParameterTypeDescription
stringString

String to escape

Returns: String -

Escaped string

Generates a UUID. Uses Crypto.randomUUID() if available, otherwise generates a random UUID using Crypto.getRandomValues().

Returns: String

Converts letters with diacritics to their base letter equivalents. For example, "Ångström" becomes "Angstrom".

ParameterTypeDescription
strString

The string to process.

Returns: String -

The processed string with diacritics removed.

xssstatic

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>`;
 }
ParameterTypeDescription
stringsTemplateStringsArray

The template string array

valuesany

The interpolated values in the template string

Returns: String -

The encoded string See encodeHtml.

String formatting

Capitalizes the first letter of a string, "myString" -> "MyString".

ParameterTypeDescription
stringString

The string to capitalize

Returns: String -

The capitalized string or the value of string if falsy.

Makes the first letter of a string lowercase, "MyString" -> "myString".

ParameterTypeDescription
stringString

The string to un-capitalize.

Returns: String -

The un-capitalized string or the value of string if falsy.