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]).
- Rule: A boundary is placed between the lowercase character and the beginning of the uppercase character.
- Example:
fooBaris separated at theoandBboundary to producefooandbar, yieldingfoo-bar.
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])).
- Rule: A boundary is placed before the final uppercase character in a consecutive sequence if that final uppercase character is followed by a lowercase letter.
- Example:
getHTTPResponseis split intoget,HTTP, andResponse, producingget-http-responserather thanget-httpresponse.
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.
- Rule: Boundaries exist between digits
(
0-9) and adjacent alphabetic characters (a-zA-Z), splitting numbers from succeeding words. - Example:
item12Detailsis split at the number boundaries to yielditem,12, anddetails, resulting initem-12-details.
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.
- Rule: Non-word characters are matched as separators and are stripped entirely from the resulting tokens.
- Example:
user_camelCasesplits at the underscore and thelCtransition, yieldinguser-camel-case.
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).
- Rule: The same casing and acronym transitions used for ASCII are mapped to their respective Unicode casing counterparts.
- Example: Strings containing characters like
éorØare correctly split when followed or preceded by opposing casing formats.