Skip to content

Instantly share code, notes, and snippets.

@belchior
Last active February 18, 2016 19:56
Show Gist options
  • Save belchior/4a89809da248ee788522 to your computer and use it in GitHub Desktop.
Save belchior/4a89809da248ee788522 to your computer and use it in GitHub Desktop.
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