Skip to content

Instantly share code, notes, and snippets.

@vjt
Created February 15, 2011 15:50
Show Gist options
  • Save vjt/827679 to your computer and use it in GitHub Desktop.
Save vjt/827679 to your computer and use it in GitHub Desktop.
String.camelize
// 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 () : '';
})
}
});
@viniciusCamargo
Copy link

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