Last active
February 18, 2016 19:56
-
-
Save belchior/4a89809da248ee788522 to your computer and use it in GitHub Desktop.
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
function textToSlug(text) { | |
var i; | |
var reg; | |
var from = 'àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŕŕ'; | |
var to = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyrr'; | |
if (typeof text !== 'string') { | |
return new TypeError('The parameter must be string'); | |
} | |
text = text.toLowerCase().trim().replace(/\s+/g, '-').replace(/\-{2,}/g, '-'); | |
for (i = 0; i < from.length; i += 1) { | |
reg = new RegExp(from.charAt(i), 'gim'); | |
text = text.replace(reg, to.charAt(i)); | |
} | |
return text.replace(/[^\w\-]*/g, ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment