/
uuid.js
54 lines (45 loc) · 1 KB
/
uuid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* RFC4122 version 4 compliant uuid generator.
*
* Check [RFC4122 4.4](http://www.ietf.org/rfc/rfc4122.txt) for reference.
*/
/* example
* uuid(); // -> '53ce0497-6554-49e9-8d79-347406d2a88b'
*/
/* module
* env: all
*/
/* typescript
* export declare function uuid(): string;
*/
_('randomBytes');
exports = function() {
const b = randomBytes(16);
b[6] = (b[6] & 0x0f) | 0x40;
b[8] = (b[8] & 0x3f) | 0x80;
return (
hexBytes[b[0]] +
hexBytes[b[1]] +
hexBytes[b[2]] +
hexBytes[b[3]] +
'-' +
hexBytes[b[4]] +
hexBytes[b[5]] +
'-' +
hexBytes[b[6]] +
hexBytes[b[7]] +
'-' +
hexBytes[b[8]] +
hexBytes[b[9]] +
'-' +
hexBytes[b[10]] +
hexBytes[b[11]] +
hexBytes[b[12]] +
hexBytes[b[13]] +
hexBytes[b[14]] +
hexBytes[b[15]]
);
};
const hexBytes = [];
for (let i = 0; i < 256; i++) {
hexBytes[i] = (i + 0x100).toString(16).substr(1);
}