lib.utils: add parseHtmlEntities
This commit is contained in:
		
							parent
							
								
									406e409a9f
								
							
						
					
					
						commit
						ccce88fdad
					
				
					 1 changed files with 35 additions and 0 deletions
				
			
		| 
						 | 
					@ -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 = {
 | 
					module.exports = {
 | 
				
			||||||
  pastelize,
 | 
					  pastelize,
 | 
				
			||||||
  getTopColor,
 | 
					  getTopColor,
 | 
				
			||||||
| 
						 | 
					@ -291,4 +325,5 @@ module.exports = {
 | 
				
			||||||
  hastebin,
 | 
					  hastebin,
 | 
				
			||||||
  selectionMessage,
 | 
					  selectionMessage,
 | 
				
			||||||
  lookupUser,
 | 
					  lookupUser,
 | 
				
			||||||
 | 
					  parseHtmlEntities,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue