RepoPilot

Does slugify split camelCase strings? Show me the relevant logic.

About sindresorhus/slugify · 2 messages · last activity 9/1/2026

Question

Does slugify split camelCase strings? Show me the relevant logic.

RepoPilot

Perfect! Here's the decamelization logic from [index.js:L5-L14]:

const decamelize = string => string
	// Separate capitalized words.
	.replaceAll(/([A-Z]{2,})(\d+)/g, '$1 $2')
	.replaceAll(/([a-z\d]+)([A-Z]{2,})/g, '$1 $2')

	.replaceAll(/([a-z\d])([A-Z])/g, '$1 $2')
	// `[a-rt-z]` matches all lowercase characters except `s`.
	// This avoids matching plural acronyms like `APIs`.
	.replaceAll(/([A-Z]+)([A-Z][a-rt-z\d]+)/g, '$1 $2');

The function uses a series of regex replacements to insert spaces between camelCase boundaries:

  1. Line 7: Separates consecutive capitals followed by digits (e.g., HTML5HTML 5)
  2. Line 8: Separates lowercase/digit sequences from consecutive capitals (e.g., XMLParserXML Parser)
  3. Line 10: Separates a lowercase/digit from a following capital (e.g., fooBarfoo Bar)
  4. Line 12: Handles acronyms carefully—separates capitals from a capitalized word, but avoids splitting plural acronyms like APIs by excluding the letter s

When decamelize: true (the default), this function is called in the main slugify() function at [index.js:L81-L82].

FOLLOW_UPS:

  • How does transliteration work in slugify?
  • What do custom replacements override by default?
  • How does the counter feature prevent duplicate slugs?

Want to ask your own question?

Open chat for sindresorhus/slugify

Public Q&A. Generated by RepoPilot from the actual source of sindresorhus/slugify. AI answers can be incomplete or stale — verify before relying on them.