Public
Edited
May 3, 2023
Fork of Base64
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
update = {
const decoder = new TextDecoder("utf-8");
const encoder = new TextEncoder("utf-8");
const redecode = () => viewof decoded.value = decoder.decode(decode(viewof encoded.value));
const reencode = () => viewof encoded.value = encode(encoder.encode(viewof decoded.value));
redecode();
viewof encoded.addEventListener("input", redecode);
viewof decoded.addEventListener("input", reencode);
invalidation.then(() => {
viewof encoded.removeEventListener("input", redecode);
viewof decoded.removeEventListener("input", reencode);
});
}
Insert cell
function decode(base64) {
let len = base64.length;
let bufferLength = len * 0.75;
let i;
let p = 0;
if (base64[base64.length - 1] === "=") {
--bufferLength;
if (base64[base64.length - 2] === "=") {
--bufferLength;
}
}
let bytes = new Uint8Array(bufferLength);
for (i = 0; i < len; i+=4) {
const encoded1 = lookup[base64.charCodeAt(i)];
const encoded2 = lookup[base64.charCodeAt(i+1)];
const encoded3 = lookup[base64.charCodeAt(i+2)];
const encoded4 = lookup[base64.charCodeAt(i+3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return bytes.buffer;
}
Insert cell
function encode(buffer) {
let bytes = new Uint8Array(buffer);
let len = bytes.length;
let i;
let base64 = "";
for (i = 0; i < len; i+=3) {
base64 += chars[bytes[i] >> 2];
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
base64 += chars[bytes[i + 2] & 63];
}
if ((len % 3) === 2) {
base64 = base64.slice(0, -1) + "=";
} else if (len % 3 === 1) {
base64 = base64.slice(0, -2) + "==";
}
return base64;
}
Insert cell
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Insert cell
lookup = {
const lookup = new Uint8Array(256);
for (let i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
return lookup;
}
Insert cell
data = fetch(
"https://gist.githubusercontent.com/Rub21/9383cefdbdd83fe65f717677391746e2/raw/04e7833da4f1f4669d497135a2bea201b51d0c0e/encoded_masks_string.json"
).then((r) => r.json())
Insert cell
decoder = new TextDecoder("utf-8")
Insert cell
decoder.decode(decode(data.masks))
Insert cell
encoder = new TextEncoder("utf-8")
Insert cell
encoder.encode(data.masks)
Insert cell
function b64_to_utf8(str) {
return decodeURIComponent(escape(window.atob(str)));
}
Insert cell
btoa(data.masks)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more