kebabCase()
kebabCase(
str):string
Converts a string to kebab-case.
Handles camelCase, PascalCase, acronyms, snake_case, and spaces.
Parametersβ
str: stringβ
The string to convert.
Returns: stringβ
The string in kebab-case.
Sinceβ
2.0.0
Performanceβ
O(n) where n is string length. Single regex pass with match + join.
Also known asβ
dash (Radashi) Β· kebabCase (Lodash, es-toolkit, Modern Dash) Β· toKebabCase (Remeda) Β· β (Ramda, Effect, Antfu)
Exampleβ
kebabCase('backgroundColor'); // => 'background-color'
kebabCase('XMLHttpRequest'); // => 'xml-http-request'
kebabCase('hello world'); // => 'hello-world'
kebabCase('foo_bar'); // => 'foo-bar'
kebabCase('--foo--bar--'); // => 'foo-bar'
How it works?β
Converts a string to kebab-case format. All lowercase, words separated by hyphens.
Conversion Examplesβ
| Input | Output |
|---|---|
backgroundColor | background-color |
XMLHttpRequest | xml-http-request |
hello world | hello-world |
foo_bar | foo-bar |
Processβ
Use Casesβ
Format CSS classes πβ
Convert strings to kebab-case for CSS class names or URL slugs.
Essential for frontend development and SEO-friendly URLs.
const className = kebabCase('MenuList'); // 'menu-list'
const slug = kebabCase('Hello World Post'); // 'hello-world-post'
Generate URL slugs from titlesβ
Convert article or product titles into SEO-friendly URL slugs. Essential for blogs, CMS platforms, and any content with human-readable URLs.
const articleTitle = "How to Build a REST API with TypeScript";
const slug = kebabCase(articleTitle);
// => "how-to-build-a-rest-api-with-type-script"
// Note: kebabCase splits on casing boundaries, so "TypeScript" becomes "type-script".
// For proper slugs with brand names, prefer deburr + manual regex: title.toLowerCase().replace(/\s+/g, "-")
const url = `/blog/${slug}`;
// => "/blog/how-to-build-a-rest-api-with-type-script"
Generate CSS class names from component propsβ
Convert component variant names to CSS class names. Essential for design system components with dynamic styling.
const getClassName = (variant: string, size: string) =>
`btn-${kebabCase(variant)}-${kebabCase(size)}`;
getClassName("primaryOutline", "extraLarge");
// => "btn-primary-outline-extra-large"
Create accessible anchor IDs from headingsβ
Generate URL-safe anchor IDs from heading text for table of contents. Essential for documentation sites and blog posts with deep linking.
const headings = ["Getting Started", "API Reference", "Error Handling Best Practices"];
const toc = headings.map((text) => ({
text,
id: kebabCase(text),
href: `#${kebabCase(text)}`,
}));
// => [{ text: "Getting Started", id: "getting-started", href: "#getting-started" }, ...]