1 | import type {SplitWords} from './split-words';
|
2 |
|
3 | /**
|
4 | CamelCase options.
|
5 |
|
6 | @see {@link CamelCase}
|
7 | */
|
8 | export type CamelCaseOptions = {
|
9 | /**
|
10 | Whether to preserved consecutive uppercase letter.
|
11 |
|
12 | @default true
|
13 | */
|
14 | preserveConsecutiveUppercase?: boolean;
|
15 | };
|
16 |
|
17 | /**
|
18 | Convert an array of words to camel-case.
|
19 | */
|
20 | type CamelCaseFromArray<
|
21 | Words extends string[],
|
22 | Options extends CamelCaseOptions,
|
23 | OutputString extends string = '',
|
24 | > = Words extends [
|
25 | infer FirstWord extends string,
|
26 | ...infer RemainingWords extends string[],
|
27 | ]
|
28 | ? Options['preserveConsecutiveUppercase'] extends true
|
29 | ? `${Capitalize<FirstWord>}${CamelCaseFromArray<RemainingWords, Options>}`
|
30 | : `${Capitalize<Lowercase<FirstWord>>}${CamelCaseFromArray<RemainingWords, Options>}`
|
31 | : OutputString;
|
32 |
|
33 | /**
|
34 | Convert a string literal to camel-case.
|
35 |
|
36 | This can be useful when, for example, converting some kebab-cased command-line flags or a snake-cased database result.
|
37 |
|
38 | By default, consecutive uppercase letter are preserved. See {@link CamelCaseOptions.preserveConsecutiveUppercase preserveConsecutiveUppercase} option to change this behaviour.
|
39 |
|
40 | @example
|
41 | ```
|
42 | import type {CamelCase} from 'type-fest';
|
43 |
|
44 | // Simple
|
45 |
|
46 | const someVariable: CamelCase<'foo-bar'> = 'fooBar';
|
47 |
|
48 | // Advanced
|
49 |
|
50 | type CamelCasedProperties<T> = {
|
51 | [K in keyof T as CamelCase<K>]: T[K]
|
52 | };
|
53 |
|
54 | interface RawOptions {
|
55 | 'dry-run': boolean;
|
56 | 'full_family_name': string;
|
57 | foo: number;
|
58 | BAR: string;
|
59 | QUZ_QUX: number;
|
60 | 'OTHER-FIELD': boolean;
|
61 | }
|
62 |
|
63 | const dbResult: CamelCasedProperties<RawOptions> = {
|
64 | dryRun: true,
|
65 | fullFamilyName: 'bar.js',
|
66 | foo: 123,
|
67 | bar: 'foo',
|
68 | quzQux: 6,
|
69 | otherField: false
|
70 | };
|
71 | ```
|
72 |
|
73 | @category Change case
|
74 | @category Template literal
|
75 | */
|
76 | export type CamelCase<Type, Options extends CamelCaseOptions = {preserveConsecutiveUppercase: true}> = Type extends string
|
77 | ? string extends Type
|
78 | ? Type
|
79 | : Uncapitalize<CamelCaseFromArray<SplitWords<Type extends Uppercase<Type> ? Lowercase<Type> : Type>, Options>>
|
80 | : Type;
|