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); +};