Base64 handling

Abstract
Although btoa() and atob() offer essential conversions between binary data and ASCII representation, we usually need additional wrappers to deal with the actual arrays that hold our data.
Encode
In a typical case, where our bytes are in an Array (ideally: Uint8Array), we convert them into a binary string before calling btoa():
b64str=btoa((Array.reduce((p,e)=>p+String.fromCharCode(e),'')))
Also, we don’t need any terminal padding '=' characters. Let’s strip them:
b64str=b64str.replace(/=+$/,'')

Decode
If the base64 string comes from an untrusted source (e.g. user input), it first should be sanitized by removing all characters that aren’t legal:
b64str=b64str.replace(/[^A-Za-z0-9+/]/g,'')
Finally, to recreate our array, we convert the binary string (returned by atob()) into integer values again:
Array=new Uint8Array(atob(b64str).split('').map(e=>e.charCodeAt()))

Checks
Let N be the number of Base64 characters.

• Check for correct number: if(N-1&3)

• Calc resulting number of data bytes: bytes=N-(N-1>>2)-1

• Calc needed number of Base64 characters: N=bytes+(bytes+2)/3|0

Calculator for data block sizes:
Data
bytes
Base64
chars
<=>