-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaseConverters.ts
More file actions
161 lines (142 loc) · 4.54 KB
/
Copy pathcaseConverters.ts
File metadata and controls
161 lines (142 loc) · 4.54 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
export type CaseType =
| 'lowerWords'
| 'upperWords'
| 'camelCase'
| 'snakeCase'
| 'upperSnakeCase'
| 'kebabCase'
| 'trainCase';
/** Ordered cycle: each Alt+Shift+L advances to the next format */
export const CYCLE_ORDER: CaseType[] = [
'camelCase',
'snakeCase',
'kebabCase',
'upperSnakeCase',
'trainCase',
'lowerWords',
'upperWords',
];
/**
* Splits any cased string into an array of lowercase word tokens.
* Handles: camelCase, PascalCase, snake_case, UPPER_SNAKE, kebab-case,
* Train-Case, plain spaces, and digits adjacent to letters (e.g. hello2World).
*/
function segmentWords(text: string): string[] {
// Insert a separator before transitions: lowercase→uppercase, digit→letter,
// letter→digit, and consecutive uppercase followed by lowercase (ABCDef → ABC Def).
const spaced = text
.replace(/([a-z\d])([A-Z])/g, '$1 $2')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
.replace(/([a-zA-Z])(\d)/g, '$1 $2')
.replace(/(\d)([a-zA-Z])/g, '$1 $2');
return spaced
.split(/[\s\-_]+/)
.map((w) => w.trim())
.filter((w) => w.length > 0);
}
// ─── Converters ──────────────────────────────────────────────────────────────
/** Splits into words and joins with spaces, all lowercase. */
export function toLowerWords(text: string): string {
const words = segmentWords(text);
if (words.length === 0) {
return text;
}
return words.map((w) => w.toLowerCase()).join(' ');
}
export function toCamelCase(text: string): string {
const words = segmentWords(text);
if (words.length === 0) {
return text;
}
return words
.map((w, i) =>
i === 0 ? w.toLowerCase() : w[0].toUpperCase() + w.slice(1).toLowerCase(),
)
.join('');
}
export function toSnakeCase(text: string): string {
const words = segmentWords(text);
if (words.length === 0) {
return text;
}
return words.map((w) => w.toLowerCase()).join('_');
}
export function toUpperSnakeCase(text: string): string {
const words = segmentWords(text);
if (words.length === 0) {
return text;
}
return words.map((w) => w.toUpperCase()).join('_');
}
export function toKebabCase(text: string): string {
const words = segmentWords(text);
if (words.length === 0) {
return text;
}
return words.map((w) => w.toLowerCase()).join('-');
}
export function toUpperWords(text: string): string {
const words = segmentWords(text);
if (words.length === 0) {
return text;
}
return words.map((w) => w.toUpperCase()).join(' ');
}
export function toTrainCase(text: string): string {
const words = segmentWords(text);
if (words.length === 0) {
return text;
}
return words.map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase()).join('-');
}
// ─── Detection ───────────────────────────────────────────────────────────────
/**
* Detects the case format of a single token (no spaces).
* Returns null if the text doesn't clearly match any supported format,
* or if it's a single word with no separators (ambiguous).
*/
export function detectCase(text: string): CaseType | null {
const t = text.trim();
if (!t) {
return null;
}
// Space-separated formats (checked before no-space formats)
if (/^[a-z\d]+( [a-z\d]+)+$/.test(t)) {
return 'lowerWords';
}
if (/^[A-Z\d]+( [A-Z\d]+)+$/.test(t)) {
return 'upperWords';
}
if (/\s/.test(t)) {
return null;
}
if (/^[a-z][a-z\d]*([A-Z][a-z\d]*)+$/.test(t)) {
return 'camelCase';
}
if (/^[A-Z][a-z\d]*([A-Z][a-z\d]*)+$/.test(t)) {
return 'camelCase';
} // PascalCase → treat as camelCase for cycling
if (/^[a-z\d]+(_[a-z\d]+)+$/.test(t)) {
return 'snakeCase';
}
if (/^[A-Z\d]+(_[A-Z\d]+)+$/.test(t)) {
return 'upperSnakeCase';
}
if (/^[a-z\d]+(-[a-z\d]+)+$/.test(t)) {
return 'kebabCase';
}
if (/^[A-Z][a-z\d]*(-[A-Z][a-z\d]*)+$/.test(t)) {
return 'trainCase';
}
return null;
}
// ─── Converter map ───────────────────────────────────────────────────────────
export const CONVERTERS: Record<CaseType, (text: string) => string> = {
lowerWords: toLowerWords,
upperWords: toUpperWords,
camelCase: toCamelCase,
snakeCase: toSnakeCase,
upperSnakeCase: toUpperSnakeCase,
kebabCase: toKebabCase,
trainCase: toTrainCase,
};