Last active
April 29, 2017 02:44
-
-
Save WebReflection/ed05681fe73ece4917de850c2c3bd7a1 to your computer and use it in GitHub Desktop.
Quite possibly the smallest base 64 utility out there.
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
// base64.decode(base64.encode('💩')); | |
// Browsers | |
const base64 = { | |
encode: str => btoa(unescape(encodeURIComponent(str))), | |
decode: str => decodeURIComponent(escape(atob(str))) | |
}; | |
/* ES3 compat version | |
var base64 = { | |
encode: function (str) { | |
return btoa(unescape(encodeURIComponent(str))); | |
}, | |
decode: function (str) { | |
return decodeURIComponent(escape(atob(str))); | |
} | |
}; | |
*/ | |
// NodeJS | |
const base64 = { | |
encode: str => Buffer.from(str).toString('base64'), | |
decode: str => Buffer.from(str, 'base64').toString() | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As single module for both browsers and node