Last active
October 26, 2023 05:30
-
-
Save FlameWolf/7753810e9990dc18e45c7043098a36cd to your computer and use it in GitHub Desktop.
Generate random ID string V3
This file contains 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
Object.defineProperty(BigInt.prototype, "toBase62String", { | |
value: function () { | |
const base = 62n; | |
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
const sign = this < 0n ? "-" : ""; | |
let [result, quotient] = ["", sign ? 0n - this : this]; | |
do { | |
result = `${digits[Number(quotient % base)]}${result}`; | |
} while (quotient /= base); | |
return `${sign}${result}`; | |
} | |
}); | |
const getRandomIdString = (steps = 4) => { | |
let seed = ""; | |
for (let i = 0; i < steps; i++) { | |
seed += Math.random().toString().substring(2); | |
} | |
return BigInt(seed).toBase62String(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment