How Lodash kebabCase Splits CamelCase Strings

The Lodash _.kebabCase function converts strings into hyphen-separated lowercase words by first parsing the input into individual word tokens using an internal boundary-detection mechanism. This article outlines the specific character boundaries Lodash recognizes to accurately split camelCase and mixed-case strings, covering transitions between lowercase and uppercase characters, consecutive acronyms, numeric sequences, and Unicode boundaries.

The Underlying Mechanism: Lodash words

To convert a string to kebab-case, _.kebabCase calls Lodash's internal words function. Instead of splitting by a simple delimiter like a space or hyphen, Lodash applies specialized regular expressions to match complete words based on character classes and casing transitions.

Depending on the input string, Lodash selects either a standard ASCII regex pattern or a comprehensive Unicode-aware regex pattern.

Specific Character Bounds Recognized

Lodash splits camelCased strings by identifying the following specific structural boundaries:

1. Lowercase to Uppercase Transitions

The most common camelCase boundary occurs when an uppercase letter directly follows a lowercase letter ([a-z][A-Z]).

2. Acronyms and Consecutive Uppercase Boundaries

When multiple consecutive uppercase letters are followed by a lowercase letter, standard camelCase splitters often break words incorrectly. Lodash handles this by using a lookahead pattern that recognizes where an acronym ends and a standard capitalized word begins ([A-Z]{2,}(?=[A-Z][a-z])).

3. Alphabetic and Numeric Boundaries

Lodash distinguishes between letters and numeric sequences, treating contiguous blocks of numbers as distinct entities or suffixes depending on their surrounding casing.

4. Non-Alphanumeric and Symbol Boundaries

Any character that is not a letter or a digit serves as an explicit delimiter. Characters such as underscores (_), hyphens (-), spaces (\s), and other punctuation are treated as terminal boundaries.

5. Unicode Casing Boundaries

If a string contains characters outside the standard ASCII range (such as accented characters or non-Latin scripts), Lodash uses regular expressions built with Unicode character classes (such as \p{Lu} for uppercase letters and \p{Ll} for lowercase letters).