Created
March 4, 2022 15:32
-
-
Save worldOneo/ae5357ca2f84f67e5e840dc9f28c8f5d to your computer and use it in GitHub Desktop.
Simple UID generator for JavaScript with logic to ensure uniqueness.
This file contains hidden or 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
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