Skip to content

Commit

Permalink
Move the fix to base64-util instead
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Feb 10, 2024
1 parent 7fb155a commit 6bb02ee
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/extensions/scratch3_wedo2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ class WeDo2 {
return this._ble.write(
BLEService.IO_SERVICE,
uuid,
Base64Util.uint8ArrayToBase64(new Uint8Array(message)),
Base64Util.uint8ArrayToBase64(message),
'base64'
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/util/base64-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ class Base64Util {

/**
* Convert a Uint8Array to a base64 encoded string.
* @param {Uint8Array} array - the array to convert.
* @param {Uint8Array|Array<number>} array - the array to convert.
* @return {string} - the base64 encoded string.
*/
static uint8ArrayToBase64 (array) {
if (Array.isArray(array)) {
array = new Uint8Array(array);
}
let binary = '';
const len = array.byteLength;
for (let i = 0; i < len; i++) {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/util_base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Base64Util = require('../../src/util/base64-util');

test('uint8ArrayToBase64', t => {
t.equal(Base64Util.uint8ArrayToBase64(new Uint8Array([0, 50, 80, 200])), 'ADJQyA==');
t.equal(Base64Util.uint8ArrayToBase64([0, 50, 80, 200]), 'ADJQyA==');
t.end();
});

Expand All @@ -29,7 +30,9 @@ test('round trips', t => {
];
for (const uint8array of data) {
const uint8ToBase64 = Base64Util.uint8ArrayToBase64(uint8array);
const arrayToBase64 = Base64Util.uint8ArrayToBase64(Array.from(uint8array));
const bufferToBase64 = Base64Util.arrayBufferToBase64(uint8array.buffer);
t.equal(uint8ToBase64, arrayToBase64);
t.equal(uint8ToBase64, bufferToBase64);
const decoded = Base64Util.base64ToUint8Array(uint8ToBase64);
t.same(uint8array, decoded);
Expand Down

0 comments on commit 6bb02ee

Please sign in to comment.