Centralize text encoding

This commit is contained in:
Essem 2022-09-10 23:48:44 -05:00
parent 20f4849fee
commit 3eae453de3
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
9 changed files with 23 additions and 34 deletions

View file

@ -38,25 +38,7 @@ export function clean(text) {
return text;
}
// regexEscape(string) to escape characters in a string for use in a regex
export function regexEscape(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
// decodeEntities(string)
export function decodeEntities(string) {
var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
var translate = {
"nbsp": " ",
"amp": "&",
"quot": "\"",
"lt": "<",
"gt": ">"
};
return string.replace(translate_re, function(match, entity) {
return translate[entity];
}).replace(/&#(\d+);/gi, function(match, numStr) {
var num = parseInt(numStr, 10);
return String.fromCharCode(num);
});
}
// textEncode(string) to encode characters for image processing
export function textEncode(string) {
return string.replaceAll("&", "&amp;").replaceAll(">", "&gt;").replaceAll("<", "&lt;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;").replaceAll("\\n", "\n").replaceAll("\\:", ":");
}