UNPKG

1.94 kBTypeScriptView Raw
1import type {SplitWords} from './split-words';
2
3/**
4CamelCase options.
5
6@see {@link CamelCase}
7*/
8export type CamelCaseOptions = {
9 /**
10 Whether to preserved consecutive uppercase letter.
11
12 @default true
13 */
14 preserveConsecutiveUppercase?: boolean;
15};
16
17/**
18Convert an array of words to camel-case.
19*/
20type 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/**
34Convert a string literal to camel-case.
35
36This can be useful when, for example, converting some kebab-cased command-line flags or a snake-cased database result.
37
38By default, consecutive uppercase letter are preserved. See {@link CamelCaseOptions.preserveConsecutiveUppercase preserveConsecutiveUppercase} option to change this behaviour.
39
40@example
41```
42import type {CamelCase} from 'type-fest';
43
44// Simple
45
46const someVariable: CamelCase<'foo-bar'> = 'fooBar';
47
48// Advanced
49
50type CamelCasedProperties<T> = {
51 [K in keyof T as CamelCase<K>]: T[K]
52};
53
54interface 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
63const 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*/
76export 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;