lib.utils: add parseHtmlEntities

This commit is contained in:
Cynthia Foxwell 2021-06-10 22:18:24 -06:00
parent 406e409a9f
commit ccce88fdad
1 changed files with 35 additions and 0 deletions

View File

@ -279,6 +279,40 @@ async function lookupUser(msg, str, filter) {
}
}
// https://stackoverflow.com/a/39243641
const htmlEntities = {
nbsp: " ",
cent: "¢",
pound: "£",
yen: "¥",
euro: "€",
copy: "©",
reg: "®",
lt: "<",
gt: ">",
quot: '"',
amp: "&",
apos: "'",
};
function parseHtmlEntities(str) {
return str.replace(/&([^;]+);/g, function (entity, entityCode) {
var match;
if (entityCode in htmlEntities) {
return htmlEntities[entityCode];
/*eslint no-cond-assign: 0*/
} else if ((match = entityCode.match(/^#x([\da-fA-F]+)$/))) {
return String.fromCharCode(parseInt(match[1], 16));
/*eslint no-cond-assign: 0*/
} else if ((match = entityCode.match(/^#(\d+)$/))) {
return String.fromCharCode(~~match[1]);
} else {
return entity;
}
});
}
module.exports = {
pastelize,
getTopColor,
@ -291,4 +325,5 @@ module.exports = {
hastebin,
selectionMessage,
lookupUser,
parseHtmlEntities,
};