Skip to content

Instantly share code, notes, and snippets.

@nakajima
Created July 23, 2008 21:09
Show Gist options
  • Select an option

  • Save nakajima/1903 to your computer and use it in GitHub Desktop.

Select an option

Save nakajima/1903 to your computer and use it in GitHub Desktop.
// 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