Skip to content

Instantly share code, notes, and snippets.

@hhhonzik
Created April 6, 2016 16:35
Show Gist options
  • Save hhhonzik/c39f668d629d161f55cf0b4152296b33 to your computer and use it in GitHub Desktop.
Save hhhonzik/c39f668d629d161f55cf0b4152296b33 to your computer and use it in GitHub Desktop.
Make friendly URL from any string.
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