Created
October 17, 2023 15:31
-
-
Save kevboutin/810bfe7b208f30e0bdfd2e3fb31f06d6 to your computer and use it in GitHub Desktop.
Generate a random UUID
This file contains 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
/* This can be done by the uuid package, which is probably better than using Math.random(), but this is great for unit tests. */ | |
const generateRandomUUID = (a) => | |
a | |
? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16) | |
: ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace( | |
/[018]/g, | |
generateRandomUUID | |
); | |
console.log(generateRandomUUID()); // f138f635-acbd-4f78-9be5-ca3198c4cf34 | |
console.log(generateRandomUUID()); // 8935bb0d-6503-441f-bb25-7bc685b5b5bc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment