Skip to content

Instantly share code, notes, and snippets.

@FrankFonts
Last active May 28, 2022 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankFonts/a854906873db4a6c727fd38b0c749028 to your computer and use it in GitHub Desktop.
Save FrankFonts/a854906873db4a6c727fd38b0c749028 to your computer and use it in GitHub Desktop.
camelise(str) JS function
// The function camelise(str) changes dash-separated words
// like “my-short-string” into camel-cased “myShortString”.
// Removes all dashes, each word after dash becomes uppercased.
function camelize(str) {
return str.split('-').reduce((a, b) => {
return a + b[0].toUpperCase()+b.slice(1);
});
}
// test
console.log(camelize('')); // returns '';
console.log(camelize('background-color')); // returns 'backgroundColor';
console.log(camelize('list-style-image')); // returns 'listStyleImage';
console.log(camelize('-webkit-transition')); // returns 'WebkitTransition';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment