-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasing.test.ts
More file actions
295 lines (235 loc) · 8.93 KB
/
casing.test.ts
File metadata and controls
295 lines (235 loc) · 8.93 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import {
lcFirst,
ucFirst,
isValidIdentifier,
isValidIdentifierCamelized,
toCamelCase,
toPascalCase,
toSnakeCase,
toKebabCase,
toConstantCase,
} from '../src';
it('should convert strings to PascalCase', () => {
expect(toPascalCase('hello_world')).toBe('HelloWorld');
expect(toPascalCase('hello world')).toBe('HelloWorld');
expect(toPascalCase('hello-world')).toBe('HelloWorld');
expect(toPascalCase('Hello-World')).toBe('HelloWorld');
});
it('should convert strings to camelCase', () => {
expect(toCamelCase('hello_world')).toBe('helloWorld');
expect(toCamelCase('hello world')).toBe('helloWorld');
expect(toCamelCase('hello-world')).toBe('helloWorld');
expect(toCamelCase('Hello-World')).toBe('helloWorld');
expect(toCamelCase('_hello_world')).toBe('helloWorld');
expect(toCamelCase('123hello_world')).toBe('123helloWorld');
});
it('should validate valid JavaScript identifiers', () => {
expect(isValidIdentifier('validIdentifier')).toBe(true);
expect(isValidIdentifier('$validIdentifier')).toBe(true);
expect(isValidIdentifier('_validIdentifier')).toBe(true);
expect(isValidIdentifier('1invalidIdentifier')).toBe(false);
expect(isValidIdentifier('invalid-Identifier')).toBe(false);
});
it('should validate valid JavaScript-like identifiers allowing internal hyphens', () => {
expect(isValidIdentifierCamelized('valid-identifier')).toBe(true);
expect(isValidIdentifierCamelized('$valid-identifier')).toBe(true);
expect(isValidIdentifierCamelized('_valid-identifier')).toBe(true);
expect(isValidIdentifierCamelized('1invalid-identifier')).toBe(false);
expect(isValidIdentifierCamelized('-invalid-identifier')).toBe(false);
expect(isValidIdentifierCamelized('invalid-identifier-')).toBe(true);
});
describe('lcFirst', () => {
test('lowercases the first character', () => {
expect(lcFirst('UserProfile')).toBe('userProfile');
expect(lcFirst('User')).toBe('user');
expect(lcFirst('ABC')).toBe('aBC');
});
test('handles already lowercase strings', () => {
expect(lcFirst('user')).toBe('user');
});
test('handles single character', () => {
expect(lcFirst('A')).toBe('a');
expect(lcFirst('a')).toBe('a');
});
});
describe('ucFirst', () => {
test('uppercases the first character', () => {
expect(ucFirst('userProfile')).toBe('UserProfile');
expect(ucFirst('user')).toBe('User');
expect(ucFirst('abc')).toBe('Abc');
});
test('handles already uppercase strings', () => {
expect(ucFirst('User')).toBe('User');
});
test('handles single character', () => {
expect(ucFirst('a')).toBe('A');
expect(ucFirst('A')).toBe('A');
});
});
describe('toPascalCase', () => {
test('converts normal string', () => {
expect(toPascalCase('hello_world')).toBe('HelloWorld');
});
test('converts string with multiple underscores and mixed case', () => {
expect(toPascalCase('Object_ID')).toBe('ObjectID');
expect(toPascalCase('Postgre_SQL_View')).toBe('PostgreSQLView');
expect(toPascalCase('postgre_sql_view')).toBe('PostgreSqlView');
});
test('handles string with multiple separators together', () => {
expect(toPascalCase('hello___world--great')).toBe('HelloWorldGreat');
});
test('handles consecutive mixed separators', () => {
expect(toPascalCase('my__double_under')).toBe('MyDoubleUnder');
expect(toPascalCase('my-_mixed-_sep')).toBe('MyMixedSep');
expect(toPascalCase('my--double-dash')).toBe('MyDoubleDash');
});
test('handles leading separators', () => {
expect(toPascalCase('_private')).toBe('Private');
expect(toPascalCase('__double')).toBe('Double');
expect(toPascalCase('-leading')).toBe('Leading');
});
test('handles single word', () => {
expect(toPascalCase('word')).toBe('Word');
});
test('handles empty string', () => {
expect(toPascalCase('')).toBe('');
});
test('handles string with numbers', () => {
expect(toPascalCase('version1_2_3')).toBe('Version123');
});
test('handles spaces', () => {
expect(toPascalCase('my table name')).toBe('MyTableName');
expect(toPascalCase('My Table Name')).toBe('MyTableName');
});
});
describe('toCamelCase', () => {
test('converts hyphenated string', () => {
expect(toCamelCase('hello-world')).toBe('helloWorld');
});
test('converts underscored string', () => {
expect(toCamelCase('hello_world')).toBe('helloWorld');
});
test('converts spaces', () => {
expect(toCamelCase('hello world')).toBe('helloWorld');
});
test('handles mixed separators', () => {
expect(toCamelCase('hello-world_now what')).toBe('helloWorldNowWhat');
});
test('handles empty string', () => {
expect(toCamelCase('')).toBe('');
});
test('handles string starting with separators', () => {
expect(toCamelCase('-hello_world')).toBe('helloWorld');
});
test('handles string with multiple separators together', () => {
expect(toCamelCase('hello___world--great')).toBe('helloWorldGreat');
});
});
describe('toSnakeCase', () => {
test('converts camelCase to snake_case', () => {
expect(toSnakeCase('helloWorld')).toBe('hello_world');
});
test('converts PascalCase to snake_case', () => {
expect(toSnakeCase('HelloWorld')).toBe('hello_world');
});
test('converts kebab-case to snake_case', () => {
expect(toSnakeCase('hello-world')).toBe('hello_world');
});
test('converts space separated to snake_case', () => {
expect(toSnakeCase('hello world')).toBe('hello_world');
});
test('handles mixed case with numbers', () => {
expect(toSnakeCase('version2APIKey')).toBe('version_2_api_key');
});
test('handles already snake_case strings', () => {
expect(toSnakeCase('hello_world')).toBe('hello_world');
});
test('handles multiple separators together', () => {
expect(toSnakeCase('hello___world--great')).toBe('hello_world_great');
});
test('handles empty string', () => {
expect(toSnakeCase('')).toBe('');
});
test('removes leading and trailing underscores', () => {
expect(toSnakeCase('_hello_world_')).toBe('hello_world');
});
test('handles single word', () => {
expect(toSnakeCase('hello')).toBe('hello');
});
test('handles consecutive capitals', () => {
expect(toSnakeCase('HTTPSConnection')).toBe('https_connection');
});
});
describe('toKebabCase', () => {
test('converts camelCase to kebab-case', () => {
expect(toKebabCase('helloWorld')).toBe('hello-world');
});
test('converts PascalCase to kebab-case', () => {
expect(toKebabCase('HelloWorld')).toBe('hello-world');
});
test('converts snake_case to kebab-case', () => {
expect(toKebabCase('hello_world')).toBe('hello-world');
});
test('converts space separated to kebab-case', () => {
expect(toKebabCase('hello world')).toBe('hello-world');
});
test('handles mixed case with numbers', () => {
expect(toKebabCase('version2APIKey')).toBe('version-2-api-key');
});
test('handles already kebab-case strings', () => {
expect(toKebabCase('hello-world')).toBe('hello-world');
});
test('handles multiple separators together', () => {
expect(toKebabCase('hello___world--great')).toBe('hello-world-great');
});
test('handles empty string', () => {
expect(toKebabCase('')).toBe('');
});
test('removes leading and trailing hyphens', () => {
expect(toKebabCase('-hello-world-')).toBe('hello-world');
});
test('handles single word', () => {
expect(toKebabCase('hello')).toBe('hello');
});
test('handles consecutive capitals', () => {
expect(toKebabCase('HTTPSConnection')).toBe('https-connection');
});
});
describe('toConstantCase', () => {
test('converts camelCase to CONSTANT_CASE', () => {
expect(toConstantCase('helloWorld')).toBe('HELLO_WORLD');
});
test('converts PascalCase to CONSTANT_CASE', () => {
expect(toConstantCase('HelloWorld')).toBe('HELLO_WORLD');
});
test('converts kebab-case to CONSTANT_CASE', () => {
expect(toConstantCase('hello-world')).toBe('HELLO_WORLD');
});
test('converts snake_case to CONSTANT_CASE', () => {
expect(toConstantCase('hello_world')).toBe('HELLO_WORLD');
});
test('converts space separated to CONSTANT_CASE', () => {
expect(toConstantCase('hello world')).toBe('HELLO_WORLD');
});
test('handles mixed case with numbers', () => {
expect(toConstantCase('version2APIKey')).toBe('VERSION_2_API_KEY');
});
test('handles already CONSTANT_CASE strings', () => {
expect(toConstantCase('HELLO_WORLD')).toBe('HELLO_WORLD');
});
test('handles multiple separators together', () => {
expect(toConstantCase('hello___world--great')).toBe('HELLO_WORLD_GREAT');
});
test('handles empty string', () => {
expect(toConstantCase('')).toBe('');
});
test('removes leading and trailing underscores', () => {
expect(toConstantCase('_hello_world_')).toBe('HELLO_WORLD');
});
test('handles single word', () => {
expect(toConstantCase('hello')).toBe('HELLO');
});
test('handles consecutive capitals', () => {
expect(toConstantCase('HTTPSConnection')).toBe('HTTPS_CONNECTION');
});
});