Last active
January 4, 2016 02:29
-
-
Save itsjavi/8555464 to your computer and use it in GitHub Desktop.
JS string slugize
This file contains hidden or 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
| /* | |
| This is a merge of the node slugize-component module | |
| Usage: $str.slugize('SómE StrïNG'); | |
| */ | |
| (function(){ | |
| var special = ['À','à','Á','á','Â','â','Ã','ã','Ä','ä','Å','å','Ă','ă','Ą','ą','Ć' | |
| , 'ć','Č','č','Ç','ç','Ď','ď','Đ','đ', 'È','è','É','é','Ê','ê','Ë','ë' | |
| , 'Ě','ě','Ę','ę','Ğ','ğ','Ì','ì','Í','í','Î','î','Ï','ï', 'Ĺ','ĺ','Ľ' | |
| , 'ľ','Ł','ł','Ñ','ñ','Ň','ň','Ń','ń','Ò','ò','Ó','ó','Ô','ô','Õ','õ' | |
| , 'Ö','ö','Ø','ø','ő','Ř','ř','Ŕ','ŕ','Š','š','Ş','ş','Ś','ś','Ť','ť' | |
| , 'Ť','ť','Ţ','ţ','Ù','ù','Ú','ú','Û','û','Ü','ü','Ů','ů','Ÿ','ÿ','ý' | |
| , 'Ý','Ž','ž','Ź','ź','Ż','ż','Þ','þ','Ð','ð','ß','Œ','œ','Æ','æ','µ'] | |
| , standard = ['A','a','A','a','A','a','A','a','Ae','ae','A','a','A','a','A','a' | |
| , 'C','c','C','c','C','c','D','d','D','d', 'E','e','E','e','E','e' | |
| , 'E','e','E','e','E','e','G','g','I','i','I','i','I','i','I','i','L' | |
| , 'l','L','l','L','l','N','n','N','n','N','n', 'O','o','O','o','O' | |
| , 'o','O','o','Oe','oe','O','o','o','R','r','R','r','S','s','S','s' | |
| , 'S','s','T','t','T','t','T','t','U','u','U','u','U','u','Ue','ue' | |
| , 'U','u','Y','y','Y','y','Z','z','Z','z','Z','z','TH','th','DH','dh' | |
| , 'ss','OE','oe','AE','ae','u']; | |
| function standarize(str){ | |
| var text = str; | |
| for (var i = 0, l = special.length; i < l; i++){ | |
| if (typeof special[i] == 'string') special[i] = new RegExp(special[i], 'g'); | |
| text = text.replace(special[i], standard[i]); | |
| } | |
| return text; | |
| }; | |
| function namize(str){ | |
| return str.toLowerCase().replace(/\b[a-z]|(\-[a-z])/g, function(match){ | |
| return match.toUpperCase(); | |
| }).replace(/(\bMc[a-z])|(\-Mc[a-z])/g, function(match){ | |
| return match.substr(0, match.length - 1) + match.substr(-1).toUpperCase(); | |
| }); | |
| }; | |
| function slugize(str){ | |
| return standarize(namize(str)).toLowerCase() | |
| .replace(/ +/g, '-') | |
| .replace(/[^-\w]/g, ''); | |
| }; | |
| window.$str = { | |
| standarize:standarize, | |
| namize:namize, | |
| slugize:slugize | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment