Hone logo
Hone
Problems

Snake Case to Camel Case Converter

In many programming languages, naming conventions differ. Snake case (e.g., my_variable_name) is common in Python and Ruby, while camel case (e.g., myVariableName) is prevalent in JavaScript. This challenge focuses on writing a JavaScript function to convert strings from snake case to camel case, a fundamental skill for maintaining consistent code style.

Problem Description

Your task is to implement a JavaScript function named snakeToCamel that accepts a single string argument. This string will be in snake_case format. The function should return a new string where all underscores have been removed, and the character immediately following each underscore is capitalized. The first word of the string should remain lowercase.

Key Requirements:

  • The function must handle strings containing one or more underscores.
  • It should correctly capitalize the letter following each underscore.
  • The function should not modify the original string; it should return a new string.

Expected Behavior:

  • "hello_world" should become "helloWorld"
  • "this_is_a_test" should become "thisIsATest"
  • "singleword" should become "singleword"

Edge Cases to Consider:

  • Empty strings.
  • Strings with leading or trailing underscores.
  • Strings with consecutive underscores.

Examples

Example 1:

Input: "first_name"
Output: "firstName"
Explanation: The underscore is removed, and the 'n' after it is capitalized.

Example 2:

Input: "user_id_number"
Output: "userIdNumber"
Explanation: Both underscores are processed, capitalizing the 'i' and the 'n'.

Example 3:

Input: "another_example_string_with_underscores"
Output: "anotherExampleStringWithUnderscores"
Explanation: Demonstrates handling multiple underscores in a longer string.

Example 4 (Edge Case):

Input: "_leading_underscore"
Output: "LeadingUnderscore"
Explanation: A leading underscore is removed, and the subsequent character is capitalized.

Example 5 (Edge Case):

Input: "trailing_underscore_"
Output: "trailingUnderscore"
Explanation: A trailing underscore is removed.

Example 6 (Edge Case):

Input: "consecutive__underscores"
Output: "consecutiveUnderscores"
Explanation: Consecutive underscores are treated as a single separator, and the character following the last underscore in the sequence is capitalized.

Example 7 (Edge Case):

Input: ""
Output: ""
Explanation: An empty string remains an empty string.

Constraints

  • The input string will consist of lowercase English letters, numbers, and underscores (_).
  • The input string length will be between 0 and 1000 characters, inclusive.
  • The function should execute efficiently, with a time complexity of O(n), where n is the length of the input string.

Notes

Consider using string manipulation methods available in JavaScript. Regular expressions can be a powerful tool for this type of transformation. Think about how to iterate through the string or use a method that efficiently finds and replaces patterns.

Loading editor...
javascript