Skip to content

Commit 50809e5

Browse files
committed
feat: add camelize and underscore exports to @interweb/inflection
1 parent 3dce373 commit 50809e5

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

packages/inflection/__tests__/inflection.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
distinctPluralizeLast,
88
lcFirst,
99
ucFirst,
10+
camelize,
11+
underscore,
1012
fixCapitalisedPlural,
1113
toFieldName,
1214
toQueryName,
@@ -126,6 +128,43 @@ describe('ucFirst', () => {
126128
});
127129
});
128130

131+
describe('camelize', () => {
132+
it('should convert snake_case to PascalCase by default', () => {
133+
expect(camelize('user_profile')).toBe('UserProfile');
134+
expect(camelize('order_item')).toBe('OrderItem');
135+
expect(camelize('api_schema')).toBe('ApiSchema');
136+
});
137+
138+
it('should convert snake_case to camelCase when lowFirstLetter is true', () => {
139+
expect(camelize('user_profile', true)).toBe('userProfile');
140+
expect(camelize('order_item', true)).toBe('orderItem');
141+
expect(camelize('api_schema', true)).toBe('apiSchema');
142+
});
143+
144+
it('should handle single words', () => {
145+
expect(camelize('user')).toBe('User');
146+
expect(camelize('user', true)).toBe('user');
147+
});
148+
});
149+
150+
describe('underscore', () => {
151+
it('should convert PascalCase to snake_case', () => {
152+
expect(underscore('UserProfile')).toBe('user_profile');
153+
expect(underscore('OrderItem')).toBe('order_item');
154+
expect(underscore('ApiSchema')).toBe('api_schema');
155+
});
156+
157+
it('should convert camelCase to snake_case', () => {
158+
expect(underscore('userProfile')).toBe('user_profile');
159+
expect(underscore('orderItem')).toBe('order_item');
160+
});
161+
162+
it('should handle single words', () => {
163+
expect(underscore('User')).toBe('user');
164+
expect(underscore('user')).toBe('user');
165+
});
166+
});
167+
129168
describe('fixCapitalisedPlural', () => {
130169
it('should fix capitalized S after numbers', () => {
131170
expect(fixCapitalisedPlural('Table1S')).toBe('Table1s');

packages/inflection/src/case.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Case transformation utilities
33
*/
4+
import * as inflection from 'inflection';
45

56
/**
67
* Convert PascalCase to camelCase (lowercase first character)
@@ -10,6 +11,25 @@ export function lcFirst(str: string): string {
1011
return str.charAt(0).toLowerCase() + str.slice(1);
1112
}
1213

14+
/**
15+
* Convert a string to camelCase
16+
* @param str - The string to convert
17+
* @param lowFirstLetter - If true, the first letter will be lowercase (default: false)
18+
* @example camelize("user_profile") -> "UserProfile"
19+
* @example camelize("user_profile", true) -> "userProfile"
20+
*/
21+
export function camelize(str: string, lowFirstLetter?: boolean): string {
22+
return inflection.camelize(str, lowFirstLetter);
23+
}
24+
25+
/**
26+
* Convert a camelCase or PascalCase string to snake_case
27+
* @example underscore("UserProfile") -> "user_profile"
28+
*/
29+
export function underscore(str: string): string {
30+
return inflection.underscore(str);
31+
}
32+
1333
/**
1434
* Convert camelCase to PascalCase (uppercase first character)
1535
* @example "userProfile" -> "UserProfile"

0 commit comments

Comments
 (0)