import {WordSeparators} from '../source/utilities'; /** 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. */ export type Split = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split] : [S]; /** 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. Only to be used by `CamelCaseStringArray<>`. @see CamelCaseStringArray */ type InnerCamelCaseStringArray = Parts extends [`${infer FirstPart}`, ...infer RemainingParts] ? FirstPart extends undefined ? '' : FirstPart extends '' ? InnerCamelCaseStringArray : `${PreviousPart extends '' ? FirstPart : Capitalize}${InnerCamelCaseStringArray}` : ''; /** Starts fusing the output of `Split<>`, an array literal of strings, into a camel-cased string literal. It's separate from `InnerCamelCaseStringArray<>` to keep a clean API outwards to the rest of the code. @see Split */ type CamelCaseStringArray = Parts extends [`${infer FirstPart}`, ...infer RemainingParts] ? Uncapitalize<`${FirstPart}${InnerCamelCaseStringArray}`> : never; /** Convert a string literal to camel-case. This can be useful when, for example, converting some kebab-cased command-line flags or a snake-cased database result. @example ``` import {CamelCase} from 'type-fest'; // Simple const someVariable: CamelCase<'foo-bar'> = 'fooBar'; // Advanced type CamelCasedProps = { [K in keyof T as CamelCase]: T[K] }; interface RawOptions { 'dry-run': boolean; 'full_family_name': string; foo: number; } const dbResult: CamelCasedProps = { dryRun: true, fullFamilyName: 'bar.js', foo: 123 }; ``` */ export type CamelCase = K extends string ? CamelCaseStringArray> : K;