Convert Camel Case to Snake Case
This challenge involves transforming strings from camelCase notation to snake_case notation. This is a common task in programming, especially when dealing with different naming conventions across languages or frameworks. Your goal is to write a JavaScript function that accurately performs this conversion.
Problem Description
You need to create a JavaScript function named toSnakeCase that accepts a single string argument representing a camelCase string. The function should return a new string where all uppercase letters in the input string are converted to lowercase and preceded by an underscore.
Key Requirements:
- The function must handle strings that start with an uppercase letter.
- The function must handle strings with consecutive uppercase letters (e.g., "APIResponse").
- The function should not add an underscore at the beginning of the output string if the input string begins with a lowercase letter.
- The function should not add an underscore at the end of the output string.
Expected Behavior:
myVariableNameshould becomemy_variable_name.APIResponseshould becomeapi_response.someHTMLStuffshould becomesome_html_stuff.HTTPRequestshould becomehttp_request.variableshould remainvariable.
Edge Cases to Consider:
- Empty string.
- Strings with only uppercase letters.
- Strings with no uppercase letters.
- Strings starting with an uppercase letter.
Examples
Example 1:
Input: "convertCamelToSnake"
Output: "convert_camel_to_snake"
Explanation: Each uppercase letter ('C', 'T', 'S') is converted to lowercase and preceded by an underscore.
Example 2:
Input: "APIResponseHandler"
Output: "api_response_handler"
Explanation: Consecutive uppercase letters ('API', 'R', 'H') are handled correctly. 'A', 'P', 'I' are treated as a unit, and 'R' and 'H' are also handled.
Example 3:
Input: "simple"
Output: "simple"
Explanation: A string with no uppercase letters remains unchanged.
Example 4:
Input: "XMLHTTPRequest"
Output: "xml_http_request"
Explanation: Demonstrates handling of multiple consecutive uppercase letters at the beginning and within the string.
Example 5:
Input: "FirstName"
Output: "first_name"
Explanation: Handles a string that starts with an uppercase letter.
Constraints
- The input string will consist of alphanumeric characters and may contain uppercase and lowercase letters.
- The input string length will be between 0 and 1000 characters, inclusive.
- The function should aim for efficient processing, ideally with a time complexity of O(n), where n is the length of the input string.
Notes
Consider using regular expressions for a concise and efficient solution. Pay close attention to how you handle the beginning of the string and sequences of uppercase letters. Remember that the goal is to replace uppercase letters with their lowercase equivalents preceded by an underscore, but only when a transition from a lowercase letter to an uppercase letter or a transition from an uppercase letter to another uppercase letter followed by a lowercase letter occurs.