Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

esnext.uint8-array.to-base64.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. var base64Alphabet = base64Map.i2c;
  11. var base64UrlAlphabet = base64Map.i2cUrl;
  12. var charAt = uncurryThis(''.charAt);
  13. // `Uint8Array.prototype.toBase64` method
  14. // https://github.com/tc39/proposal-arraybuffer-base64
  15. if (globalThis.Uint8Array) $({ target: 'Uint8Array', proto: true }, {
  16. toBase64: function toBase64(/* options */) {
  17. var array = anUint8Array(this);
  18. var options = arguments.length ? anObjectOrUndefined(arguments[0]) : undefined;
  19. var alphabet = getAlphabetOption(options) === 'base64' ? base64Alphabet : base64UrlAlphabet;
  20. var omitPadding = !!options && !!options.omitPadding;
  21. notDetached(this.buffer);
  22. var result = '';
  23. var i = 0;
  24. var length = array.length;
  25. var triplet;
  26. var at = function (shift) {
  27. return charAt(alphabet, (triplet >> (6 * shift)) & 63);
  28. };
  29. for (; i + 2 < length; i += 3) {
  30. triplet = (array[i] << 16) + (array[i + 1] << 8) + array[i + 2];
  31. result += at(3) + at(2) + at(1) + at(0);
  32. }
  33. if (i + 2 === length) {
  34. triplet = (array[i] << 16) + (array[i + 1] << 8);
  35. result += at(3) + at(2) + at(1) + (omitPadding ? '' : '=');
  36. } else if (i + 1 === length) {
  37. triplet = array[i] << 16;
  38. result += at(3) + at(2) + (omitPadding ? '' : '==');
  39. }
  40. return result;
  41. }
  42. });