Skip to main content

String

Helper functions that have to do with strings.

Functions

capitalizeFirstLetter()

function capitalizeFirstLetter(string): string

Defined in: functions/string.ts:27

Helper function to capitalize the first letter of a string.

Parameters

ParameterType
stringstring

Returns

string


escapeHTMLCharacters()

function escapeHTMLCharacters(string): string

Defined in: functions/string.ts:44

Helper function to replace all of the ampersands, less than signs, greater than signs, double quotes, and single quotes in a string with the escaped counterparts. For example, "<" will be replaced with "<".

Parameters

ParameterType
stringstring

Returns

string


getNumConsecutiveDiacritics()

function getNumConsecutiveDiacritics(string): number

Defined in: functions/string.ts:59

Helper function to get the number of consecutive diacritics in a string.

This is useful for sanitization purposes, since many subsequent diacritics can be a sign of a malicious string.

Parameters

ParameterType
stringstring

Returns

number


hasDiacritic()

function hasDiacritic(string): boolean

Defined in: functions/string.ts:83

Helper function to check if a string has one or more diacritics in it.

Parameters

ParameterType
stringstring

Returns

boolean


hasEmoji()

function hasEmoji(string): boolean

Defined in: functions/string.ts:97

Helper function to check if a string has one or more emoji in it.

Under the hood, this uses the same regex check as the Zod library.

Parameters

ParameterType
stringstring

Returns

boolean


hasWhitespace()

function hasWhitespace(string): boolean

Defined in: functions/string.ts:102

From: https://stackoverflow.com/questions/1731190/check-if-a-string-has-white-space

Parameters

ParameterType
stringstring

Returns

boolean


isFirstLetterCapitalized()

function isFirstLetterCapitalized(string): boolean

Defined in: functions/string.ts:110

From: https://stackoverflow.com/questions/8334606/check-if-first-letter-of-word-is-a-capital-letter

Parameters

ParameterType
stringstring

Returns

boolean


isKebabCase()

function isKebabCase(string): boolean

Defined in: functions/string.ts:115

Kebab case is the naming style of using all lowercase and hyphens, like "foo-bar".

Parameters

ParameterType
stringstring

Returns

boolean


isSemanticVersion()

function isSemanticVersion(versionString): boolean

Defined in: functions/string.ts:124

Helper function to check if a given string is a valid Semantic Version.

Parameters

ParameterType
versionStringstring

Returns

boolean

See

https://semver.org/


kebabCaseToCamelCase()

function kebabCaseToCamelCase(string): string

Defined in: functions/string.ts:130

Helper function to convert a string from kebab-case to camel-case.

Parameters

ParameterType
stringstring

Returns

string


normalizeString()

function normalizeString(string): string

Defined in: functions/string.ts:150

Helper function to normalize a string. Specifically, this performs the following steps:

  • Removes any non-printable characters, if any.
  • Normalizes all newlines to "\n".
  • Normalizes all spaces to " ".
  • Removes leading/trailing whitespace.

Parameters

ParameterType
stringstring

Returns

string

See

https://stackoverflow.com/questions/11598786/how-to-replace-non-printable-unicode-characters-javascript


parseSemanticVersion()

function parseSemanticVersion(versionString): 
| undefined
| {
majorVersion: number;
minorVersion: number;
patchVersion: number;
}

Defined in: functions/string.ts:175

Helper function to parse a Semantic Versioning string into its individual constituents. Returns undefined if the submitted string was not a proper Semantic Version.

Parameters

ParameterType
versionStringstring

Returns

| undefined | { majorVersion: number; minorVersion: number; patchVersion: number; }

See

https://semver.org/


removeLinesBetweenMarkers()

function removeLinesBetweenMarkers(string, marker): string

Defined in: functions/string.ts:234

Helper function to remove lines from a multi-line string. This function looks for a "-start" and a "-end" suffix after the marker. Lines with markets will be completely removed from the output.

For example, by using a marker of "@foo":

line1
# @foo-start
line2
line3
# @foo-end
line4

Would return:

line1
line4

Parameters

ParameterType
stringstring
markerstring

Returns

string


removeLinesMatching()

function removeLinesMatching(string, match): string

Defined in: functions/string.ts:263

Helper function to remove lines from a multi-line string matching a certain other string.

Parameters

ParameterType
stringstring
matchstring

Returns

string


removeNonPrintableCharacters()

function removeNonPrintableCharacters(string): string

Defined in: functions/string.ts:275

Helper function to remove all non-printable characters from a string.

Parameters

ParameterType
stringstring

Returns

string

See

https://stackoverflow.com/questions/11598786/how-to-replace-non-printable-unicode-characters-javascript


removeWhitespace()

function removeWhitespace(string): string

Defined in: functions/string.ts:280

Helper function to remove all whitespace characters from a string.

Parameters

ParameterType
stringstring

Returns

string


trimPrefix()

function trimPrefix(
string,
prefix,
trimAll): string

Defined in: functions/string.ts:292

Helper function to trim a prefix from a string, if it exists. Returns the trimmed string.

Parameters

ParameterTypeDefault valueDescription
stringstringundefinedThe string to trim.
prefixstringundefinedThe prefix to trim.
trimAllbooleanfalseWhether to remove multiple instances of the prefix, if they exist. If this is set to true, the prefix must only be a single character.

Returns

string


trimSuffix()

function trimSuffix(string, prefix): string

Defined in: functions/string.ts:310

Helper function to trim a suffix from a string, if it exists. Returns the trimmed string.

Parameters

ParameterType
stringstring
prefixstring

Returns

string


truncateString()

function truncateString(string, maxLength): string

Defined in: functions/string.ts:323

Helper function to truncate a string to a maximum length. If the length of the string is less than or equal to the provided maximum length, the string will be returned unmodified.

Parameters

ParameterType
stringstring
maxLengthnumber

Returns

string