Created
August 25, 2018 12:13
-
-
Save leidegre/8608327cc999536a224b7d5e793fe667 to your computer and use it in GitHub Desktop.
gen-base32-id.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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