Created
October 28, 2013 06:23
-
-
Save jonypawks/7192173 to your computer and use it in GitHub Desktop.
Coffeescript UUID4
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
_random = | |
if crypto?.getRandomValues | |
-> crypto.getRandomValues(new Uint32Array(1))[0] / Math.pow(2, 32) | |
else | |
Math.random | |
uuid4 = -> | |
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace /[xy]/g, (c) -> | |
u = _random() * 16 | 0 | |
u = u & 0x3 | 0x8 if c is 'y' | |
u.toString 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment