Skip to content

Instantly share code, notes, and snippets.

@wesruv
Last active October 31, 2016 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wesruv/5e6d7dd49e3b339cfa17667f8aac5afb to your computer and use it in GitHub Desktop.
Save wesruv/5e6d7dd49e3b339cfa17667f8aac5afb to your computer and use it in GitHub Desktop.
Clean CSS identifier from string
// In a perfect world, you'd never have to do this. Wish I lived in a perfect world.
/**
* Convert a string to a usable class name
* @param {string} stringToConvert String of text
* @return {string} Usable class name
*/
function convertStringToClassName(stringToConvert) {
if (typeof stringToConvert === 'string') {
return stringToConvert.trim().replace(/[\s-_]+/g, '-').replace(/[^\w-]|^-|-$/g, '').replace(/-+(?=-)/, '').toLowerCase();
}
};
@wesruv
Copy link
Author

wesruv commented Oct 31, 2016

Updated with @m4olivei's feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment