Repository navigation
Expand file tree
/
Copy pathpascal-case.d.ts
More file actions
53 lines (42 loc) · 1.45 KB
/
Copy pathpascal-case.d.ts
File metadata and controls
53 lines (42 loc) · 1.45 KB
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
import type {CamelCase, CamelCaseOptions} from './camel-case.d.ts';
import type {IsStringLiteral} from './is-string-literal.d.ts';
/**
Convert a string literal to pascal-case.
Note: `PascalCase` capitalizes the result of `CamelCase`, and `Capitalize` does nothing when the first character has no uppercase form. An underscore has none, so the `preserveLeadingUnderscores` option also turns off the Pascal casing: `PascalCase<'_foo', {preserveLeadingUnderscores: true}>` is `'_foo'`, not `'_Foo'`.
@example
```
import type {PascalCase} from 'type-fest';
// Simple
const someVariable: PascalCase<'foo-bar'> = 'FooBar';
const preserveConsecutiveUppercase: PascalCase<'foo-BAR-baz', {preserveConsecutiveUppercase: true}> = 'FooBARBaz';
const splitOnPunctuation: PascalCase<'foo-bar>>baz', {splitOnPunctuation: true}> = 'FooBarBaz';
// Advanced
type PascalCasedProperties<T> = {
[K in keyof T as PascalCase<K>]: T[K]
};
type RawOptions = {
'dry-run': boolean;
'full_family_name': string;
foo: number;
BAR: string;
QUZ_QUX: number;
'OTHER-FIELD': boolean;
};
const dbResult: PascalCasedProperties<RawOptions> = {
DryRun: true,
FullFamilyName: 'bar.js',
Foo: 123,
Bar: 'foo',
QuzQux: 6,
OtherField: false,
};
```
@category Change case
@category Template literal
*/
export type PascalCase<Value, Options extends CamelCaseOptions = {}> = Value extends string
? IsStringLiteral<Value> extends false
? Value
: Capitalize<CamelCase<Value, Options>>
: Value;
export {};