typedash
Version:
modern, type-safe collection of utility functions
28 lines (26 loc) • 804 B
JavaScript
//#region src/functions/kebabCase/kebabCase.ts
/**
* Changes the casing of a string to kebab case.
* @param string The input string to change the casing of.
* @returns A new string with the casing changed to kebab case.
* @example
* ```ts
* kebabCase('fooBar') // 'foo-bar'
* kebabCase('foo bar') // 'foo-bar'
* kebabCase('foo-bar') // 'foo-bar'
* kebabCase('fooBar42') // 'foo-bar42'
* ```
*/
function kebabCase(string) {
if (!/[a-z]+/i.test(string)) return string;
return string.match(KEBAB_REGEX)?.map((x) => x.toLowerCase()).join("-");
}
const KEBAB_REGEX = /[A-Z]{2,}(?=[A-Z][a-z]+\d*|\b)|[A-Z]?[a-z]+\d*|[A-Z]|\d+/g;
//#endregion
Object.defineProperty(exports, 'kebabCase', {
enumerable: true,
get: function () {
return kebabCase;
}
});
//# sourceMappingURL=kebabCase-FvqbOvFa.cjs.map