Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/inflekt/__tests__/inflection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ describe('camelize', () => {
expect(camelize('api_schema', true)).toBe('apiSchema');
});

it('should convert kebab-case to PascalCase by default', () => {
expect(camelize('schema-file')).toBe('SchemaFile');
expect(camelize('dry-run')).toBe('DryRun');
expect(camelize('output-dir')).toBe('OutputDir');
});

it('should convert kebab-case to camelCase when lowFirstLetter is true', () => {
expect(camelize('schema-file', true)).toBe('schemaFile');
expect(camelize('dry-run', true)).toBe('dryRun');
expect(camelize('output-dir', true)).toBe('outputDir');
});

it('should handle single words', () => {
expect(camelize('user')).toBe('User');
expect(camelize('user', true)).toBe('user');
Expand Down
8 changes: 5 additions & 3 deletions packages/inflekt/src/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ export function fixCapitalisedPlural(str: string): string {
}

/**
* Convert snake_case to PascalCase (or camelCase if lowFirstLetter is true)
* @param str - The snake_case string to convert
* Convert snake_case or kebab-case to PascalCase (or camelCase if lowFirstLetter is true)
* @param str - The snake_case or kebab-case string to convert
* @param lowFirstLetter - If true, returns camelCase instead of PascalCase
* @example camelize('user_profile') -> 'UserProfile'
* @example camelize('user_profile', true) -> 'userProfile'
* @example camelize('schema-file') -> 'SchemaFile'
* @example camelize('schema-file', true) -> 'schemaFile'
*/
export function camelize(str: string, lowFirstLetter?: boolean): string {
const result = str
.split('_')
.split(/[-_]/)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('');

Expand Down