UNPKG

@4players/odin-common

Version:

A collection of commonly used type definitions and utility functions across ODIN web projects

26 lines (25 loc) 852 B
import { failure, success } from './result'; export function fromBase64(base64) { try { const binString = atob(base64); /* @ts-ignore: strings are array-like */ const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0)); const decoder = new TextDecoder('utf8'); return success(decoder.decode(bytes)); } catch (error) { return failure(String(error)); } } export function fromBase64Url(base64uri) { try { const binString = atob(base64uri.replace(/-/g, '+').replace(/_/g, '/')); /* @ts-ignore: strings are array-like */ const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0)); const decoder = new TextDecoder('utf8'); return success(decoder.decode(bytes)); } catch (error) { return failure(String(error)); } }