1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var globalThis = require('../internals/global-this');
|
---|
4 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
5 | var anObjectOrUndefined = require('../internals/an-object-or-undefined');
|
---|
6 | var anUint8Array = require('../internals/an-uint8-array');
|
---|
7 | var notDetached = require('../internals/array-buffer-not-detached');
|
---|
8 | var base64Map = require('../internals/base64-map');
|
---|
9 | var getAlphabetOption = require('../internals/get-alphabet-option');
|
---|
10 |
|
---|
11 | var base64Alphabet = base64Map.i2c;
|
---|
12 | var base64UrlAlphabet = base64Map.i2cUrl;
|
---|
13 |
|
---|
14 | var charAt = uncurryThis(''.charAt);
|
---|
15 |
|
---|
16 | // `Uint8Array.prototype.toBase64` method
|
---|
17 | // https://github.com/tc39/proposal-arraybuffer-base64
|
---|
18 | if (globalThis.Uint8Array) $({ target: 'Uint8Array', proto: true }, {
|
---|
19 | toBase64: function toBase64(/* options */) {
|
---|
20 | var array = anUint8Array(this);
|
---|
21 | var options = arguments.length ? anObjectOrUndefined(arguments[0]) : undefined;
|
---|
22 | var alphabet = getAlphabetOption(options) === 'base64' ? base64Alphabet : base64UrlAlphabet;
|
---|
23 | var omitPadding = !!options && !!options.omitPadding;
|
---|
24 | notDetached(this.buffer);
|
---|
25 |
|
---|
26 | var result = '';
|
---|
27 | var i = 0;
|
---|
28 | var length = array.length;
|
---|
29 | var triplet;
|
---|
30 |
|
---|
31 | var at = function (shift) {
|
---|
32 | return charAt(alphabet, (triplet >> (6 * shift)) & 63);
|
---|
33 | };
|
---|
34 |
|
---|
35 | for (; i + 2 < length; i += 3) {
|
---|
36 | triplet = (array[i] << 16) + (array[i + 1] << 8) + array[i + 2];
|
---|
37 | result += at(3) + at(2) + at(1) + at(0);
|
---|
38 | }
|
---|
39 | if (i + 2 === length) {
|
---|
40 | triplet = (array[i] << 16) + (array[i + 1] << 8);
|
---|
41 | result += at(3) + at(2) + at(1) + (omitPadding ? '' : '=');
|
---|
42 | } else if (i + 1 === length) {
|
---|
43 | triplet = array[i] << 16;
|
---|
44 | result += at(3) + at(2) + (omitPadding ? '' : '==');
|
---|
45 | }
|
---|
46 |
|
---|
47 | return result;
|
---|
48 | }
|
---|
49 | });
|
---|