Last active
January 3, 2016 00:19
-
-
Save alivesay/8381931 to your computer and use it in GitHub Desktop.
Fast Javascript RFC-compliant UUID generator.
This file contains hidden or 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
/** | |
* Generates an RFC4122-compliant v4 UUID. | |
* @returns {string} | |
*/ | |
function generateUUID() { | |
var timestamp = Date.now(), uuid = '', i = 36, c, | |
hexChars = '0123456789abcdef', | |
uuidPattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; | |
while (i--) { | |
c = uuidPattern.charAt(i); | |
if (c === 'x') { | |
uuid = hexChars.charAt(timestamp + Math.random() * 0xFF & 15)) + uuid; | |
} else if (c === '-' || c === '4') { | |
uuid = c + uuid; | |
} else { | |
uuid = hexChars.charAt(Math.random() * 4 + 8 | 0) + uuid; | |
} | |
timestamp >>= 2; | |
} | |
return uuid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 13 has a syntax error (parentheses).