Created
April 6, 2016 16:35
-
-
Save hhhonzik/c39f668d629d161f55cf0b4152296b33 to your computer and use it in GitHub Desktop.
Make friendly URL from any string.
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 nodiac = { 'á': 'a', 'č': 'c', 'ď': 'd', 'é': 'e', 'ě': 'e', 'í': 'i', 'ň': 'n', 'ó': 'o', 'ř': 'r', 'š': 's', 'ť': 't', 'ú': 'u', 'ů': 'u', 'ý': 'y', 'ž': 'z' }; | |
/** Friendly URL | |
* @param s string with spaces and weird chars. | |
* @return string URL friendly | |
*/ | |
function make_url(s) { | |
return s.toLowerCase() | |
.split('') | |
.map( | |
letter => nodiac[letter] || letter | |
) | |
.join('') | |
.replace(/[^a-z0-9_]+/g, '-') | |
.replace(/^-|-$/g, ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment