utils/ts-src/kebabCase.ts

24 lines
607 B
TypeScript

/**
* Turn a string into a kebab-case string
* @param inval
*
* @example
* U.kebabCase('test string') // => 'test-string'
* U.kebabCase('testString') // => 'test-string'
* U.kebabCase('test_string') // => 'test-string'
*/
export function kebabCase(inval: string | null): string {
const tempString = (inval) ? inval : '';
const workArray = tempString.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g);
let workString: string = '';
if (workArray && workArray.length > 0) {
workString = workArray!.join('-').toLowerCase();
}
return workString;
}