forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeToCamelCase.js
44 lines (40 loc) · 1.53 KB
/
SnakeToCamelCase.js
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
/**
* Converts a string from snake_case to camelCase.
*
* @param {string} str - The input string in snake_case format.
* @throws {Error} Will throw an error if the input is not a string.
* @returns {string} The converted string in camelCase format.
*
* @example
*
* snakeToCamelCase("hello_world"); // Returns "helloWorld"
* snakeToCamelCase("snake_case_example"); // Returns "snakeCaseExample"
* snakeToCamelCase("_leading_underscore"); // Returns "leadingUnderscore"
* snakeToCamelCase("trailing_underscore_"); // Returns "trailingUnderscore"
* snakeToCamelCase("__multiple__underscores__"); // Returns "multipleUnderscores"
* snakeToCamelCase("snake_case@example"); // Returns "snakeCaseExample"
* snakeToCamelCase("_leading_underscore_#"); // Returns "leadingUnderscore"
* snakeToCamelCase("trailing_underscore_&"); // Returns "trailingUnderscore"
* snakeToCamelCase(""); // Returns ""
*
* @throws {Error} If the input is not a string.
*/
function snakeToCamelCase(str) {
// Will throw an error if the input is not a string.
if (typeof str !== 'string') {
throw new Error(`Expected string as input, found ${typeof str}`)
}
if (str.trim() === '') return '' // Handle empty string
// Remove special characters (excluding underscores)
const cleanedStr = str.replace(/[^a-zA-Z0-9_]/g, '')
return cleanedStr
.split('_')
.filter(Boolean)
.map((value, index) => {
return index === 0
? value
: value.charAt(0).toUpperCase() + value.slice(1)
})
.join('')
}
export { snakeToCamelCase }