Base62 Encoding for Compact Identifiers
Encode non-negative integers as short Base62 strings and decode them back with BigInt, rejecting invalid input.
Code
const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const BASE = 62n;
export function encodeBase62(value: bigint | number): string {
if (typeof value !== "bigint" && typeof value !== "number") {
throw new TypeError("value must be a bigint or integer number");
}
if (typeof value === "number" && !Number.isInteger(value)) {
throw new TypeError("number input must be an integer");
}
let remaining = typeof value === "number" ? BigInt(value) : value;
if (remaining < 0n) throw new RangeError("value must be non-negative");
if (remaining === 0n) return "0";
let output = "";
while (remaining > 0n) {
output = ALPHABET[Number(remaining % BASE)] + output;
remaining /= BASE;
}
return output;
}
export function decodeBase62(input: string): bigint {
if (typeof input !== "string" || input.length === 0) {
throw new TypeError("input must be a non-empty string");
}
let result = 0n;
for (const char of input) {
const index = ALPHABET.indexOf(char);
if (index === -1) throw new RangeError("invalid base62 character: " + char);
result = result * BASE + BigInt(index);
}
return result;
}What the code does
Base62 Encoding for Compact Identifiers keeps everything in the functions encodeBase62 and decodeBase62.
The nesting pairs while (remaining > 0n) with for (const char of input) — the inner runs once per outer step. Invalid states are rejected up front: it throws TypeError and RangeError instead of continuing with bad data.
What to watch for
- The guard
typeof value !== "bigint" && typeof value !== "number"short-circuits the flow with an earlythrowwhen its condition holds.
Applying it
Signature: export function encodeBase62(value: bigint | number)
Import encodeBase62 from the file and call it — the rest of the code is implementation detail.
Statements that matter
if (index === -1) throw new RangeError("invalid base62 character: " + char);
if (remaining < 0n) throw new RangeError("value must be non-negative");
throw new TypeError("value must be a bigint or integer number");
Interactive Tools for This Topic
ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.ASCII85 EncoderEncode and decode ASCII85 (Base85), the Adobe variant with z zero-compression, handling arbitrary bytes and Unicode.Base32 Encoder & DecoderEncode and decode RFC 4648 Base32 with padding, fully UTF-8 safe so Unicode text round-trips without corruption. Nothing leaves your browser.Base45 Encoder and DecoderEncode and decode text with the Base45 alphabet defined in RFC 9285, with UTF-8 support, padding-free output, and clear validation errors.
FAQ
- What happens if the preconditions do not hold?
- The code throws `TypeError` and `RangeError` — the guard `typeof value !== "bigint" && typeof value !== "number"` rejects bad states before the main logic runs. Handle it at the call site or let it propagate deliberately.
- Is the dependency story complicated?
- Not at all: zero dependencies, so there is nothing to version or audit. The file defines `encodeBase62`, `decodeBase62` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.