Last active
December 24, 2015 03:59
-
-
Save nicolashery/6740659 to your computer and use it in GitHub Desktop.
Change variable names to another naming convention in JavaScript
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
// 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