-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
35 additions
and
25 deletions.
There are no files selected for viewing
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,22 @@ | ||
/** | ||
* Source: [jed's gist]{@link https://gist.github.com/982883}. | ||
* Source: [jed's gist's comment]{@link https://gist.github.com/jed/982883?permalink_comment_id=3223002#gistcomment-3223002}. | ||
* Returns a random v4 UUID of the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, | ||
* where each x is replaced with a random hexadecimal digit from 0 to f, and | ||
* y is replaced with a random hexadecimal digit from 8 to b. | ||
* Used to generate UUIDs for deviceIds. | ||
* @private | ||
*/ | ||
var uuid = function (a) { | ||
return a // if the placeholder was passed, return | ||
? // a random number from 0 to 15 | ||
( | ||
a ^ // unless b is 8, | ||
((Math.random() * // in which case | ||
16) >> // a random number from | ||
(a / 4)) | ||
) // 8 to 11 | ||
.toString(16) // in hexadecimal | ||
: // or otherwise a concatenated string: | ||
( | ||
[1e7] + // 10000000 + | ||
-1e3 + // -1000 + | ||
-4e3 + // -4000 + | ||
-8e3 + // -80000000 + | ||
-1e11 | ||
) // -100000000000, | ||
.replace( | ||
// replacing | ||
/[018]/g, // zeroes, ones, and eights with | ||
uuid, // random hex digits | ||
); | ||
|
||
// hoist hex table out of the function to avoid re-calculation | ||
const hex = [...Array(256).keys()].map((index) => index.toString(16).padStart(2, '0')); | ||
|
||
var uuid = () => { | ||
const r = crypto.getRandomValues(new Uint8Array(16)); | ||
|
||
r[6] = (r[6] & 0x0f) | 0x40; | ||
r[8] = (r[8] & 0x3f) | 0x80; | ||
|
||
return [...r.entries()].map(([index, int]) => ([4, 6, 8, 10].includes(index) ? `-${hex[int]}` : hex[int])).join(''); | ||
}; | ||
|
||
export default 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 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