保誠-保戶業務員媒合平台
Tomas
2022-05-19 957a1f10a06fdbb76f1a0ba94fe44126c613fee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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<S extends string, D extends string> =
    string extends S ? string[] :
    S extends '' ? [] :
    S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] :
    [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 any[], PreviousPart> =
    Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
        ? FirstPart extends undefined
            ? ''
            : FirstPart extends ''
                    ? InnerCamelCaseStringArray<RemainingParts, PreviousPart>
                    : `${PreviousPart extends '' ? FirstPart : Capitalize<FirstPart>}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}`
        : '';
 
/**
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 string[]> =
    Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
        ? Uncapitalize<`${FirstPart}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}`>
        : 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<T> = {
    [K in keyof T as CamelCase<K>]: T[K]
};
 
interface RawOptions {
    'dry-run': boolean;
    'full_family_name': string;
    foo: number;
}
 
const dbResult: CamelCasedProps<ModelProps> = {
    dryRun: true,
    fullFamilyName: 'bar.js',
    foo: 123
};
```
*/
export type CamelCase<K> = K extends string ? CamelCaseStringArray<Split<K, WordSeparators>> : K;