1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var globalThis = require('../internals/global-this');
|
---|
4 | var getBuiltIn = require('../internals/get-built-in');
|
---|
5 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
6 | var call = require('../internals/function-call');
|
---|
7 | var fails = require('../internals/fails');
|
---|
8 | var toString = require('../internals/to-string');
|
---|
9 | var validateArgumentsLength = require('../internals/validate-arguments-length');
|
---|
10 | var i2c = require('../internals/base64-map').i2c;
|
---|
11 |
|
---|
12 | var $btoa = getBuiltIn('btoa');
|
---|
13 | var charAt = uncurryThis(''.charAt);
|
---|
14 | var charCodeAt = uncurryThis(''.charCodeAt);
|
---|
15 |
|
---|
16 | var BASIC = !!$btoa && !fails(function () {
|
---|
17 | return $btoa('hi') !== 'aGk=';
|
---|
18 | });
|
---|
19 |
|
---|
20 | var NO_ARG_RECEIVING_CHECK = BASIC && !fails(function () {
|
---|
21 | $btoa();
|
---|
22 | });
|
---|
23 |
|
---|
24 | var WRONG_ARG_CONVERSION = BASIC && fails(function () {
|
---|
25 | return $btoa(null) !== 'bnVsbA==';
|
---|
26 | });
|
---|
27 |
|
---|
28 | var 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, globalThis, 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 | });
|
---|