Skip to content

Instantly share code, notes, and snippets.

@C-F-K
Last active December 19, 2019 16:30
Show Gist options
  • Save C-F-K/43f3dff00d79103bb3047fc87fd0a632 to your computer and use it in GitHub Desktop.
Save C-F-K/43f3dff00d79103bb3047fc87fd0a632 to your computer and use it in GitHub Desktop.
RFC4122-compliant v4 UUID using crypto-strong randomness, code so functional Haskell B. Curry would fucking WEEP
const [Bx,Hx,Qx] = [()=>crypto.getRandomValues(new Uint8Array(1))[0],x=>[...Array(x)].map(Bx),m=>n=>x=>y=>y<=n&&y>=m?y:Qx(m)(n)(x)(x())];
export const guid = ()=>[Hx(4),Hx(2),[Qx(0b01000000)(0b01001111)(Bx)(Bx()),Bx()],[Qx(0b10000000)(0b10111111)(Bx)(Bx()),Bx()],Hx(6)].map(a=>a.map(a=>a.toString(16).padStart(2,'0')).reduce((a,b)=>a.concat(b),'')).join('-');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment