Public
Edited
Sep 19, 2024
Insert cell
Insert cell
Insert cell
Insert cell
btoa(input) // it's longer
Insert cell
Insert cell
input.toString(16) // basically the same size
Insert cell
input.toString(36) // a little bit shorter
Insert cell
input.toString(64) // lame!
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let bytes = new TextEncoder().encode(String(input))
const binString = Array.from(bytes, (byte) =>
String.fromCodePoint(byte),
).join("");
return btoa(binString) // oh, there it is
}
Insert cell
Insert cell
customBase64(input) // snippet from the internet
Insert cell
Insert cell
customBase64 = function(num) {
const BASE_BITS = 6
const alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"

let log = Math.log2(num)
if (Math.pow(2, Math.round(log)) === num) {
log++
}
let length = Math.max(1, Math.ceil(log / BASE_BITS))

let chars = new Array(length)
let x = chars.length - 1
while (num > 0) {
chars[x--] = alphabet[num % BASE]
num = Math.floor(num / BASE)
}
while (x >= 0) {
chars[x--] = alphabet[0]
}
return chars.join('')
}

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
BASE = 64
Insert cell
BASE_BITS = 6
Insert cell
Insert cell
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