-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinflection.test.ts
More file actions
329 lines (283 loc) · 11.3 KB
/
inflection.test.ts
File metadata and controls
329 lines (283 loc) · 11.3 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import {
singularize,
pluralize,
singularizeLast,
pluralizeLast,
distinctPluralize,
distinctPluralizeLast,
lcFirst,
ucFirst,
toCamelCase,
toPascalCase,
toScreamingSnake,
underscore,
fixCapitalisedPlural,
toFieldName,
toQueryName,
} from '../src';
describe('singularize', () => {
it('should singularize regular words', () => {
expect(singularize('Users')).toBe('User');
expect(singularize('users')).toBe('user');
expect(singularize('People')).toBe('Person');
expect(singularize('people')).toBe('person');
expect(singularize('Categories')).toBe('Category');
});
it('should handle Latin suffix overrides', () => {
expect(singularize('Schemata')).toBe('Schema');
expect(singularize('schemata')).toBe('schema');
expect(singularize('Criteria')).toBe('Criterion');
expect(singularize('criteria')).toBe('criterion');
expect(singularize('Phenomena')).toBe('Phenomenon');
expect(singularize('Media')).toBe('Medium');
expect(singularize('Memoranda')).toBe('Memorandum');
expect(singularize('Strata')).toBe('Stratum');
expect(singularize('Curricula')).toBe('Curriculum');
expect(singularize('Data')).toBe('Datum');
});
it('should handle compound words with Latin suffixes', () => {
expect(singularize('ApiSchemata')).toBe('ApiSchema');
expect(singularize('UserMedia')).toBe('UserMedium');
expect(singularize('TestCriteria')).toBe('TestCriterion');
});
it('should preserve case of suffix', () => {
expect(singularize('apiSchemata')).toBe('apiSchema');
expect(singularize('SCHEMATA')).toBe('SCHEMA');
});
it('should singularize compound *base words correctly', () => {
expect(singularize('codebases')).toBe('codebase');
expect(singularize('databases')).toBe('database');
expect(singularize('firebases')).toBe('firebase');
expect(singularize('knowledgebases')).toBe('knowledgebase');
expect(singularize('Codebases')).toBe('Codebase');
expect(singularize('Databases')).toBe('Database');
});
it('should still singularize "bases" to "basis"', () => {
expect(singularize('bases')).toBe('basis');
});
it('should canonicalize malformed trailing triple-s words', () => {
expect(singularize('classs')).toBe('class');
expect(singularize('Classs')).toBe('Class');
expect(singularize('hazardClasss')).toBe('hazardClass');
expect(singularize('hazardClassses')).toBe('hazardClass');
expect(singularize('CLASSS')).toBe('CLASS');
});
});
describe('pluralize', () => {
it('should pluralize regular words', () => {
expect(pluralize('User')).toBe('Users');
expect(pluralize('user')).toBe('users');
expect(pluralize('Person')).toBe('People');
expect(pluralize('Category')).toBe('Categories');
});
it('should normalize class variants', () => {
expect(pluralize('class')).toBe('classes');
expect(pluralize('Class')).toBe('Classes');
expect(pluralize('hazardClass')).toBe('hazardClasses');
expect(pluralize('HazardClass')).toBe('HazardClasses');
expect(pluralize('hazardClasss')).toBe('hazardClasses');
expect(pluralize('classs')).toBe('classes');
});
it('should preserve already-plural Latin words', () => {
expect(pluralize('Schemata')).toBe('Schemata');
expect(pluralize('schemata')).toBe('schemata');
});
it.each([
['class', 'classes'],
['glass', 'glasses'],
['boss', 'bosses'],
['process', 'processes'],
['address', 'addresses'],
['witness', 'witnesses'],
['abyss', 'abysses'],
])(
'should handle -ss noun roundtrip for %s -> %s',
(singularWord, pluralWord) => {
expect(pluralize(singularWord)).toBe(pluralWord);
expect(singularize(pluralWord)).toBe(singularWord);
}
);
});
describe('singularizeLast', () => {
it('should singularize only the last word in compound names', () => {
expect(singularizeLast('user_profiles')).toBe('user_profile');
expect(singularizeLast('UserProfiles')).toBe('UserProfile');
expect(singularizeLast('order_items')).toBe('order_item');
expect(singularizeLast('OrderItems')).toBe('OrderItem');
});
it('should handle Latin suffixes in compound names', () => {
expect(singularizeLast('api_schemata')).toBe('api_schema');
expect(singularizeLast('ApiSchemata')).toBe('ApiSchema');
});
it('should normalize malformed class suffixes in the final segment', () => {
expect(singularizeLast('hazardClassses')).toBe('hazardClass');
expect(singularizeLast('HazardClassses')).toBe('HazardClass');
});
});
describe('pluralizeLast', () => {
it('should pluralize only the last word in compound names', () => {
expect(pluralizeLast('user_profile')).toBe('user_profiles');
expect(pluralizeLast('UserProfile')).toBe('UserProfiles');
expect(pluralizeLast('order_item')).toBe('order_items');
expect(pluralizeLast('OrderItem')).toBe('OrderItems');
});
it('should normalize malformed class suffixes in the final segment', () => {
expect(pluralizeLast('hazardClass')).toBe('hazardClasses');
expect(pluralizeLast('HazardClass')).toBe('HazardClasses');
expect(pluralizeLast('hazardClasss')).toBe('hazardClasses');
expect(pluralizeLast('HazardClasss')).toBe('HazardClasses');
});
});
describe('distinctPluralize', () => {
it('should pluralize regular words', () => {
expect(distinctPluralize('user')).toBe('users');
expect(distinctPluralize('User')).toBe('Users');
});
it('should handle words where singular equals plural', () => {
expect(distinctPluralize('sheep')).toBe('sheeps');
expect(distinctPluralize('fish')).toBe('fishes');
});
it('should handle words ending in ch, s, sh, x, z', () => {
expect(distinctPluralize('bus')).toBe('buses');
expect(distinctPluralize('box')).toBe('boxes');
});
it('should normalize malformed class variants', () => {
expect(distinctPluralize('classs')).toBe('classes');
expect(distinctPluralize('hazardClasss')).toBe('hazardClasses');
});
});
describe('distinctPluralizeLast', () => {
it('should distinctly pluralize only the last word', () => {
expect(distinctPluralizeLast('user_profile')).toBe('user_profiles');
expect(distinctPluralizeLast('UserProfile')).toBe('UserProfiles');
});
it('should normalize malformed class variants in the final segment', () => {
expect(distinctPluralizeLast('hazardClasss')).toBe('hazardClasses');
expect(distinctPluralizeLast('HazardClasss')).toBe('HazardClasses');
});
});
describe('lcFirst', () => {
it('should lowercase the first character', () => {
expect(lcFirst('UserProfile')).toBe('userProfile');
expect(lcFirst('User')).toBe('user');
expect(lcFirst('ABC')).toBe('aBC');
});
it('should handle already lowercase strings', () => {
expect(lcFirst('user')).toBe('user');
});
});
describe('ucFirst', () => {
it('should uppercase the first character', () => {
expect(ucFirst('userProfile')).toBe('UserProfile');
expect(ucFirst('user')).toBe('User');
expect(ucFirst('abc')).toBe('Abc');
});
it('should handle already uppercase strings', () => {
expect(ucFirst('User')).toBe('User');
});
});
describe('toCamelCase', () => {
it('should convert snake_case to camelCase', () => {
expect(toCamelCase('user_profile')).toBe('userProfile');
expect(toCamelCase('order_item')).toBe('orderItem');
expect(toCamelCase('api_schema')).toBe('apiSchema');
});
it('should convert hyphenated strings to camelCase', () => {
expect(toCamelCase('user-profile')).toBe('userProfile');
expect(toCamelCase('order-item')).toBe('orderItem');
expect(toCamelCase('my-component')).toBe('myComponent');
});
it('should convert PascalCase to camelCase', () => {
expect(toCamelCase('UserProfile')).toBe('userProfile');
expect(toCamelCase('HelloWorld')).toBe('helloWorld');
});
it('should handle single words', () => {
expect(toCamelCase('user')).toBe('user');
expect(toCamelCase('User')).toBe('user');
});
});
describe('toPascalCase', () => {
it('should convert snake_case to PascalCase', () => {
expect(toPascalCase('user_profile')).toBe('UserProfile');
expect(toPascalCase('order_item')).toBe('OrderItem');
expect(toPascalCase('api_schema')).toBe('ApiSchema');
});
it('should convert hyphenated strings to PascalCase', () => {
expect(toPascalCase('user-profile')).toBe('UserProfile');
expect(toPascalCase('order-item')).toBe('OrderItem');
expect(toPascalCase('my-component')).toBe('MyComponent');
});
it('should convert camelCase to PascalCase', () => {
expect(toPascalCase('userProfile')).toBe('UserProfile');
expect(toPascalCase('helloWorld')).toBe('HelloWorld');
});
it('should handle single words', () => {
expect(toPascalCase('user')).toBe('User');
expect(toPascalCase('User')).toBe('User');
});
});
describe('toScreamingSnake', () => {
it('should convert camelCase to SCREAMING_SNAKE_CASE', () => {
expect(toScreamingSnake('userProfile')).toBe('USER_PROFILE');
expect(toScreamingSnake('orderItem')).toBe('ORDER_ITEM');
});
it('should convert PascalCase to SCREAMING_SNAKE_CASE', () => {
expect(toScreamingSnake('UserProfile')).toBe('USER_PROFILE');
expect(toScreamingSnake('OrderItem')).toBe('ORDER_ITEM');
});
it('should handle single words', () => {
expect(toScreamingSnake('user')).toBe('USER');
expect(toScreamingSnake('User')).toBe('USER');
});
});
describe('underscore', () => {
it('should convert PascalCase to snake_case', () => {
expect(underscore('UserProfile')).toBe('user_profile');
expect(underscore('OrderItem')).toBe('order_item');
expect(underscore('ApiSchema')).toBe('api_schema');
});
it('should convert camelCase to snake_case', () => {
expect(underscore('userProfile')).toBe('user_profile');
expect(underscore('orderItem')).toBe('order_item');
});
it('should handle single words', () => {
expect(underscore('User')).toBe('user');
expect(underscore('user')).toBe('user');
});
});
describe('fixCapitalisedPlural', () => {
it('should fix capitalized S after numbers', () => {
expect(fixCapitalisedPlural('Table1S')).toBe('Table1s');
expect(fixCapitalisedPlural('blahTable1S')).toBe('blahTable1s');
expect(fixCapitalisedPlural('Table1SConnection')).toBe('Table1sConnection');
});
it('should not affect normal strings', () => {
expect(fixCapitalisedPlural('Users')).toBe('Users');
expect(fixCapitalisedPlural('Table1')).toBe('Table1');
});
});
describe('toFieldName', () => {
it('should convert plural PascalCase to singular camelCase', () => {
expect(toFieldName('Users')).toBe('user');
expect(toFieldName('OrderItems')).toBe('orderItem');
expect(toFieldName('Categories')).toBe('category');
});
it('should handle Latin suffixes', () => {
expect(toFieldName('Schemata')).toBe('schema');
expect(toFieldName('ApiSchemata')).toBe('apiSchema');
});
it('should normalize malformed class variants', () => {
expect(toFieldName('Classes')).toBe('class');
expect(toFieldName('HazardClasses')).toBe('hazardClass');
expect(toFieldName('HazardClassses')).toBe('hazardClass');
});
});
describe('toQueryName', () => {
it('should convert singular PascalCase to plural camelCase', () => {
expect(toQueryName('User')).toBe('users');
expect(toQueryName('OrderItem')).toBe('orderItems');
expect(toQueryName('Category')).toBe('categories');
expect(toQueryName('Class')).toBe('classes');
expect(toQueryName('HazardClass')).toBe('hazardClasses');
});
});