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:
- String Coercion: If the input is not a primitive
string, Lodash calls internal casting utilities (
toString) to convert the input into a string. - Tokenization via
_.words: Lodash segments the coerced string into an array of discrete words using a regular expression designed for Unicode and ASCII boundaries. - 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:
- Brackets (
[and]), quotation marks ("), and commas (,) are categorized as delimiters and discarded. - Spaced acronym letters are treated as independent, single-letter tokens because the whitespace separates them just as it would distinct words.
- The resulting array of tokens for
JSON.stringify(["N A S A", "E S A"])becomes['N', 'A', 'S', 'A', 'E', 'S', 'A']. - Rejoining these tokens yields a single space-separated output:
"N A S A E S A".
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:
- No Case-Shift Boundaries: Because spaced acronyms
already contain explicit whitespace separators (
\s+), Lodash does not need to invoke its camelCase boundary-splitting logic. - Isolated Character Handling: Each letter bounded by
spaces is processed as an individual token. Lodash makes no semantic
distinction between a single-character word (like
"A") and a spaced acronym letter (like"U"in"U S A"). - Idempotent Casing: Applying
_.upperCaseto an already capitalized, single-spaced acronym returns the identical character sequence, effectively acting as an idempotent whitespace-sanitizing operation.