Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/BaseCoder.web.ts
Original file line number Diff line number Diff line change
@@ -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);
};