@rbxts/jsnatives
toKebabCase
Converts a string to kebab-case format.
Signature
function toKebabCase(str: string): string
Description
The toKebabCase
function converts a string to kebab-case format. It replaces spaces, underscores, and other separators with hyphens, and transforms the string into a format where all letters are lowercase with words separated by hyphens.
Parameters
str
: The string to convert to kebab-case.
Return value
- A new string in kebab-case format.
Examples
Basic usage
// Converting different formats to kebab-caseconst space = String.toKebabCase("hello world");print(space); // Outputs: "hello-world"
const camel = String.toKebabCase("helloWorld");print(camel); // Outputs: "hello-world"
const pascal = String.toKebabCase("HelloWorld");print(pascal); // Outputs: "hello-world"
const snake = String.toKebabCase("hello_world");print(snake); // Outputs: "hello-world"
Multiple words
// Converting multi-word stringsconst multiple = String.toKebabCase("hello beautiful world");print(multiple); // Outputs: "hello-beautiful-world"
const mixedSeparators = String.toKebabCase("hello_beautiful-world");print(mixedSeparators); // Outputs: "hello-beautiful-world"
Handling edge cases
// Empty stringconst empty = String.toKebabCase("");print(empty); // Outputs: ""
// Single wordconst single = String.toKebabCase("hello");print(single); // Outputs: "hello"
// Already in kebab-caseconst alreadyKebab = String.toKebabCase("hello-world");print(alreadyKebab); // Outputs: "hello-world"
// Uppercaseconst uppercase = String.toKebabCase("HELLO WORLD");print(uppercase); // Outputs: "h-e-l-l-o-w-o-r-l-d" (each uppercase is treated as new word)
Converting from other cases
// From sentence caseconst sentence = String.toKebabCase("This is a sentence");print(sentence); // Outputs: "this-is-a-sentence"
// From title caseconst title = String.toKebabCase("This Is A Title");print(title); // Outputs: "this-is-a-title"
// From camelCaseconst camel = String.toKebabCase("userFirstName");print(camel); // Outputs: "user-first-name"
// From PascalCaseconst pascal = String.toKebabCase("UserLoginRequest");print(pascal); // Outputs: "user-login-request"
// From snake_caseconst snake = String.toKebabCase("user_login_request");print(snake); // Outputs: "user-login-request"
Practical applications
// Create CSS class namesfunction createClassName(name: string): string { return String.toKebabCase(name);}
print(createClassName("ButtonPrimary")); // Outputs: "button-primary"print(createClassName("Header Section")); // Outputs: "header-section"
// Create URL slugsfunction createSlug(title: string): string { return String.toKebabCase(title);}
print(createSlug("Getting Started with TypeScript")); // Outputs: "getting-started-with-typescript"print(createSlug("10 Best Practices for Clean Code")); // Outputs: "10-best-practices-for-clean-code"