typedash
Version:
modern, type-safe collection of utility functions
21 lines (19 loc) • 825 B
TypeScript
/**
* 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'
* ```
*/
declare function constantCase<S extends string>(string: S): ConstantCase<S>;
/**
* Changes the casing of a string to constant case.
* @see {@link constantCase}.
*/
type ConstantCase<S extends string> = Uppercase<S extends `${infer S1}${infer S2}` ? `${S1 extends '-' ? '_' : S1 extends ' ' ? '_' : S1 extends '_' ? '_' : S1 extends Capitalize<S1> ? `_${Lowercase<S1>}` : S1}${ConstantCase<S2>}` : S>;
export { type ConstantCase, constantCase };