toKebabCase
A utility function to convert a string into kebab-case format.> toKebabCase(str: string): string
- str (string): The input string to be converted to kebab-case. Can include camelCase, PascalCase, or any other mixed-case format with letters and numbers.
- Returns: A string in kebab-case format, where words are separated by hyphens (`-`) and all letters are lowercase.
Example
import { toKebabCase } from "@explita/daily-toolset";
// Convert camelCase to kebab-case
const result1 = toKebabCase("camelCaseExample");
console.log(result1); // Output: "camel-case-example"
// Convert PascalCase to kebab-case
const result2 = toKebabCase("PascalCaseExample");
console.log(result2); // Output: "pascal-case-example"
// Convert a mixed-case string with numbers
const result3 = toKebabCase("exampleString123WithNumbers");
console.log(result3); // Output: "example-string-123-with-numbers"
// Handle empty input
const result4 = toKebabCase("");
console.log(result4); // Output: ""
Use Cases
- Converting string identifiers to kebab-case for URL slugs or CSS class names.
- Normalizing variable names for consistency in a codebase or API responses.
- Preparing strings for file or database naming conventions that use kebab-case.