siliconid/siliconid.js

23 lines
708 B
JavaScript

function toBase62(num) {
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let encoded = "";
while (num > 0) {
encoded = base62[num % 62] + encoded;
num = Math.floor(num / 62);
}
return encoded;
}
function generateID() {
const epochStart = new Date("2020-01-01").getTime(); // Epoch in ms
const now = Date.now();
let timestamp = (now - epochStart) % 1_000_000_000_000; // Keep last 12 digits
let randomNum = Math.floor(Math.random() * 100); // Random 2-digit number
let finalNum = timestamp * 100 + randomNum;
return toBase62(finalNum).padStart(8, "0");
}
console.log(generateID()); // Example output: "A1b2C3D4"