Created
January 18, 2018 19:32
-
-
Save rogeralbinoi/4189d8284df5cc3072be5f763dfd55f9 to your computer and use it in GitHub Desktop.
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
const unwantedChars = /[^a-z0-9_-]/gi; | |
const defaultReplacement = ''; | |
/* eslint-disable */ | |
const replacements = { | |
' ': '-' | |
}; | |
const regexReplacements = [ | |
{char: 'a', pattern: /[\xE0-\xE6]/gi}, | |
{char: 'e', pattern: /[\xE8-\xEB]/gi}, | |
{char: 'i', pattern: /[\xEC-\xEF]/gi}, | |
{char: 'o', pattern: /[\xF2-\xF6]/gi}, | |
{char: 'u', pattern: /[\xF9-\xFC]/gi}, | |
{char: 'c', pattern: /\xE7/gi}, | |
{char: 'n', pattern: /\xF1/gi} | |
] | |
/* eslint-enable */ | |
const replaceChar = char => { | |
const replacementPattern = regexReplacements.find(item => item.pattern.test(char)); | |
return replacements[char] || (replacementPattern || {}).char || defaultReplacement; | |
}; | |
const sanitizeString = str => str.toLowerCase().trim().replace(unwantedChars, replaceChar); | |
export default sanitizeString; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment