typedash
Version:
modern, type-safe collection of utility functions
27 lines (25 loc) • 771 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
Object.defineProperty(exports, 'constantCase', {
enumerable: true,
get: function () {
return constantCase;
}
});
//# sourceMappingURL=constantCase-BbzF3WeM.cjs.map