Skip to content

Instantly share code, notes, and snippets.

@mrofi
Last active August 29, 2015 14:19
Show Gist options
  • Save mrofi/e1271c963e97461ca542 to your computer and use it in GitHub Desktop.
Save mrofi/e1271c963e97461ca542 to your computer and use it in GitHub Desktop.
case in string
// convert space to undersocre (snake case)
string.toLowerCase().split(' ').join('_');
// convert underscore to space
string.split('_').join(' ');
// camelize
function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index == 0 ? match.toLowerCase() : match.toUpperCase();
});
}
// credits : http://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case
// Capitalize
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment