Created
February 21, 2018 18:40
-
-
Save chriskonnertz/cd428dba7a497c7d6478a77b4ce1926f to your computer and use it in GitHub Desktop.
JS snake to camel case converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts a string in snake case to camel case and returns it. | |
* Example "this_is_cool" => "thisIsCool" | |
* ATTENTION: Expects the original string to be in valid snake case. | |
* Will not perform any validation - invalid input creates invalid output! | |
*/ | |
snakeToCamel: function(snakeCased) { | |
// Use a regular expression to find the underscores + the next letter | |
return snakeCased.replace(/(_\w)/g, function (match) { | |
// Convert to upper case and ignore the first char (=the underscore) | |
return match.toUpperCase().substr(1); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment