Last active
October 31, 2016 18:18
-
-
Save wesruv/5e6d7dd49e3b339cfa17667f8aac5afb to your computer and use it in GitHub Desktop.
Clean CSS identifier from string
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
// 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(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated with @m4olivei's feedback