Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cc3c52d

Browse files
committedOct 1, 2024
add SnakeToCamelCase conversion
1 parent 9010481 commit cc3c52d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
 

‎Conversions/SnakeToCamelCase.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Converts a string from snake_case to camelCase.
3+
*
4+
* @param {string} str - The input string in snake_case format.
5+
* @throws {Error} Will throw an error if the input is not a string.
6+
* @returns {string} The converted string in camelCase format.
7+
*
8+
* @example
9+
*
10+
* snakeToCamelCase("hello_world"); // Returns "helloWorld"
11+
* snakeToCamelCase("snake_case_example"); // Returns "snakeCaseExample"
12+
* snakeToCamelCase("_leading_underscore"); // Returns "leadingUnderscore"
13+
* snakeToCamelCase("trailing_underscore_"); // Returns "trailingUnderscore"
14+
* snakeToCamelCase("__multiple__underscores__"); // Returns "multipleUnderscores"
15+
* snakeToCamelCase("snake_case@example"); // Returns "snakeCaseExample"
16+
* snakeToCamelCase("_leading_underscore_#"); // Returns "leadingUnderscore"
17+
* snakeToCamelCase("trailing_underscore_&"); // Returns "trailingUnderscore"
18+
* snakeToCamelCase(""); // Returns ""
19+
*
20+
* @throws {Error} If the input is not a string.
21+
*/
22+
function snakeToCamelCase(str) {
23+
// Will throw an error if the input is not a string.
24+
if (typeof str !== 'string') {
25+
throw new Error(`Expected string as input, found ${typeof str}`)
26+
}
27+
28+
if (str.trim() === '') return '' // Handle empty string
29+
30+
// Remove special characters (excluding underscores)
31+
const cleanedStr = str.replace(/[^a-zA-Z0-9_]/g, '')
32+
33+
return cleanedStr
34+
.split('_')
35+
.filter(Boolean)
36+
.map((value, index) => {
37+
return index === 0
38+
? value
39+
: value.charAt(0).toUpperCase() + value.slice(1)
40+
})
41+
.join('')
42+
}
43+
44+
export { snakeToCamelCase }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { snakeToCamelCase } from '../SnakeToCamelCase'
2+
3+
describe('snakeToCamelCase', () => {
4+
it.each([
5+
['hello_world', 'helloWorld'],
6+
['snake_case_example', 'snakeCaseExample'],
7+
['_leading_underscore', 'leadingUnderscore'],
8+
['trailing_underscore_', 'trailingUnderscore'],
9+
['__multiple__underscores__', 'multipleUnderscores'],
10+
['snake_case@example', 'snakeCaseexample'],
11+
['_leading_underscore_#', 'leadingUnderscore'],
12+
['trailing_underscore_&', 'trailingUnderscore'],
13+
['', '']
14+
])('converts %s to snake_case %s', (input, expected) => {
15+
expect(snakeToCamelCase(input)).toBe(expected)
16+
})
17+
18+
it('throws an error when the input is not a string', () => {
19+
expect(() => snakeToCamelCase(123)).toThrow('Expected string as input')
20+
})
21+
})

0 commit comments

Comments
 (0)
Please sign in to comment.