-
Notifications
You must be signed in to change notification settings - Fork 969
/
Copy pathbase64Utils.js
32 lines (29 loc) · 957 Bytes
/
base64Utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Provides utility functions for Base64 encoding and decoding.
* @module base64Utils
*/
/**
* Encodes a string to Base64 format.
* @param {string} str - The string to encode.
* @returns {string} - The Base64 encoded string.
*/
function base64Encode(str) {
let encoder = new TextEncoder();
let uint8Array = encoder.encode(str);
let binaryString = String.fromCharCode(...uint8Array);
return btoa(binaryString); // Proper Base64 encoding
}
/**
* Decodes a Base64 encoded string.
* @param {string} str - The Base64 encoded string.
* @returns {string} - The decoded string.
*/
function base64Decode(str) {
let binaryString = window.atob(str);
let uint8Array = new Uint8Array(binaryString.split('').map(char => char.charCodeAt(0)));
let decoder = new TextDecoder();
return decoder.decode(uint8Array);
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = { base64Encode, base64Decode };
}