Skip to main content

string

Library of helpers for formatting strings.

capitalize()

function capitalize(str): string

Defined in: string.ts:224

Returns a string in lowercase excepted for the 1st character capitalized using firstUppercase.

Parameters

str

Maybe<string>

the string to be transformed.

Returns

string

a capitalized string, or an empty string if str is not a valid string.

Example

import { capitalize } from "@graphql-markdown/utils/string";

capitalize("the quick Brown Fox");
// Expected result: "The quick brown fox"

escapeMDX()

function escapeMDX(str): string

Defined in: string.ts:177

Returns a string with MDX special characters converted to HTML unicode using toHTMLUnicode. Characters within code notation should not be converted. List of special characters: {, <, >, }

Parameters

str

unknown

the string to be transformed.

Returns

string

a string with MDX special characters replaced by HTML unicode equivalents.

Example

import { escapeMDX } from "@graphql-markdown/utils/string";

escapeMDX("{MDX} <special> characters");
// Expected result: "&#x007B;MDX&#x007D; &#x003C;special&#x003E; characters"

escapeMDX("`{MDX}` `<special>` characters");
// Expected result: "`{MDX}` `<special>` characters"

firstUppercase()

function firstUppercase(str): string

Defined in: string.ts:200

Returns a string with the 1st character in uppercase.

Parameters

str

Maybe<string>

the string to be transformed.

Returns

string

a string with the 1st character in uppercase, or an empty string if str is not a valid string.

Example

import { firstUppercase } from "@graphql-markdown/utils/string";

firstUppercase("the quick Brown Fox");
// Expected result: "The quick Brown Fox"

kebabCase()

function kebabCase(str): string

Defined in: string.ts:267

Returns a lowercase string with - as replacement for non alphanum characters using stringCaseBuilder.

Parameters

str

Maybe<string>

the string to be transformed.

Returns

string

a string converted to start case, or an empty string if str is not a valid string.

Example

import { kebabCase } from "@graphql-markdown/utils/string";

kebabCase("The quick brown Fox");
// Expected result: "the-quick-brown-fox"

prune()

function prune(str, substr): string

Defined in: string.ts:58

Returns a string pruned on both start and end, similar to trim() but with any substring.

Parameters

str

Maybe<string>

the string to be pruned.

substr

string = ""

the substring to be removed from str.

Returns

string

a pruned string, or an empty string if str is not a valid string.

Example

import { prune } from "@graphql-markdown/utils/string";

const text = "**The quick brown fox jumps over the lazy dog.**";

prune(text, "**");
// Expected result: "The quick brown fox jumps over the lazy dog."

replaceDiacritics()

function replaceDiacritics(str): string

Defined in: string.ts:27

Replaces diacritics by non-diacritic equivalent characters.

Parameters

str

Maybe<string>

the string to be transformed.

Returns

string

a string with diacritic characters replaced, or an empty string if str is not a valid string.

Example

import { replaceDiacritics } from "@graphql-markdown/utils/string";

replaceDiacritics("Âéêś"); // Expected result: "Aees"

See

StackOverflow source.


slugify()

function slugify(str): string

Defined in: string.ts:282

Alias of kebabCase.

Parameters

str

Maybe<string>

the string to be transformed.

Returns

string

a string converted to start case, or an empty string if str is not a valid string.

Alias

Example

import { kebabCase } from "@graphql-markdown/utils/string";

kebabCase("The quick brown Fox");
// Expected result: "the-quick-brown-fox"

startCase()

function startCase(str): string

Defined in: string.ts:246

Applies firstUppercase using stringCaseBuilder to every word of a string with space character as separator.

Parameters

str

Maybe<string>

the string to be transformed.

Returns

string

a string converted to start case, or an empty string if str is not a valid string.

Example

import { startCase } from "@graphql-markdown/utils/string";

startCase("the quick Brown Fox");
// Expected result: "The Quick Brown Fox"

stringCaseBuilder()

function stringCaseBuilder(
str,
transformation?,
separator?,
splitter?): string

Defined in: string.ts:100

Returns a string after applying a transformation function. By default splitter expression will split the string into words, where non-alphanum chars are considered as word separators. separator will be used for joining the words back together. prune using separator is applied to the result of the transformation.

Parameters

str

Maybe<string>

the string to be transformed.

transformation?

Maybe<(word) => string>

optional transformation callback function.

separator?

string

optional character separator for word-based transformation.

splitter?

optional regex or string rule for splitting string into word.

string | RegExp

Returns

string

a transformed string, or an empty string if str is not a valid string.

Example

import { stringCaseBuilder } from "@graphql-markdown/utils/string";

const text = "The quick brown fox jumps over the lazy dog.";
const transformation = (word: string): string => `*${word}*`

stringCaseBuilder(text, transformation, " ");
// Expected result: "*The* *quick* *brown* *fox* *jumps* *over* *the* *lazy* *dog*"

toHTMLUnicode()

function toHTMLUnicode(char): string

Defined in: string.ts:146

Converts a character to its equivalent HTML unicode representation &#x0000.

Parameters

char

Maybe<string>

the character to be transformed.

Returns

string

a HTML unicode representation of char, or an empty string if char is not a valid string.

Example

import { toHTMLUnicode } from "@graphql-markdown/utils/string";

toHTMLUnicode("%"); // Expected result: "&#x0025;"

toString()

function toString(variable): string

Defined in: string.ts:292

Returns a stringified version of the variable.

Parameters

variable

unknown

the variable to be transformed.

Returns

string

a string