forked from tus/tus-js-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.js
20 lines (20 loc) · 773 Bytes
/
uuid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Generate a UUID v4 based on random numbers. We intentioanlly use the less
* secure Math.random function here since the more secure crypto.getRandomNumbers
* is not available on all platforms.
* This is not a problem for us since we use the UUID only for generating a
* request ID, so we can correlate server logs to client errors.
*
* This function is taken from following site:
* https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
*
* @return {string} The generate UUID
*/
export default function uuid() {
/* eslint-disable no-bitwise */
return '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)
})
}