Skip to content

Commit

Permalink
Add base64 functions to utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
TekMonksGitHub committed Dec 2, 2023
1 parent f133c4c commit c618a0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build_number
Original file line number Diff line number Diff line change
@@ -1 +1 @@
768
769
24 changes: 23 additions & 1 deletion frontend/framework/js/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,28 @@ const createAsyncFunction = code => {
return newFunction;
}

/**
* Converts unicode string to Base64.
* @param {string} text The unicode string to convert to Base64.
* @returns Base64 of the string
*/
function stringToBase64(text) {
const bytes = new TextEncoder().encode(text);
const binString = String.fromCodePoint(...bytes);
return btoa(binString);
}

/**
* Converts base64 back to unicode string.
* @param {string} base64 Base 64 text
* @returns The original string in unicode.
*/
function base64ToString(base64) {
const binString = atob(base64);
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
return new TextDecoder().decode(bytes);
}

export const util = {getCSSRule, getFunctionFromString, replaceURLParamValue, parseBoolean, escapeHTML, getModulePath,
downloadFile, uploadAFile, getFileData, clone, resolveURL, baseURL, safeURIDecode, getChildByID, getChildrenByTagName,
removeAllChildElements, setIntervalImmediately, generateUUID, createAsyncFunction}
removeAllChildElements, setIntervalImmediately, generateUUID, createAsyncFunction, stringToBase64, base64ToString};

0 comments on commit c618a0e

Please sign in to comment.