Skip to content

Instantly share code, notes, and snippets.

@jestho
Created April 24, 2020 15:29
Show Gist options
  • Save jestho/00f08326722085c7e7e1a2b59c1d71ef to your computer and use it in GitHub Desktop.
Save jestho/00f08326722085c7e7e1a2b59c1d71ef to your computer and use it in GitHub Desktop.
type UUID = string & { UUID: never };
export const uuid = () =>
"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
}) as UUID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment