Skip to content

Instantly share code, notes, and snippets.

@FlameWolf
Last active October 26, 2023 05:29
Show Gist options
  • Save FlameWolf/1502afc6621b5da9327e89fd53cae572 to your computer and use it in GitHub Desktop.
Save FlameWolf/1502afc6621b5da9327e89fd53cae572 to your computer and use it in GitHub Desktop.
Convert a BigInt to a base-62 number representation where the digits include (in the ascending order of value): 0-9, a-z, and A-Z
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}`;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment