Created
June 4, 2017 23:46
-
-
Save JDMCreator/af7ecded20602dcf7950091ae7886a21 to your computer and use it in GitHub Desktop.
Convert a String to ASCII (Convert accented characters to normal characters and remove others characters)
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
function toASCII(str){ | |
var newstr = "", | |
graph_table = { | |
"\u0132" : "IJ", | |
"\u0152" : "OE", | |
"\u00C6" : "AE", | |
"\u00D8" : "O" | |
} | |
str = str.normalize("NFD"); | |
for(var i=0, code, char;i<str.length;i++){ | |
code = str.charCodeAt(i), | |
char = str.charAt(i); | |
if(code<128){ | |
newstr += "" + char; | |
} | |
else if(char.toUpperCase() != char.toLowerCase()){ | |
var isLowerCase = char.toLowerCase() == char; | |
char = graph_table[char.toUpperCase()] | |
if(char){ | |
newstr += isLowerCase ? char.toLowerCase() : char; | |
} | |
} | |
} | |
return newstr | |
} | |
/* | |
/* Example : | |
/* toASCII("étalon") // "etalon" | |
/**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment