Last active
August 13, 2025 02:41
-
-
Save kafene/41257d8d82bf9ae23dde316049a4c618 to your computer and use it in GitHub Desktop.
Tiny JavaScript functions to entitize/deentitize (escape/unescape) HTML entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const [ entitize, deentitize ] = (charmap => [ | |
| h => charmap.reduce((v, [char, entity]) => v.replaceAll(char, entity), h), | |
| h => charmap.reduce((v, [char, entity]) => v.replaceAll(entity, char), h), | |
| ])([['&','&'],['<','<'],['>','>'],['"','"'],["'",''']]); | |
| // minified | |
| const [e,d]=(m=>[h=>m.reduce((v,[c,e])=>v.replaceAll(c,e),h),h=>m.reduce((v,[c,e])=>v.replaceAll(e,c),h)])([['&','&'],['<','<'],['>','>'],['"','"'],["'",''']]); | |
| // example | |
| var html = '<b>You & I really just "<i>get it</i>", don\'t we?</b>'; | |
| var entitized = '<b>You & I really just "<i>get it</i>", don't we?</b>'; | |
| console.log(entitize(html) === entitized, entitize(html)); | |
| console.log(deentitize(entitized) === html, deentitize(entitized)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment