Skip to content

Instantly share code, notes, and snippets.

@maxcelos
Created September 27, 2017 16:55
Show Gist options
  • Save maxcelos/e8cbabc1c6bfab78e171d7f77107bb76 to your computer and use it in GitHub Desktop.
Save maxcelos/e8cbabc1c6bfab78e171d7f77107bb76 to your computer and use it in GitHub Desktop.
Same as slugify.js but using underscore
// 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