typedash
Version:
modern, type-safe collection of utility functions
21 lines (20 loc) • 676 B
JavaScript
//#region src/functions/constantCase/constantCase.ts
/**
* Changes the casing of a string to constant case.
* @param string The input string to change the casing of.
* @returns A new string with the casing changed to constant case.
* @example
* ```ts
* constantCase('fooBar') // 'FOO_BAR'
* constantCase('foo bar') // 'FOO_BAR'
* constantCase('foo-bar') // 'FOO_BAR'
* constantCase('fooBar42') // 'FOO_BAR42'
* ```
*/
function constantCase(string) {
if (!/[a-z]+/i.test(string)) return "";
return string.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[\s-]+/g, "_").toUpperCase();
}
//#endregion
export { constantCase as t };
//# sourceMappingURL=constantCase-DUVPSf7U.js.map