diff --git a/Makefile b/Makefile index 433cf37..7542b65 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,9 @@ lint: $(ESLINT) \ --config node_modules/sanctuary-style/eslint-es3.json \ --global $$ \ + --global define \ --global exports \ + --global module \ --global self \ --rule 'max-len: [off]' \ --rule 'no-plusplus: [off]' \ diff --git a/README.md b/README.md index 8720b30..1b1970c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![CDNJS](https://img.shields.io/cdnjs/v/Base64.svg)](https://cdnjs.com/libraries/Base64) -≈ 600 byte* polyfill for browsers which don't provide [`window.btoa`][1] and +≈ 700 byte* polyfill for browsers which don't provide [`window.btoa`][1] and [`window.atob`][2]. Base64.js stems from a [gist][3] by [yahiko][4]. diff --git a/base64.js b/base64.js index 57e1b04..e558553 100644 --- a/base64.js +++ b/base64.js @@ -1,18 +1,23 @@ -(function() { +(function(f) { 'use strict'; - var object = ( - // #34: CommonJS - typeof exports === 'object' && exports != null && - typeof exports.nodeType !== 'number' ? - exports : - // #8: web workers - typeof self !== 'undefined' ? - self : - // #31: ExtendScript - $.global - ); + /* istanbul ignore else */ + if (typeof exports === 'object' && exports != null && + typeof exports.nodeType !== 'number') { + module.exports = f (); + } else if (typeof define === 'function' && define.amd != null) { + define ([], f); + } else { + var base64 = f (); + var global = typeof self !== 'undefined' ? self : $.global; + if (typeof global.btoa !== 'function') global.btoa = base64.btoa; + if (typeof global.atob !== 'function') global.atob = base64.atob; + } + +} (function() { + + 'use strict'; var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; @@ -24,8 +29,7 @@ // encoder // [https://gist.github.com/999166] by [https://github.com/nignag] - object.btoa || ( - object.btoa = function(input) { + function btoa(input) { var str = String (input); for ( // initialize result and counter @@ -44,12 +48,11 @@ block = block << 8 | charCode; } return output; - }); + } // decoder // [https://gist.github.com/1020396] by [https://github.com/atk] - object.atob || ( - object.atob = function(input) { + function atob(input) { var str = (String (input)).replace (/[=]+$/, ''); // #31: ExtendScript bad parse of /= if (str.length % 4 === 1) { throw new InvalidCharacterError ("'atob' failed: The string to be decoded is not correctly encoded."); @@ -69,6 +72,8 @@ buffer = chars.indexOf (buffer); } return output; - }); + } + + return {btoa: btoa, atob: atob}; -} ()); +}));