1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | /*-----------------------------------------------------------------------------
|
4 | | Copyright (c) 2014-2017, PhosphorJS Contributors
|
5 | |
|
6 | | Distributed under the terms of the BSD 3-Clause License.
|
7 | |
|
8 | | The full license is in the file LICENSE, distributed with this software.
|
9 | |----------------------------------------------------------------------------*/
|
10 | var random_1 = require("./random");
|
11 | /**
|
12 | * The namespace for UUID related functionality.
|
13 | */
|
14 | var UUID;
|
15 | (function (UUID) {
|
16 | /**
|
17 | * A function which generates UUID v4 identifiers.
|
18 | *
|
19 | * @returns A new UUID v4 string.
|
20 | *
|
21 | * #### Notes
|
22 | * This implementation complies with RFC 4122.
|
23 | *
|
24 | * This uses `Random.getRandomValues()` for random bytes, which in
|
25 | * turn will use the underlying `crypto` module of the platform if
|
26 | * it is available. The fallback for randomness is `Math.random`.
|
27 | */
|
28 | UUID.uuid4 = (function () {
|
29 | // Create a 16 byte array to hold the random values.
|
30 | var bytes = new Uint8Array(16);
|
31 | // Create a look up table from bytes to hex strings.
|
32 | var lut = new Array(256);
|
33 | // Pad the single character hex digits with a leading zero.
|
34 | for (var i = 0; i < 16; ++i) {
|
35 | lut[i] = '0' + i.toString(16);
|
36 | }
|
37 | // Populate the rest of the hex digits.
|
38 | for (var i = 16; i < 256; ++i) {
|
39 | lut[i] = i.toString(16);
|
40 | }
|
41 | // Return a function which generates the UUID.
|
42 | return function uuid4() {
|
43 | // Get a new batch of random values.
|
44 | random_1.Random.getRandomValues(bytes);
|
45 | // Set the UUID version number to 4.
|
46 | bytes[6] = 0x40 | (bytes[6] & 0x0F);
|
47 | // Set the clock sequence bit to the RFC spec.
|
48 | bytes[8] = 0x80 | (bytes[8] & 0x3F);
|
49 | // Assemble the UUID string.
|
50 | return (lut[bytes[0]] +
|
51 | lut[bytes[1]] +
|
52 | lut[bytes[2]] +
|
53 | lut[bytes[3]] +
|
54 | '-' +
|
55 | lut[bytes[4]] +
|
56 | lut[bytes[5]] +
|
57 | '-' +
|
58 | lut[bytes[6]] +
|
59 | lut[bytes[7]] +
|
60 | '-' +
|
61 | lut[bytes[8]] +
|
62 | lut[bytes[9]] +
|
63 | '-' +
|
64 | lut[bytes[10]] +
|
65 | lut[bytes[11]] +
|
66 | lut[bytes[12]] +
|
67 | lut[bytes[13]] +
|
68 | lut[bytes[14]] +
|
69 | lut[bytes[15]]);
|
70 | };
|
71 | })();
|
72 | })(UUID = exports.UUID || (exports.UUID = {}));
|