RepoPilot

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

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

Question

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

RepoPilot

Perfect! Here's the actual implementation logic:

The decamelization happens in the decamelize function [index.js:L5-L14], which uses a series of regex replacements to insert spaces between camelCase segments:

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');

This function is called conditionally in the main slugify function [index.js:L84-L86]:

if (options.decamelize) {
	string = decamelize(string);
}

The regex patterns handle:

  1. Consecutive uppercase letters followed by digits
  2. Lowercase/digit sequences followed by uppercase letters
  3. Single lowercase/digit followed by uppercase (the main camelCase case)
  4. Consecutive uppercase letters followed by a titlecased word (avoiding plural acronyms like APIs)

FOLLOW_UPS:

  • What happens with consecutive capitals like "XMLParser"?
  • How are spaces converted to the separator character?
  • What does the transliterate option do?

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.