Skip to content

Instantly share code, notes, and snippets.

@nick1n
Created October 22, 2022 23:43
Show Gist options
  • Save nick1n/611a2220c9038b688d5b0eabf8dc1a46 to your computer and use it in GitHub Desktop.
Save nick1n/611a2220c9038b688d5b0eabf8dc1a46 to your computer and use it in GitHub Desktop.
Tiny cuid, not secure, fast, less than 170 bytes, uses a random session id instead of fingerprint.
let
text,
base36 = (number) => number.toString(36),
randomStr = (length) => {
for (
text = "";
text.length < length;
text = base36(Math.random()).slice(2, length + 2)
);
return text
},
session = randomStr(4),
count = 0,
cuid = () =>
"c" +
base36(Date.now()) +
("000" + base36(count++)).slice(-4) +
session +
randomStr(8)
let t,b=(n)=>n.toString(36),r=(l)=>{for(t="";t.length<l;t=b(Math.random()).slice(2,l+2));return t},s=r(4),c=0,cuid=()=>"c"+b(Date.now())+("000"+b(c++)).slice(-4)+s+r(8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment