Skip to content

Instantly share code, notes, and snippets.

@nicolashery
Last active December 24, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolashery/6740659 to your computer and use it in GitHub Desktop.
Save nicolashery/6740659 to your computer and use it in GitHub Desktop.
Change variable names to another naming convention in JavaScript
// Change variable names to another naming convention
//
// Thanks to Oliver Caldwell for the inspiration
// http://oli.me.uk/2013/09/25/grabbing-elements-from-the-dom/
// 'hello_world' -> 'helloWorld'
function snakeToCamelCase(name) {
return name.toLowerCase().replace(/_(\w)/ig, function (match, hump) {
return hump.toUpperCase();
});
}
// 'hello-world' -> 'helloWorld'
function hyphensToCamelCase(name) {
return name.toLowerCase().replace(/-(\w)/ig, function (match, hump) {
return hump.toUpperCase();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment