Skip to content

string

Utility functions for normalizing, comparing, pattern matching, and transforming strings.

DIACRITICS

A map of base characters to their diacritic variants, used by makeDiacriticPattern.

js
string.DIACRITICS = {
  a: 'aáàäâã',
  e: 'eéëèê',
  i: 'iíïìî',
  o: 'oóöòõô',
  u: 'uüúùû',
  c: 'cç'
}

normalize

Signature

js
string.normalize (str, options = {})

Description

Normalizes a string by optionally collapsing whitespace, stripping diacritics, and lowercasing. Transformations are applied in order: spaces → diacritics → case.

Parameters

NameTypeRequiredDescription
strstringyesThe string to normalize
optionsobjectnoNormalization options
options.ignoreSpacesbooleannoCollapse consecutive whitespace and trim. Defaults to false
options.ignoreDiacriticsbooleannoStrip diacritics using NFKD decomposition. Defaults to false
options.ignoreCasebooleannoConvert to lowercase. Defaults to false
options.localestringnoLocale passed to toLocaleLowerCase (e.g. 'fr-FR'). Defaults to system locale

Returns

TypeDescription
stringThe normalized string

Throws

Throws a TypeError if str is not a string, or if options does not conform to the expected schema (e.g. wrong option type).

Examples

js
string.normalize('  Héllo   World  ', { ignoreSpaces: true })
// 'Héllo World'

string.normalize('éàü', { ignoreDiacritics: true })
// 'eau'

string.normalize('Hello', { ignoreCase: true })
// 'hello'

string.normalize('  Héllo  ', { ignoreSpaces: true, ignoreDiacritics: true, ignoreCase: true })
// 'hello'

compare

Signature

js
string.compare (str1, str2, options = {})

Description

Compares two strings for sorting purposes, using locale-aware collation (String.prototype.localeCompare) rather than raw Unicode code point comparison. This ensures accented characters sort in their expected linguistic position (e.g. 'été' sorts before 'zoo', not after).

Internally normalizes both strings via string.normalize before comparing. Unlike normalize, diacritics and case are ignored by default, since this reflects the typical intent when sorting user-facing labels.

Can be passed directly as the callback to Array.prototype.sort.

Parameters

NameTypeRequiredDescription
str1stringyesThe first string to compare
str2stringyesThe second string to compare
optionsobjectnoComparison options
options.ignoreSpacesbooleannoCollapse consecutive whitespace and trim before comparing. Defaults to false
options.ignoreDiacriticsbooleannoIgnore diacritics when comparing. Defaults to true
options.ignoreCasebooleannoIgnore case when comparing. Defaults to true
options.localestringnoLocale passed to localeCompare (e.g. 'fr-FR'). Defaults to system locale

Returns

TypeDescription
numberA negative number if str1 sorts before str2, a positive number if after, 0 if equal under the given options

Throws

Throws a TypeError if str1 or str2 is not a string, or if options does not conform to the expected schema.

Examples

js
string.compare('été', 'zoo')
// negative — 'été' sorts before 'zoo'

string.compare('Hello', 'hello')
// 0 — case is ignored by default

string.compare('été', 'ete', { ignoreDiacritics: false })
// non-zero — diacritics are distinguished

;['zèbre', 'étoile', 'abricot'].sort(string.compare)
// ['abricot', 'étoile', 'zèbre']

makeDiacriticPattern

Signature

js
string.makeDiacriticPattern (pattern, options = {})

Description

Converts a string into a regex-compatible pattern where each character is expanded to match all its diacritic variants. Useful for building case/accent-insensitive search patterns.

By default (reverse: false), only base characters (e.g. a) are expanded to their variants ([aáàäâã]). With reverse: true, any diacritic variant in the pattern is also expanded.

Parameters

NameTypeRequiredDescription
patternstringyesThe string to convert into a diacritic pattern
optionsobjectnoOptions
options.reversebooleannoIf true, expands diacritic variants back to their family. Defaults to false

Returns

TypeDescription
stringA regex-compatible pattern string

Throws

Throws a TypeError if pattern is not a string.

Examples

js
string.makeDiacriticPattern('cafe')
// 'c[cç][aáàäâã]f[eéëèê]'

string.makeDiacriticPattern('café', { reverse: true })
// 'c[cç][aáàäâã]f[eéëèê]'

// Use in a regex
const pattern = string.makeDiacriticPattern('cafe')
new RegExp(pattern, 'i').test('Café') // true

slugify

Signature

js
string.slugify (str, separator = '-')

Description

Converts a string into a URL-friendly slug by stripping diacritics, lowercasing, and replacing non-alphanumeric characters with a separator. Leading and trailing separators are removed.

Parameters

NameTypeRequiredDescription
strstringyesThe string to slugify
separatorstringnoA single character used as separator. Defaults to '-'

Returns

TypeDescription
stringThe slugified string

Throws

Throws a TypeError if str is not a string or if separator is not a single character.

Examples

js
string.slugify('Hello World')
// 'hello-world'

string.slugify('Héllo Wörld')
// 'hello-world'

string.slugify('  Hello   World  ')
// 'hello-world'

string.slugify('Hello World', '_')
// 'hello_world'

initials

Signature

js
string.initials (str, options = {})

Description

Extracts the initials of a string by taking the first character of each word, uppercased.

Parameters

NameTypeRequiredDescription
strstringyesThe string to extract initials from
optionsobjectnoOptions
options.maxnumbernoMaximum number of initials to return

Returns

TypeDescription
stringThe initials, uppercased and concatenated

Throws

Throws a TypeError if str is not a string.

Examples

