Lodash kebabCase: Formatting URL-Friendly Strings
The _.kebabCase function in the Lodash JavaScript
library converts strings into lowercase, hyphen-separated tokens
commonly known as kebab-case. This article explains how the method
analyzes input strings, handles word transitions caused by uppercase
letters, strips extraneous whitespace and punctuation, and joins the
remaining components into clean, URL-friendly slugs.
How _.kebabCase
Processes Text
Lodash achieves consistent kebab-case formatting through a multi-step sequence: word segmentation, case normalization, and delimiter joining.
1. Tokenizing Words and Detecting Uppercase Boundaries
Before modifying any letters, _.kebabCase breaks the
input string into individual word tokens using an internal regex-based
helper (words). The method identifies word boundaries using
specific patterns:
- camelCase and PascalCase: A lowercase letter
immediately followed by an uppercase letter indicates a split point
(e.g.,
userProfilebecomes['user', 'Profile']). - Consecutive Uppercase Letters (Acronyms): Sequences
of capital letters followed by a lowercase letter are split to preserve
word integrity (e.g.,
parseHTMLDocumentbecomes['parse', 'HTML', 'Document']). - Existing Delimiters: Spaces, underscores, and hyphens serve as explicit word boundaries.
2. Removing Punctuation and Whitespace
Characters that are not alphanumeric (such as exclamation marks, commas, quotes, and symbols) are omitted during the tokenization step. Whitespace sequences—whether single spaces, tabs, or multiple consecutive spaces—are discarded rather than preserved.
3. Normalizing to Lowercase
Once the string is broken into an array of discrete word tokens, every token is converted entirely to lowercase. This removes case sensitivity issues and ensures the final string conforms to web URL conventions.
4. Joining with Hyphens
The resulting array of lowercase words is joined together using a
single dash (-). Trailing, leading, or duplicated
separators are eliminated automatically because the joining occurs only
between extracted tokens.
Examples of String Transformations
The following examples demonstrate how _.kebabCase
handles different string patterns:
const _ = require('lodash');
// Converting camelCase and PascalCase
_.kebabCase('camelCaseString');
// Output: 'camel-case-string'
_.kebabCase('PascalCaseString');
// Output: 'pascal-case-string'
// Handling spaces and punctuation
_.kebabCase('Hello World! How are you?');
// Output: 'hello-world-how-are-you'
// Handling acronyms and multiple uppercase letters
_.kebabCase('getJSONResponseFromAPI');
// Output: 'get-json-response-from-api'
// Handling underscores and mixed separators
_.kebabCase('__USER_AUTH_TOKEN__');
// Output: 'user-auth-token'Why This Format Is Ideal for URLs
URLs cannot reliably contain spaces or arbitrary punctuation, and
mixed casing can cause indexing and routing problems on case-sensitive
servers. By decomposing strings into discrete terms, converting them to
lowercase, and uniting them with hyphens, _.kebabCase
produces predictable, human-readable, and search-engine-safe slugs.