Last active
July 28, 2023 06:54
-
-
Save espretto/8115083347144fd0afc793a3e3b60f45 to your computer and use it in GitHub Desktop.
js: slugify
This file contains 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
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