UNPKG

1.9 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var global = require('../internals/global');
4var getBuiltIn = require('../internals/get-built-in');
5var uncurryThis = require('../internals/function-uncurry-this');
6var call = require('../internals/function-call');
7var fails = require('../internals/fails');
8var toString = require('../internals/to-string');
9var validateArgumentsLength = require('../internals/validate-arguments-length');
10var i2c = require('../internals/base64-map').i2c;
11
12var $btoa = getBuiltIn('btoa');
13var charAt = uncurryThis(''.charAt);
14var charCodeAt = uncurryThis(''.charCodeAt);
15
16var BASIC = !!$btoa && !fails(function () {
17 return $btoa('hi') !== 'aGk=';
18});
19
20var NO_ARG_RECEIVING_CHECK = BASIC && !fails(function () {
21 $btoa();
22});
23
24var WRONG_ARG_CONVERSION = BASIC && fails(function () {
25 return $btoa(null) !== 'bnVsbA==';
26});
27
28var WRONG_ARITY = BASIC && $btoa.length !== 1;
29
30// `btoa` method
31// https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa
32$({ global: true, bind: true, enumerable: true, forced: !BASIC || NO_ARG_RECEIVING_CHECK || WRONG_ARG_CONVERSION || WRONG_ARITY }, {
33 btoa: function btoa(data) {
34 validateArgumentsLength(arguments.length, 1);
35 // `webpack` dev server bug on IE global methods - use call(fn, global, ...)
36 if (BASIC) return call($btoa, global, toString(data));
37 var string = toString(data);
38 var output = '';
39 var position = 0;
40 var map = i2c;
41 var block, charCode;
42 while (charAt(string, position) || (map = '=', position % 1)) {
43 charCode = charCodeAt(string, position += 3 / 4);
44 if (charCode > 0xFF) {
45 throw new (getBuiltIn('DOMException'))('The string contains characters outside of the Latin1 range', 'InvalidCharacterError');
46 }
47 block = block << 8 | charCode;
48 output += charAt(map, 63 & block >> 8 - position % 1 * 8);
49 } return output;
50 }
51});