js
string.initials('John Doe')                        // 'JD'
string.initials('Jean Pierre Dupont')              // 'JPD'
string.initials('Jean Pierre Dupont', { max: 2 }) // 'JP'
string.initials('john doe')                        // 'JD'

words

Signature

js
string.words (str)

Description

Splits a string into words using Unicode-aware rules.

The function recognizes words from common naming conventions such as camel case, Pascal case, acronyms, separators, spaces, and digits.

Parameters

NameTypeRequiredDescription
strstringyesThe string to split into words

Returns

TypeDescription
string[]The detected words

Throws

Throws a TypeError if str is not a string.

Examples

js
string.words('roundedRect')
// ['rounded', 'Rect']

string.words('XMLHttpRequest')
// ['XML', 'Http', 'Request']

string.words('triangle-down')
// ['triangle', 'down']

string.words('hello_world')
// ['hello', 'world']

string.words('star5')
// ['star', '5']

capitalize

Signature

js
string.capitalize (str)

Description

Capitalizes the first character of a string and lowercases the remaining characters.

Parameters

NameTypeRequiredDescription
strstringyesThe string to capitalize

Returns

TypeDescription
stringThe capitalized string

Throws

Throws a TypeError if str is not a string.

Examples

js
string.capitalize('hello')
// 'Hello'

string.capitalize('HELLO')
// 'Hello'

string.capitalize('hELLO')
// 'Hello'

camelCase

Signature

js
string.camelCase (str)

Description

Converts a string to camel case.

The input is split using string.words. Detected words are normalized by removing diacritics. The first word is lowercased and subsequent words are capitalized.

Parameters

NameTypeRequiredDescription
strstringyesThe string to convert

Returns

TypeDescription
stringThe camel-case string

Throws

Throws a TypeError if str is not a string.

Examples

js
string.camelCase('rounded-rect')
// 'roundedRect'

string.camelCase('XML HTTP parser')
// 'xmlHttpParser'

string.camelCase('Éléphant Bleu')
// 'elephantBleu'

pascalCase

Signature

js
string.pascalCase (str)

Description

Converts a string to Pascal case.

The input is split using string.words. Detected words are normalized by removing diacritics and each word is capitalized.

Parameters

NameTypeRequiredDescription
strstringyesThe string to convert

Returns

TypeDescription
stringThe Pascal-case string

Throws

Throws a TypeError if str is not a string.

Examples

js
string.pascalCase('rounded-rect')
// 'RoundedRect'

string.pascalCase('XML HTTP parser')
// 'XmlHttpParser'

string.pascalCase('éléphant bleu')
// 'ElephantBleu'

kebabCase

Signature

js
string.kebabCase (str)

Description

Converts a string to kebab case.

The input is split using string.words. Detected words are normalized by removing diacritics, lowercased, and joined with -.

Parameters

NameTypeRequiredDescription
strstringyesThe string to convert

Returns

TypeDescription
stringThe kebab-case string

Throws

Throws a TypeError if str is not a string.

Examples

js
string.kebabCase('roundedRect')
// 'rounded-rect'

string.kebabCase('XMLHttpRequest')
// 'xml-http-request'

string.kebabCase('ÉléphantBleu')
// 'elephant-bleu'

snakeCase

Signature

js
string.snakeCase (str)

Description

Converts a string to snake case.

The input is split using string.words. Detected words are normalized by removing diacritics, lowercased, and joined with _.

Parameters

NameTypeRequiredDescription
strstringyesThe string to convert

Returns

TypeDescription
stringThe snake-case string

Throws

Throws a TypeError if str is not a string.

Examples

js
string.snakeCase('roundedRect')
// 'rounded_rect'

string.snakeCase('triangle-down')
// 'triangle_down'

string.snakeCase('ÉléphantBleu')
// 'elephant_bleu'

constantCase

Signature

js
string.constantCase (str)

Description

Converts a string to constant case.

The input is split using string.words. Detected words are normalized by removing diacritics, uppercased, and joined with _.

Parameters

NameTypeRequiredDescription
strstringyesThe string to convert

Returns

TypeDescription
stringThe constant-case string

Throws

Throws a TypeError if str is not a string.

Examples

js
string.constantCase('roundedRect')
// 'ROUNDED_RECT'

string.constantCase('triangle-down')
// 'TRIANGLE_DOWN'

string.constantCase('ÉléphantBleu')
// 'ELEPHANT_BLEU'

dotCase

Signature

js
string.dotCase (str)

Description

Converts a string to dot case.

The input is split using string.words. Detected words are normalized by removing diacritics, lowercased, and joined with ..

Parameters

NameTypeRequiredDescription
strstringyesThe string to convert

Returns

TypeDescription
stringThe dot-case string

Throws

Throws a TypeError if str is not a string.

Examples

js
string.dotCase('roundedRect')
// 'rounded.rect'

string.dotCase('triangle-down')
// 'triangle.down'

string.dotCase('ÉléphantBleu')
// 'elephant.bleu'

titleCase

Signature

js
string.titleCase (str)

Description

Converts a string to title case.

The input is split using string.words(). Each detected word is capitalized and joined with spaces.

Unlike the other case conversion helpers, titleCase() preserves diacritics.

Parameters

NameTypeRequiredDescription
strstringyesThe string to convert

Returns

TypeDescription
stringThe title-case string

Throws

Throws a TypeError if str is not a string.

Examples

js
string.titleCase('roundedRect')
// 'Rounded Rect'

string.titleCase('XML HTTP parser')
// 'Xml Http Parser'

string.titleCase('éléphantBleu')
// 'Éléphant Bleu'