Created
July 23, 2008 21:09
-
-
Save nakajima/1903 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
| // Module to contain logic for fixing characters. | |
| var FigureFixer = { | |
| Map: { }, // Holds character/code pairs | |
| // Adds a character/code pairing, where the "code" | |
| // is the escaped string and the "character" is the | |
| // character that should replace it. | |
| add: function(code, character) { | |
| FigureFixer.Map[code] = character; | |
| }, | |
| // Iterate through the code/characters added and replace | |
| // each, then return the string. | |
| parse: function(string) { | |
| for (code in FigureFixer.Map) | |
| string.replace(code, FigureFixer[code]); | |
| return string; | |
| } | |
| } | |
| // Add a simple method to strings for fixing characters | |
| String.prototype.fixCharacters = function() { | |
| return FigureFixer.parse(this); | |
| } | |
| // Add character/code pair | |
| FigureFixer.add("\u0026", '&'); | |
| // Create a test string | |
| var TEST_STRING = "This \u0026 That"; | |
| // Call string method | |
| TEST_STRING.fixCharacters(); | |
| // View result | |
| print(TEST_STRING); // => This & That |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment