/
names.ts
69 lines (64 loc) · 1.59 KB
/
names.ts
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
/**
* Util function to generate different strings based off the provided name.
*
* Examples:
*
* ```typescript
* names("my-name") // {name: 'my-name', className: 'MyName', propertyName: 'myName', constantName: 'MY_NAME', fileName: 'my-name'}
* names("myName") // {name: 'my-name', className: 'MyName', propertyName: 'myName', constantName: 'MY_NAME', fileName: 'my-name'}
* ```
* @param name
*/
export function names(name: string): {
name: string;
className: string;
propertyName: string;
constantName: string;
fileName: string;
} {
return {
name,
className: toClassName(name),
propertyName: toPropertyName(name),
constantName: toConstantName(name),
fileName: toFileName(name),
};
}
/**
* Hyphenated to UpperCamelCase
*/
function toClassName(str: string): string {
return toCapitalCase(toPropertyName(str));
}
/**
* Hyphenated to lowerCamelCase
*/
function toPropertyName(s: string): string {
return s
.replace(/([^a-zA-Z0-9])+(.)?/g, (_, __, chr) =>
chr ? chr.toUpperCase() : ''
)
.replace(/[^a-zA-Z\d]/g, '')
.replace(/^([A-Z])/, (m) => m.toLowerCase());
}
/**
* Hyphenated to CONSTANT_CASE
*/
function toConstantName(s: string): string {
return s.replace(/([^a-zA-Z0-9])/g, '_').toUpperCase();
}
/**
* Upper camelCase to lowercase, hyphenated
*/
function toFileName(s: string): string {
return s
.replace(/([a-z\d])([A-Z])/g, '$1_$2')
.toLowerCase()
.replace(/[ _]/g, '-');
}
/**
* Capitalizes the first letter of a string
*/
function toCapitalCase(s: string): string {
return s.charAt(0).toUpperCase() + s.slice(1);
}