Native base64 utility methods

Andrea Giammarchi andrea.giammarchi at gmail.com
Sun May 4 15:00:04 PDT 2014


+1 and as generic global utility it would be also nice to make it
compatible with all strings.

I have this good old object: http://devpro.it/code/214.html

that indeed needs to normalize before encoding or decoding or errors might
happen quite frequently in user-land:

{
        atob: atob,
        btoa: btoa,
        fromCharCode: fromCharCode,
        encode: function encode(string) {
            return btoa(unescape(encodeURIComponent(string)));
        },
        decode: function decode(string) {
            return decodeURIComponent(escape(atob(string)));
        },
        toDataURL: function toDataURL(string, mime) {
            return "data:" + (mime ? mime + ";" : "") + base +
base64.encode(string);
        },
        fromDataURL: function fromDataURL(string) {
            return base64.decode(string.slice(string.indexOf(base) +
base.length));
        }
    }


Take care



On Sun, May 4, 2014 at 2:16 PM, Mathias Bynens <mathias at qiwi.be> wrote:

> To convert from base64 to ASCII and vice versa, browsers have had global
> `atob` and `btoa` functions for a while now. At the moment, these are
> defined in the HTML standard: http://whatwg.org/html/webappapis.html#atob
>
> However, such utility methods are not only useful in browsers. How about
> adding these as global functions to ECMAScript so that they’re natively
> available in all JavaScript engines, not just in browser environments?
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140504/cee39727/attachment.html>


More information about the es-discuss mailing list