Created
December 9, 2011 12:08
-
-
Save gerardpaapu/1451292 to your computer and use it in GitHub Desktop.
Get a clean ASCII string from a JavaScript string
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 cleanString(str, method, r) { | |
| var result = '', | |
| max = str.length, | |
| i, ch; | |
| for (i = 0; i < max; i++) { | |
| ch = str.charCodeAt(i); | |
| if (ch < 128) { | |
| result += str.charAt(i); | |
| } else { | |
| switch (method) { | |
| case 'elide': | |
| break; | |
| case 'replace': | |
| result += r || '?'; | |
| break; | |
| case 'entity': | |
| result += '&#' + ch + ';'; | |
| break; | |
| default: | |
| throw new Error(); | |
| } | |
| } | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment