From 7402aad10e1b59d2c6e87b85b69cc9368f12e5d7 Mon Sep 17 00:00:00 2001 From: Sasha Ilieva <sasha.ilieva@vereign.com> Date: Mon, 27 May 2019 12:46:28 +0300 Subject: [PATCH] Move string methods to stringUtilities file --- javascript/src/iframe/stringUtilities.js | 53 ++++++++++++++++++++ javascript/src/iframe/viamapi-iframe.js | 62 +++--------------------- 2 files changed, 61 insertions(+), 54 deletions(-) create mode 100644 javascript/src/iframe/stringUtilities.js diff --git a/javascript/src/iframe/stringUtilities.js b/javascript/src/iframe/stringUtilities.js new file mode 100644 index 0000000..a369130 --- /dev/null +++ b/javascript/src/iframe/stringUtilities.js @@ -0,0 +1,53 @@ +export function stringToUtf8ByteArray(str) { + if (typeof str !== 'string') { + str = str.toString(); + } + const res = Buffer.from(str, 'utf-8'); + return res; +} + +export function utf8ByteArrayToString(ba) { + if (!Buffer.isBuffer(ba)) { + ba = Buffer.from(ba); + } + const res = ba.toString('utf-8'); + return res; +} + +export function stringToUtf8Base64(str) { + if (!Buffer.isBuffer(str)) { + if (typeof str !== 'string') { + str = str.toString(); + } + str = Buffer.from(str, 'utf-8'); + } + const res = str.toString('base64'); + return res; +} + +export function utf8Base64ToString(strBase64) { + if (!Buffer.isBuffer(strBase64)) { + if (typeof strBase64 !== 'string') { + strBase64 = strBase64.toString(); + } + strBase64 = Buffer.from(strBase64, 'base64'); + } + const res = strBase64.toString('utf-8'); + return res; +} + +export function base64ToByteArray(strBase64) { + if (typeof strBase64 !== 'string') { + strBase64 = strBase64.toString(); + } + const res = Buffer.from(strBase64, 'base64'); + return res; +} + +export function byteArrayToBase64(ba) { + if (!Buffer.isBuffer(ba)) { + ba = Buffer.from(ba); + } + const res = ba.toString('base64'); + return res; +} diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js index 0d75a88..cf1d5e1 100644 --- a/javascript/src/iframe/viamapi-iframe.js +++ b/javascript/src/iframe/viamapi-iframe.js @@ -1,4 +1,12 @@ import { parseSMIME } from '../utilities/emailUtilities'; +import { + stringToUtf8ByteArray, + utf8ByteArrayToString, + stringToUtf8Base64, + utf8Base64ToString, + base64ToByteArray, + byteArrayToBase64 +} from '../utilities/stringUtilities'; const QRCode = require('qrcode'); const Penpal = require('penpal').default; @@ -434,60 +442,6 @@ function getCertificateForPassport(passportUUID, internal) { }); } -function stringToUtf8ByteArray(str) { - if (typeof str !== 'string') { - str = str.toString(); - } - const res = Buffer.from(str, 'utf-8'); - return res; -} - -function utf8ByteArrayToString(ba) { - if (!Buffer.isBuffer(ba)) { - ba = Buffer.from(ba); - } - const res = ba.toString('utf-8'); - return res; -} - -function stringToUtf8Base64(str) { - if (!Buffer.isBuffer(str)) { - if (typeof str !== 'string') { - str = str.toString(); - } - str = Buffer.from(str, 'utf-8'); - } - const res = str.toString('base64'); - return res; -} - -function utf8Base64ToString(strBase64) { - if (!Buffer.isBuffer(strBase64)) { - if (typeof strBase64 !== 'string') { - strBase64 = strBase64.toString(); - } - strBase64 = Buffer.from(strBase64, 'base64'); - } - const res = strBase64.toString('utf-8'); - return res; -} - -function base64ToByteArray(strBase64) { - if (typeof strBase64 !== 'string') { - strBase64 = strBase64.toString(); - } - const res = Buffer.from(strBase64, 'base64'); - return res; -} - -function byteArrayToBase64(ba) { - if (!Buffer.isBuffer(ba)) { - ba = Buffer.from(ba); - } - const res = ba.toString('base64'); - return res; -} - const connection = Penpal.connectToParent({ // Methods child is exposing to parent methods: { -- GitLab