Skip to content

Instantly share code, notes, and snippets.

@espretto
Last active July 28, 2023 06:54
Show Gist options
  • Save espretto/8115083347144fd0afc793a3e3b60f45 to your computer and use it in GitHub Desktop.
Save espretto/8115083347144fd0afc793a3e3b60f45 to your computer and use it in GitHub Desktop.
js: slugify
const latin1 = (
'A,A,A,A,Ae,A,AE,C,E,E,E,E,I,I,I,I,D,N,O,O,O,O,Oe,-,Oe,U,U,U,Ue,Y,Th,ss,' +
'a,a,a,a,ae,a,ae,c,e,e,e,e,i,i,i,i,d,n,o,o,o,o,oe,-,oe,u,u,u,ue,y,th,y'
).split(',');
function slugifyChar(chr) {
return latin1[chr.charCodeAt(0) - 0xC0];
}
function slugify(str) {
return str
.replace(/[\xC0-\xFF]/g, slugifyChar)
.replace(/[a-z](?=[A-Z])/g, '$&-')
.replace(/\W+/g, '-')
.toLowerCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment