const escapeEntities = new Map([ ["&", "&"], ["<", "<"], [">", ">"], ['"', """], ["'", "'"], ]); const unescapeEntities = new Map([ ["&", "&"], ["<", "<"], [">", ">"], [""", '"'], ["'", "'"], ]); const escapeKeys = new RegExp([...escapeEntities.keys()].join("|"), "g"); const unescapeKeys = new RegExp([...unescapeEntities.keys()].join("|"), "g"); /** Escape HTML entities */ export const escape = (str: string): string => str.replaceAll(escapeKeys, (k) => escapeEntities.get(k)!); /** Unescape HTML entities */ export const unescape = (str: string): string => str.replaceAll(unescapeKeys, (k) => unescapeEntities.get(k)!); /** Unescape and escape HTML entities */ export const reEscape = (str: string) => escape(unescape(str));