How Lodash lowerCase Handles Compound Words
The _.lowerCase method in the Lodash JavaScript library
converts strings into space-separated, lowercase words. When dealing
with compound words written in conventions such as camelCase,
kebab-case, or snake_case, _.lowerCase breaks these terms
down into their individual constituent words, eliminates all separating
characters or casing boundaries, and joins them using single spaces in
full lowercase.
Splitting and Normalizing Compound Words
When _.lowerCase evaluates a string, it relies
internally on Lodash's word-splitting logic. It identifies the
boundaries between words based on uppercase transitions, punctuation
marks, hyphens, and underscores.
Instead of simply converting all letters to lowercase while retaining
the structure, _.lowerCase fundamentally reformats compound
tokens. It takes the parsed words, strips any punctuation or symbols
connecting them, and unites them with a single standard space.
Behavior by Casing Format
- camelCase and PascalCase: Transitions from
lowercase to uppercase letters mark a word boundary. The uppercase
letters are converted to lowercase and preceded by a space.
_.lowerCase('camelCaseWord')results in'camel case word'._.lowerCase('PascalCaseWord')results in'pascal case word'.
- kebab-case: Hyphens are treated as delimiters and
removed.
_.lowerCase('kebab-case-word')results in'kebab case word'.
- snake_case: Underscores act as delimiters and are
removed.
_.lowerCase('snake_case_word')results in'snake case word'.
- Mixed Delimiters and Casing: Combinations
containing mixed symbols and capitalization are completely normalized.
_.lowerCase('__COMPOUND_mixed-caseWord__')results in'compound mixed case word'.
Handling Consecutive Uppercase Letters
When compound words contain acronyms or abbreviations,
_.lowerCase detects where the acronym ends and the next
word begins:
_.lowerCase('getHTMLResponse')results in'get html response'._.lowerCase('parseXML')results in'parse xml'.
Because _.lowerCase always decouples connected words, it
is best suited for producing human-readable text for display, rather
than generating programming identifiers like variable names or object
keys.