const characterToHtmlEntityCode = { '¢' : 'cent' , '£' : 'pound' , '¥' : 'yen' , '€' : 'euro' , '©' : 'copy' , '®' : 'reg' , '™' : 'trade' , '<' : 'lt' , '>' : 'gt' , '"' : 'quot' , '&' : 'amp' , "'" : '#39' , } as const ; const regex = new RegExp ( `[${ Object . keys ( characterToHtmlEntityCode ) . join ( '' ) }]` , 'g' ) ; const toString = ( object ) : string => ( object ? `${ object }` : '' ) ; const isEscapable = ( char ) : char is keyof typeof characterToHtmlEntityCode => char in characterToHtmlEntityCode ; const escapeChar = ( char ) : string => isEscapable ( char ) ? `&${ characterToHtmlEntityCode [ char ] };` : '' ; export const escapeHTML = ( str ) : string => toString ( str ) . replace ( regex , escapeChar ) ;