Skip to content

Instantly share code, notes, and snippets.

@worldOneo
Created March 4, 2022 15:32
Show Gist options
  • Save worldOneo/ae5357ca2f84f67e5e840dc9f28c8f5d to your computer and use it in GitHub Desktop.
Save worldOneo/ae5357ca2f84f67e5e840dc9f28c8f5d to your computer and use it in GitHub Desktop.
Simple UID generator for JavaScript with logic to ensure uniqueness.
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890+@".split("");
const maxBase = n => {
let r = "";
while (n) {
r += chars[n & 63];
n = n/64|0;
}
return r;
};
class Generator {
counter = 0;
newId = () => {
this.counter &= 2048;
this.counter++;
return maxBase(Date.now() - 1646400000000) + maxBase(this.counter);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment