Created
September 27, 2017 16:55
-
-
Save maxcelos/e8cbabc1c6bfab78e171d7f77107bb76 to your computer and use it in GitHub Desktop.
Same as slugify.js but using underscore
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
// Same as slugify.js but using underscore | |
// Ex: "Anões de Jardim Botânico" will become "anoes_de_jardim_botanico" | |
function underscoredSlug (text) { | |
const a = 'àáãäâèéëêìíïîòóöõôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/-,:;'; | |
const b = 'aaaaaeeeeiiiiooooouuuuncsyoarsnpwgnmuxzh______'; | |
const p = new RegExp(a.split('').join('|'), 'g'); | |
return text.toString().toLowerCase() | |
.replace(/\s+/g, '_') // Replace spaces with _ | |
.replace(p, c => | |
b.charAt(a.indexOf(c))) // Replace special chars | |
.replace(/&/g, '_and_') // Replace & with 'and' | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\_\_+/g, '_') // Replace multiple - with single - | |
.replace(/^_+/, '') // Trim - from start of text | |
.replace(/_+$/, '') // Trim - from end of text | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment