Convert Snake Case Variables to Camel Case
Problem statement
Given source text text, convert every occurrence of snake_case to lowerCamelCase. Apply the change throughout the text, including comments and strings; you do not need to parse a programming language.
- Remove each single underscore directly between two lowercase English letters, and capitalize the letter immediately after it.
- Preserve leading and trailing underscores.
- Keep every other character in its original order, including spaces, punctuation, and line breaks.
For this exercise, use the original neighboring characters to decide whether an underscore is internal. Runs of two or more underscores stay unchanged. An empty input returns an empty string.
Function
toCamelCase(text: String) → StringExamples
Example 1
text = "__variable_one__ _variable_two variable_three"return = "__variableOne__ _variableTwo variableThree"The leading and trailing underscores stay in place. The internal underscores are removed, and the letters o and t that follow them become uppercase.
Example 2
text = "foo_bar __hello_world_ plain"return = "fooBar __helloWorld_ plain"foo_bar becomes fooBar, __hello_world_ becomes __helloWorld_, and plain is unchanged.
Example 3
text = "This is the doc_string for __secret_fun"return = "This is the docString for __secretFun"The source example changes doc_string to docString and __secret_fun to __secretFun. The surrounding text stays unchanged.
Example 4
text = ""return = ""There are no characters to convert, so the result is the empty string.
Constraints
0 <= text.length <= 10^5.- For this exercise,
textcontains printable ASCII characters, tabs, carriage returns, and line feeds. - Reported snake-case variable words use lowercase English letters and one underscore between words.