Skip to content

Instantly share code, notes, and snippets.

@theredhead
Created March 9, 2021 19:33
Show Gist options
  • Save theredhead/2d91aaf6724adbf6398de4472b57f067 to your computer and use it in GitHub Desktop.
Save theredhead/2d91aaf6724adbf6398de4472b57f067 to your computer and use it in GitHub Desktop.
Sluggify a text (typescript)
const sluggify = (text: string): string => {
const allowedCharacters = 'abcdefghijklmnopqrstuvwxyz_1234567890';
const result = [];
const preTransformed = text.toLowerCase().replace(/\s/, '-');
for (let char of preTransformed) {
if (allowedCharacters.includes(char)) {
result.push(char);
}
}
return result.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment