Created
February 15, 2011 15:50
-
-
Save vjt/827679 to your computer and use it in GitHub Desktop.
String.camelize
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
// I thought I needed it, but I didn't need it anymore, | |
// but I already implemented it. So, here we go, if you | |
// ever would need a Javascript camelize implementation | |
// you can freely use this :-). | |
// - [email protected] Tue Feb 15 16:49:52 CET 2011 | |
jQuery.extend (String.prototype, { | |
camelize: function () { | |
return this.replace (/(?:^|[-_])(\w)/g, function (_, c) { | |
return c ? c.toUpperCase () : ''; | |
}) | |
} | |
}); |
@knutole: modifying the prototype of a built-in type is considered bad practice in JavaScript...
Thanks!
@mishrud get over it
👍 thanks
More ES6ish:
const camelCase = (string) => {
/*
* by http://stackoverflow.com/users/140811/scott
* at http://stackoverflow.com/a/2970588/6776673
*/
return string
.replace(/\s(.)/g, $1 => $1.toUpperCase())
.replace(/\s/g, '')
.replace(/^(.)/, $1 => $1.toLowerCase())
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!
vanilla version:
String.prototype.camelize = function () {
return this.replace (/(?:^|[-])(\w)/g, function (, c) {
return c ? c.toUpperCase () : '';
});
}