1 import {WordSeparators} from '../source/utilities';
4 Recursively split a string literal into two parts on the first occurence of the given string, returning an array literal of all the separate parts.
6 export type Split<S extends string, D extends string> =
7 string extends S ? string[] :
9 S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] :
13 Step by step takes the first item in an array literal, formats it and adds it to a string literal, and then recursively appends the remainder.
15 Only to be used by `CamelCaseStringArray<>`.
17 @see CamelCaseStringArray
19 type InnerCamelCaseStringArray<Parts extends any[], PreviousPart> =
20 Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
21 ? FirstPart extends undefined
23 : FirstPart extends ''
24 ? InnerCamelCaseStringArray<RemainingParts, PreviousPart>
25 : `${PreviousPart extends '' ? FirstPart : Capitalize<FirstPart>}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}`
29 Starts fusing the output of `Split<>`, an array literal of strings, into a camel-cased string literal.
31 It's separate from `InnerCamelCaseStringArray<>` to keep a clean API outwards to the rest of the code.
35 type CamelCaseStringArray<Parts extends string[]> =
36 Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
37 ? Uncapitalize<`${FirstPart}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}`>
41 Convert a string literal to camel-case.
43 This can be useful when, for example, converting some kebab-cased command-line flags or a snake-cased database result.
47 import {CamelCase} from 'type-fest';
51 const someVariable: CamelCase<'foo-bar'> = 'fooBar';
55 type CamelCasedProps<T> = {
56 [K in keyof T as CamelCase<K>]: T[K]
59 interface RawOptions {
61 'full_family_name': string;
65 const dbResult: CamelCasedProps<ModelProps> = {
67 fullFamilyName: 'bar.js',
72 export type CamelCase<K> = K extends string ? CamelCaseStringArray<Split<K, WordSeparators>> : K;