UNPKG

typedash

Version:

modern, type-safe collection of utility functions

24 lines 1.74 kB
//#region src/functions/startCase/startCase.d.ts /** * Converts a string to start case (first letter of each word capitalized, joined by spaces). * @param string The input string to convert to start case. * @returns A new string converted to start case. * @example * ```ts * startCase('--foo-bar--') // 'Foo Bar' * startCase('fooBar') // 'Foo Bar' * startCase('__FOO_BAR__') // 'FOO BAR' * ``` */ declare function startCase<S extends string>(string: S): StartCase<S>; type IsUpperLetter<C extends string> = C extends Uppercase<C> ? (C extends Lowercase<C> ? false : true) : false; type TrimTrailingSpaces<S extends string> = S extends `${infer L} ` ? TrimTrailingSpaces<L> : S; type StartCaseImpl<S extends string, PrevIsUpper extends boolean = false, AtWordStart extends boolean = true> = S extends `${infer C}${infer Rest}` ? C extends '-' | '_' | ' ' ? `${AtWordStart extends true ? '' : ' '}${StartCaseImpl<Rest, false, true>}` : AtWordStart extends true ? `${Uppercase<C>}${StartCaseImpl<Rest, IsUpperLetter<C>, false>}` : IsUpperLetter<C> extends true ? PrevIsUpper extends true ? Rest extends `${infer Next}${string}` ? Next extends '-' | '_' | ' ' ? `${C}${StartCaseImpl<Rest, true, false>}` : IsUpperLetter<Next> extends true ? `${C}${StartCaseImpl<Rest, true, false>}` : ` ${C}${StartCaseImpl<Rest, true, false>}` : `${C}${StartCaseImpl<Rest, true, false>}` : ` ${C}${StartCaseImpl<Rest, true, false>}` : `${C}${StartCaseImpl<Rest, false, false>}` : ''; /** * Converts a string to start case at the type level. * @see {@link startCase}. */ type StartCase<S extends string> = TrimTrailingSpaces<StartCaseImpl<S>>; //#endregion export { startCase as n, StartCase as t }; //# sourceMappingURL=startCase-CcCLrLny.d.cts.map