Skip to content

Instantly share code, notes, and snippets.

@leidegre
Created August 25, 2018 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leidegre/8608327cc999536a224b7d5e793fe667 to your computer and use it in GitHub Desktop.
Save leidegre/8608327cc999536a224b7d5e793fe667 to your computer and use it in GitHub Desktop.
gen-base32-id.js
function genBase32Id() {
// Math.pow(2, 40): 1099511627776
const x = Math.floor(Math.random() * 1099511627776).toString(32) // 40 bits
// padding
switch (x.length) {
case 8:
return x
case 7:
return "0" + x
case 6:
return "00" + x
case 5:
return "000" + x
case 4:
return "0000" + x
case 3:
return "00000" + x
case 2:
return "000000" + x
case 1:
return "0000000" + x
}
throw new Error()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment