-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74a6d3a
commit 71ed1c8
Showing
7 changed files
with
95 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { toCamelCase, toPascalCase } from "../src/utils"; | ||
|
||
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 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'); | ||
}); | ||
}); | ||
|
||
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function toPascalCase(str: string) { | ||
return str.replace(/(^|_|\s|-)(\w)/g, (_: any, __: any, letter: string) => letter.toUpperCase()).replace(/[_\s-]/g, ''); | ||
} | ||
|
||
export function toCamelCase(key: string) { | ||
return key | ||
// First, remove all leading non-alphabet characters | ||
.replace(/^[^a-zA-Z]+/, '') | ||
// Convert what follows a separator into upper case | ||
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '') | ||
// Ensure the first character of the result is always lowercase | ||
.replace(/^./, (c) => c.toLowerCase()); | ||
} |