Skip to content

string

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.

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'

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'