Created
July 28, 2017 16:38
-
-
Save michaelpumo/fecf088d4bfcd73327c94d501d50b5ef to your computer and use it in GitHub Desktop.
A utility function for turning a string into a slugged version.
This file contains hidden or 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
function slugify (text) { | |
const special = 'ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđ߯a·/_,:;' | |
const ordinary = 'AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------' | |
const p = new RegExp(special.split('').join('|'), 'g') | |
return text.toString().toLowerCase() | |
.replace(/\s+/g, '-') | |
.replace(p, c => ordinary.charAt(special.indexOf(c))) | |
.replace(/&/g, '-and-') | |
.replace(/[^\w-]+/g, '') | |
.replace(/--+/g, '-') | |
.replace(/^-+/, '') | |
.replace(/-+$/, '') | |
} | |
export { slugify } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment