Lodash _.upperCase on Stringified Array Acronyms

This article provides an in-depth technical breakdown of how Lodash's _.upperCase method parses, tokenizes, and formats spaced acronyms embedded within explicitly stringified arrays. It explores the internal sequence of operations—from string coercion and regular expression-based word boundary detection to final case normalization—contrasting direct array-to-string passing with dynamically mapped array elements.


Understanding the Lodash _.upperCase Pipeline

Lodash implements _.upperCase using an internal factory function called createCompounder. This compounder standardizes input by stripping non-word characters, identifying word boundaries, and joining the normalized tokens with a space. The underlying execution flow consists of three primary steps:

  1. String Coercion: If the input is not a primitive string, Lodash calls internal casting utilities (toString) to convert the input into a string.
  2. Tokenization via _.words: Lodash segments the coerced string into an array of discrete words using a regular expression designed for Unicode and ASCII boundaries.
  3. Reduction and Capitalization: The extracted tokens are joined with a single space separator, and each character is converted to its uppercase equivalent using standard JavaScript string mapping (toUpperCase).

Tokenization of Explicitly Stringified Arrays

When an array containing spaced acronyms is explicitly stringified—either via JSON.stringify() or Array.prototype.toString()—it introduces structural syntax such as square brackets, quotes, and commas:

const stringified = JSON.stringify(["N A S A", "E S A"]);
// Output: "[\"N A S A\",\"E S A\"]"

When passed directly into _.upperCase(stringified), the method treats the entire string representation as a single input stream. Lodash's internal words pattern targets word-forming characters ([a-zA-Z0-9]+ in ASCII contexts, or complex Unicode word ranges). Consequently:

Evaluation via Dynamic Mapping

When processing arrays dynamically, developers typically apply _.upperCase across an array via Array.prototype.map():

const data = ["N A S A", "F B I"];
const processed = data.map(item => _.upperCase(String(item)));

In this context, each array entry is coerced to a string individually rather than serialized as an array literal.

Because _.upperCase standardizes multiple spaces into single spaces and strips leading or trailing whitespace, spaced acronyms such as "N A S A" (with uneven padding) are normalized into single-spaced uppercase characters: "N A S A". The dynamic map preserves the array boundary, yielding ["N A S A", "F B I"] instead of collapsing the entire collection into a single concatenated string.

Handling Continuous vs. Spaced Acronyms

Lodash's regex incorporates split heuristics for camelCase and PascalCase boundaries (e.g., detecting changes between lowercase and uppercase characters). However, for spaced acronyms: