Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions js/__tests__/base64Utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const base64Utils = require('../base64Utils');
global.TextEncoder = require('util').TextEncoder;
global.TextDecoder = require('util').TextDecoder;

describe('base64Utils', () => {
test('base64Encode should correctly encode a string to Base64', () => {
expect(base64Utils.base64Encode('hello')).toBe('aGVsbG8=');
expect(base64Utils.base64Encode('Base64 Encoding!')).toBe('QmFzZTY0IEVuY29kaW5nIQ==');
expect(base64Utils.base64Encode('123456')).toBe('MTIzNDU2');
});

test('base64Decode should correctly decode a Base64 string', () => {
expect(base64Utils.base64Decode('aGVsbG8=')).toBe('hello');
expect(base64Utils.base64Decode('QmFzZTY0IEVuY29kaW5nIQ==')).toBe('Base64 Encoding!');
expect(base64Utils.base64Decode('MTIzNDU2')).toBe('123456');
});

test('base64Encode and base64Decode should be reversible', () => {
const originalText = 'Reversible Test!';
const encoded = base64Utils.base64Encode(originalText);
const decoded = base64Utils.base64Decode(encoded);
expect(decoded).toBe(originalText);
});

test('base64Encode should handle empty strings', () => {
expect(base64Utils.base64Encode('')).toBe('');
});

test('base64Decode should handle empty strings', () => {
expect(base64Utils.base64Decode('')).toBe('');
});
});
7 changes: 4 additions & 3 deletions js/base64Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function base64Encode(str) {
let encoder = new TextEncoder();
let uint8Array = encoder.encode(str);
let binaryString = String.fromCharCode(...uint8Array);
return (binaryString);
return btoa(binaryString); // Proper Base64 encoding
}

/**
Expand All @@ -27,5 +27,6 @@ function base64Decode(str) {
return decoder.decode(uint8Array);
}

export default { base64Encode, base64Decode };
//module.exports = { base64Encode, base64Decode };
if (typeof module !== 'undefined' && module.exports) {
module.exports = { base64Encode, base64Decode };
}