From 81a1d80f11e100881d4bb39556d96e9000929b6a Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle <patrizio.bekerle@tugraz.at> Date: Mon, 29 Jul 2019 10:03:22 +0200 Subject: [PATCH] Add function base64EncodeUnicode --- packages/common/utils.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/common/utils.js b/packages/common/utils.js index fe95c7e6..435d82e5 100644 --- a/packages/common/utils.js +++ b/packages/common/utils.js @@ -53,4 +53,21 @@ module.exports = { list.forEach((item) => {data.push({id: item, text: item})}); return data; }, + + /** + * Does generic Base64 Encoding with support for 16-bit encoded strings + * @see https://www.base64encoder.io/javascript/ + * + * @param str + * @returns {string} + */ + base64EncodeUnicode: (str) => { + // First we escape the string using encodeURIComponent to get the UTF-8 encoding of the characters, + // then we convert the percent encodings into raw bytes, and finally feed it to btoa() function. + const utf8Bytes = encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { + return String.fromCharCode('0x' + p1); + }); + + return btoa(utf8Bytes); + } }; -- GitLab