Created
October 11, 2021 12:55
-
-
Save le0pard/5b90ee9569a3a9c5c14a9e5f8d007bdb to your computer and use it in GitHub Desktop.
Base64 in browser with Unicode support (no deprecated 'unescape' and 'escape' functions)
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 b64EncodeUnicode = (str) => ( | |
window.btoa(window.encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_match, p1) => ( | |
String.fromCharCode(`0x${p1}`) | |
))) | |
) | |
const b64DecodeUnicode = (str) => ( | |
window.decodeURIComponent(window.atob(str).split('').map((c) => ( | |
`%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}` | |
)).join('')) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment