1 | const randomBuffer = new Uint16Array(1);
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | function rand() {
|
8 | if (typeof crypto !== "undefined") {
|
9 | crypto.getRandomValues(randomBuffer);
|
10 | return randomBuffer[0] / 2**16;
|
11 | } else {
|
12 | return Math.random();
|
13 | }
|
14 | }
|
15 |
|
16 | const UUID = {
|
17 | |
18 |
|
19 |
|
20 | get() {
|
21 | const template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
|
22 | const value = template.replace(/[xy]/g, c => {
|
23 | let v = Math.floor(rand() * 16);
|
24 |
|
25 | if (c !== "x") {
|
26 | v = v & 0x3 | 0x8;
|
27 | }
|
28 |
|
29 | return v.toString(16);
|
30 | });
|
31 |
|
32 | return value;
|
33 | }
|
34 | };
|
35 |
|
36 | export default UUID; |
\ | No newline at end of file |