Skip to content

Instantly share code, notes, and snippets.

@chriskonnertz
Created February 21, 2018 18:40
Show Gist options
  • Save chriskonnertz/cd428dba7a497c7d6478a77b4ce1926f to your computer and use it in GitHub Desktop.
Save chriskonnertz/cd428dba7a497c7d6478a77b4ce1926f to your computer and use it in GitHub Desktop.
JS snake to camel case converter
/**
* 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