codetrix
Version:
A lightweight lodash-style utility library
17 lines (16 loc) • 377 B
JavaScript
/**
* Converts a string to kebab-case format.
*
* @param str - The input string.
* @returns The kebab-cased string.
*
* @example
* kebabCase('Hello World!'); // 'hello-world'
*/
export function kebabCase(str) {
return str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[\s_]+/g, '-')
.replace(/[^a-zA-Z0-9-]/g, '')
.toLowerCase();
}