How Lodash lowerCase Deconstructs Hyphenated Strings

Lodash’s _.lowerCase method converts strings into separated, lowercase words by extracting individual linguistic components and recombining them. When processing explicitly hyphenated compound strings (such as "foo-bar-baz"), Lodash systematically identifies hyphens as boundary delimiters, isolates the alphanumeric substrings via internal regular expression engines, and reduces the extracted tokens into a single space-delimited string.

The Compounder Pipeline

At its core, _.lowerCase is not an isolated function; it is generated by an internal higher-order function called createCompounder. This utility takes a transformation callback and standardizes how strings are split, modified, and joined:

var lowerCase = createCompounder(function(result, word, index) {
  return result + (index ? ' ' : '') + word.toLowerCase();
});

When a hyphenated string is passed into this compounder, the method initiates a two-phase process: decomposition via the internal words() function and recomposition via the callback.

Step 1: Delimiter Identification and Tokenization

Before applying letter casing, the input string must be split into isolated word segments. createCompounder passes the raw input to words(), which strips quotes and evaluates the string against specialized regular expressions.

Lodash determines whether the input string contains complex Unicode symbols using the internal hasUnicodeWord check:

  1. ASCII Path (reAsciiWord): If the string contains standard Latin characters and explicit hyphens, Lodash utilizes the ASCII word matcher:

    var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;

    In the ASCII table, the hyphen-minus character (-) has the character code 45 (hexadecimal \x2d). Because \x2d falls directly inside the excluded range \x00-\x2f, the regular expression refuses to match the hyphen. Instead, it matches the contiguous valid characters on either side, effectively slicing the string wherever a hyphen appears.

  2. Unicode Path (reUnicodeWord): If the hyphenated string contains extended Unicode characters, Lodash routes the string through a more complex regular expression pattern. This pattern explicitly recognizes various dash categories, punctuation marks, and whitespace boundaries, separating the adjacent character sets accordingly.

In both execution paths, the explicit hyphen is never captured as part of a word; it serves purely as an uncaptured boundary marker that dictates where one token terminates and the next begins.

Step 2: Array Generation

By running string.match(regex) using the delimiter rules above, Lodash dynamically flattens the compound structure into a JavaScript array of clean substrings.

For example, the evaluation of:

_.lowerCase('kebab-case-example');

yields the intermediate word array:

['kebab', 'case', 'example']

Step 3: Reduction and Casing

Once the hyphenated input has been deconstructed into discrete tokens, createCompounder iterates through the array using an internal reduction loop (arrayReduce).

During this reduction:

The hyphen is completely excised from memory, leaving the final reconstructed string: "kebab case example".