Skip to content

Instantly share code, notes, and snippets.

@qfox
Created February 1, 2013 00:19
Show Gist options
  • Save qfox/4688012 to your computer and use it in GitHub Desktop.
Save qfox/4688012 to your computer and use it in GitHub Desktop.
Variable naming style converters via camelCase one
String.prototype.toCamelCase = function() {
return this.substr(0, 1).toLowerCase() + this.substr(1).replace(/[\-_][a-z]/g, function($0){return $0.toUpperCase().replace(/[\_\-]/,'');});
};
String.prototype.toPascalCase = function() {
return this.substr(0, 1).toUpperCase() + this.substr(1).toCamelCase();
};
String.prototype.toUnderscore = function() {
return this.toCamelCase().replace(/[A-Z]/g, function($0){return "_"+$0.toLowerCase();}).replace(/^_/,'');
};
String.prototype.toDash = function() {
return this.toCamelCase().replace(/[A-Z]/g, function($0){return "-"+$0.toLowerCase();}).replace(/^\-/,'');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment