From 37048660814c664dc5178d5e8dd43763c1245f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Barbet?= Date: Wed, 11 May 2022 08:10:37 +0000 Subject: [PATCH] wip --- src/BaseCoder.web.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/BaseCoder.web.ts diff --git a/src/BaseCoder.web.ts b/src/BaseCoder.web.ts new file mode 100644 index 0000000..6af7f22 --- /dev/null +++ b/src/BaseCoder.web.ts @@ -0,0 +1,33 @@ +import { Algorithm, Config } from './types/global'; + +const isBrowser = typeof window !== `undefined`; + +export const encode = ( + stringToEncode: string, + config: Config = { algorithm: Algorithm.base64Rfc4648 }, +) => { + if (!isBrowser) { + throw new Error(`Can only use BaseCoder in the browser.`); + } + + if (config.algorithm !== Algorithm.base64Rfc4648) { + throw new Error(`Other algorithms than base64Rfc4648 are not supported yet.`); + } + + return btoa(stringToEncode); +}; + +export const decode = ( + bytesToEncode: string, + config: Config = { algorithm: Algorithm.base64Rfc4648 }, +) => { + if (!isBrowser) { + throw new Error(`Can only use BaseCoder in the browser.`); + } + + if (config.algorithm !== Algorithm.base64Rfc4648) { + throw new Error(`Other algorithms than base64Rfc4648 are not supported yet.`); + } + + return atob(bytesToEncode); +};