Skip to content

Instantly share code, notes, and snippets.

@tony-0tis
Last active June 24, 2024 10:02
Show Gist options
  • Save tony-0tis/816bc579e30c134e388478ca36ece750 to your computer and use it in GitHub Desktop.
Save tony-0tis/816bc579e30c134e388478ca36ece750 to your computer and use it in GitHub Desktop.
UUID generator provides good uniqueness guarantees

0tis-uuid4

UUID generator provides good uniqueness guarantees

Install

npm i -S gist:816bc579e30c134e388478ca36ece750

How to use

import uuidv4 from '0tis-uuid4';
uuidv4();
{
"version": "0.1.0",
"name": "0tis-uuid4",
"main": "uuid4.js",
}
function uuidv4() {
// by broofa - https://stackoverflow.com/a/2117523
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
export default uuidv4;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment