uuid 8.0.0 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [8.1.0](https://github.com/uuidjs/uuid/compare/v8.0.0...v8.1.0) (2020-05-20)
6
+
7
+ ### Features
8
+
9
+ - improve v4 performance by reusing random number array ([#435](https://github.com/uuidjs/uuid/issues/435)) ([bf4af0d](https://github.com/uuidjs/uuid/commit/bf4af0d711b4d2ed03d1f74fd12ad0baa87dc79d))
10
+ - optimize V8 performance of bytesToUuid ([#434](https://github.com/uuidjs/uuid/issues/434)) ([e156415](https://github.com/uuidjs/uuid/commit/e156415448ec1af2351fa0b6660cfb22581971f2))
11
+
12
+ ### Bug Fixes
13
+
14
+ - export package.json required by react-native and bundlers ([#449](https://github.com/uuidjs/uuid/issues/449)) ([be1c8fe](https://github.com/uuidjs/uuid/commit/be1c8fe9a3206c358e0059b52fafd7213aa48a52)), closes [/github.com/ai/nanoevents/issues/44#issuecomment-602010343](https://github.com/uuidjs//github.com/ai/nanoevents/issues/44/issues/issuecomment-602010343) [#444](https://github.com/uuidjs/uuid/issues/444)
15
+
5
16
  ## [8.0.0](https://github.com/uuidjs/uuid/compare/v7.0.3...v8.0.0) (2020-04-29)
6
17
 
7
18
  ### ⚠ BREAKING CHANGES
@@ -9,17 +9,18 @@ exports.default = void 0;
9
9
  * Convert array of 16 byte values to UUID string format of the form:
10
10
  * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
11
11
  */
12
- var byteToHex = [];
12
+ const byteToHex = [];
13
13
 
14
- for (var i = 0; i < 256; ++i) {
15
- byteToHex[i] = (i + 0x100).toString(16).substr(1);
14
+ for (let i = 0; i < 256; ++i) {
15
+ byteToHex.push((i + 0x100).toString(16).substr(1));
16
16
  }
17
17
 
18
18
  function bytesToUuid(buf, offset) {
19
- var i = offset || 0;
20
- var bth = byteToHex; // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
19
+ const i = offset || 0;
20
+ const bth = byteToHex; // Note: Be careful editing this code! It's been tuned for performance
21
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
21
22
 
22
- return [bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]]].join('');
23
+ return (bth[buf[i + 0]] + bth[buf[i + 1]] + bth[buf[i + 2]] + bth[buf[i + 3]] + '-' + bth[buf[i + 4]] + bth[buf[i + 5]] + '-' + bth[buf[i + 6]] + bth[buf[i + 7]] + '-' + bth[buf[i + 8]] + bth[buf[i + 9]] + '-' + bth[buf[i + 10]] + bth[buf[i + 11]] + bth[buf[i + 12]] + bth[buf[i + 13]] + bth[buf[i + 14]] + bth[buf[i + 15]]).toLowerCase();
23
24
  }
24
25
 
25
26
  var _default = bytesToUuid;
@@ -5,14 +5,15 @@
5
5
  var byteToHex = [];
6
6
 
7
7
  for (var i = 0; i < 256; ++i) {
8
- byteToHex[i] = (i + 0x100).toString(16).substr(1);
8
+ byteToHex.push((i + 0x100).toString(16).substr(1));
9
9
  }
10
10
 
11
11
  function bytesToUuid(buf, offset) {
12
12
  var i = offset || 0;
13
- var bth = byteToHex; // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
13
+ var bth = byteToHex; // Note: Be careful editing this code! It's been tuned for performance
14
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
14
15
 
15
- return [bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]]].join('');
16
+ return (bth[buf[i + 0]] + bth[buf[i + 1]] + bth[buf[i + 2]] + bth[buf[i + 3]] + '-' + bth[buf[i + 4]] + bth[buf[i + 5]] + '-' + bth[buf[i + 6]] + bth[buf[i + 7]] + '-' + bth[buf[i + 8]] + bth[buf[i + 9]] + '-' + bth[buf[i + 10]] + bth[buf[i + 11]] + bth[buf[i + 12]] + bth[buf[i + 13]] + bth[buf[i + 14]] + bth[buf[i + 15]]).toLowerCase();
16
17
  }
17
18
 
18
19
  export default bytesToUuid;
@@ -19,12 +19,12 @@
19
19
  * See http://pajhome.org.uk/crypt/md5 for more info.
20
20
  */
21
21
  function md5(bytes) {
22
- if (typeof bytes == 'string') {
22
+ if (typeof bytes === 'string') {
23
23
  var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
24
24
 
25
- bytes = new Array(msg.length);
25
+ bytes = new Uint8Array(msg.length);
26
26
 
27
- for (var i = 0; i < msg.length; i++) {
27
+ for (var i = 0; i < msg.length; ++i) {
28
28
  bytes[i] = msg.charCodeAt(i);
29
29
  }
30
30
  }
@@ -37,21 +37,26 @@ function md5(bytes) {
37
37
 
38
38
 
39
39
  function md5ToHexEncodedArray(input) {
40
- var i;
41
- var x;
42
40
  var output = [];
43
41
  var length32 = input.length * 32;
44
42
  var hexTab = '0123456789abcdef';
45
- var hex;
46
43
 
47
- for (i = 0; i < length32; i += 8) {
48
- x = input[i >> 5] >>> i % 32 & 0xff;
49
- hex = parseInt(hexTab.charAt(x >>> 4 & 0x0f) + hexTab.charAt(x & 0x0f), 16);
44
+ for (var i = 0; i < length32; i += 8) {
45
+ var x = input[i >> 5] >>> i % 32 & 0xff;
46
+ var hex = parseInt(hexTab.charAt(x >>> 4 & 0x0f) + hexTab.charAt(x & 0x0f), 16);
50
47
  output.push(hex);
51
48
  }
52
49
 
53
50
  return output;
54
51
  }
52
+ /**
53
+ * Calculate output length with padding and bit length
54
+ */
55
+
56
+
57
+ function getOutputLength(inputLength8) {
58
+ return (inputLength8 + 64 >>> 9 << 4) + 14 + 1;
59
+ }
55
60
  /*
56
61
  * Calculate the MD5 of an array of little-endian words, and a bit length.
57
62
  */
@@ -60,22 +65,17 @@ function md5ToHexEncodedArray(input) {
60
65
  function wordsToMd5(x, len) {
61
66
  /* append padding */
62
67
  x[len >> 5] |= 0x80 << len % 32;
63
- x[(len + 64 >>> 9 << 4) + 14] = len;
64
- var i;
65
- var olda;
66
- var oldb;
67
- var oldc;
68
- var oldd;
68
+ x[getOutputLength(len) - 1] = len;
69
69
  var a = 1732584193;
70
70
  var b = -271733879;
71
71
  var c = -1732584194;
72
72
  var d = 271733878;
73
73
 
74
- for (i = 0; i < x.length; i += 16) {
75
- olda = a;
76
- oldb = b;
77
- oldc = c;
78
- oldd = d;
74
+ for (var i = 0; i < x.length; i += 16) {
75
+ var olda = a;
76
+ var oldb = b;
77
+ var oldc = c;
78
+ var oldd = d;
79
79
  a = md5ff(a, b, c, d, x[i], 7, -680876936);
80
80
  d = md5ff(d, a, b, c, x[i + 1], 12, -389564586);
81
81
  c = md5ff(c, d, a, b, x[i + 2], 17, 606105819);
@@ -155,17 +155,14 @@ function wordsToMd5(x, len) {
155
155
 
156
156
 
157
157
  function bytesToWords(input) {
158
- var i;
159
- var output = [];
160
- output[(input.length >> 2) - 1] = undefined;
161
-
162
- for (i = 0; i < output.length; i += 1) {
163
- output[i] = 0;
158
+ if (input.length === 0) {
159
+ return [];
164
160
  }
165
161
 
166
162
  var length8 = input.length * 8;
163
+ var output = new Uint32Array(getOutputLength(length8));
167
164
 
168
- for (i = 0; i < length8; i += 8) {
165
+ for (var i = 0; i < length8; i += 8) {
169
166
  output[i >> 5] |= (input[i / 8] & 0xff) << i % 32;
170
167
  }
171
168
 
@@ -3,9 +3,8 @@
3
3
  // generators (like Math.random()).
4
4
  // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation. Also,
5
5
  // find the complete implementation of crypto (msCrypto) on IE11.
6
- var getRandomValues = typeof crypto != 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto != 'undefined' && typeof msCrypto.getRandomValues == 'function' && msCrypto.getRandomValues.bind(msCrypto);
7
- var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
8
-
6
+ var getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto !== 'undefined' && typeof msCrypto.getRandomValues === 'function' && msCrypto.getRandomValues.bind(msCrypto);
7
+ var rnds8 = new Uint8Array(16);
9
8
  export default function rng() {
10
9
  if (!getRandomValues) {
11
10
  throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
@@ -24,13 +24,13 @@ function sha1(bytes) {
24
24
  var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
25
25
  var H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
26
26
 
27
- if (typeof bytes == 'string') {
27
+ if (typeof bytes === 'string') {
28
28
  var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
29
29
 
30
- bytes = new Array(msg.length);
30
+ bytes = [];
31
31
 
32
- for (var i = 0; i < msg.length; i++) {
33
- bytes[i] = msg.charCodeAt(i);
32
+ for (var i = 0; i < msg.length; ++i) {
33
+ bytes.push(msg.charCodeAt(i));
34
34
  }
35
35
  }
36
36
 
@@ -39,27 +39,29 @@ function sha1(bytes) {
39
39
  var N = Math.ceil(l / 16);
40
40
  var M = new Array(N);
41
41
 
42
- for (var i = 0; i < N; i++) {
43
- M[i] = new Array(16);
42
+ for (var _i = 0; _i < N; ++_i) {
43
+ var arr = new Uint32Array(16);
44
44
 
45
- for (var j = 0; j < 16; j++) {
46
- M[i][j] = bytes[i * 64 + j * 4] << 24 | bytes[i * 64 + j * 4 + 1] << 16 | bytes[i * 64 + j * 4 + 2] << 8 | bytes[i * 64 + j * 4 + 3];
45
+ for (var j = 0; j < 16; ++j) {
46
+ arr[j] = bytes[_i * 64 + j * 4] << 24 | bytes[_i * 64 + j * 4 + 1] << 16 | bytes[_i * 64 + j * 4 + 2] << 8 | bytes[_i * 64 + j * 4 + 3];
47
47
  }
48
+
49
+ M[_i] = arr;
48
50
  }
49
51
 
50
52
  M[N - 1][14] = (bytes.length - 1) * 8 / Math.pow(2, 32);
51
53
  M[N - 1][14] = Math.floor(M[N - 1][14]);
52
54
  M[N - 1][15] = (bytes.length - 1) * 8 & 0xffffffff;
53
55
 
54
- for (var i = 0; i < N; i++) {
55
- var W = new Array(80);
56
+ for (var _i2 = 0; _i2 < N; ++_i2) {
57
+ var W = new Uint32Array(80);
56
58
 
57
- for (var t = 0; t < 16; t++) {
58
- W[t] = M[i][t];
59
+ for (var t = 0; t < 16; ++t) {
60
+ W[t] = M[_i2][t];
59
61
  }
60
62
 
61
- for (var t = 16; t < 80; t++) {
62
- W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
63
+ for (var _t = 16; _t < 80; ++_t) {
64
+ W[_t] = ROTL(W[_t - 3] ^ W[_t - 8] ^ W[_t - 14] ^ W[_t - 16], 1);
63
65
  }
64
66
 
65
67
  var a = H[0];
@@ -68,9 +70,9 @@ function sha1(bytes) {
68
70
  var d = H[3];
69
71
  var e = H[4];
70
72
 
71
- for (var t = 0; t < 80; t++) {
72
- var s = Math.floor(t / 20);
73
- var T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t] >>> 0;
73
+ for (var _t2 = 0; _t2 < 80; ++_t2) {
74
+ var s = Math.floor(_t2 / 20);
75
+ var T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[_t2] >>> 0;
74
76
  e = d;
75
77
  d = c;
76
78
  c = ROTL(b, 30) >>> 0;
@@ -39,7 +39,7 @@ function v1(options, buf, offset) {
39
39
  // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
40
40
 
41
41
 
42
- var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime(); // Per 4.2.1.2, use count of uuid's generated during the current clock
42
+ var msecs = options.msecs !== undefined ? options.msecs : Date.now(); // Per 4.2.1.2, use count of uuid's generated during the current clock
43
43
  // cycle to simulate higher resolution clock
44
44
 
45
45
  var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; // Time since last uuid creation (in msecs)
@@ -89,7 +89,7 @@ function v1(options, buf, offset) {
89
89
  b[i + n] = node[n];
90
90
  }
91
91
 
92
- return buf ? buf : bytesToUuid(b);
92
+ return buf || bytesToUuid(b);
93
93
  }
94
94
 
95
95
  export default v1;
@@ -12,10 +12,10 @@ function uuidToBytes(uuid) {
12
12
  function stringToBytes(str) {
13
13
  str = unescape(encodeURIComponent(str)); // UTF8 escape
14
14
 
15
- var bytes = new Array(str.length);
15
+ var bytes = [];
16
16
 
17
- for (var i = 0; i < str.length; i++) {
18
- bytes[i] = str.charCodeAt(i);
17
+ for (var i = 0; i < str.length; ++i) {
18
+ bytes.push(str.charCodeAt(i));
19
19
  }
20
20
 
21
21
  return bytes;
@@ -24,12 +24,19 @@ function stringToBytes(str) {
24
24
  export var DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
25
25
  export var URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
26
26
  export default function (name, version, hashfunc) {
27
- var generateUUID = function generateUUID(value, namespace, buf, offset) {
27
+ function generateUUID(value, namespace, buf, offset) {
28
28
  var off = buf && offset || 0;
29
- if (typeof value == 'string') value = stringToBytes(value);
30
- if (typeof namespace == 'string') namespace = uuidToBytes(namespace);
31
- if (!Array.isArray(value)) throw TypeError('value must be an array of bytes');
32
- if (!Array.isArray(namespace) || namespace.length !== 16) throw TypeError('namespace must be uuid string or an Array of 16 byte values'); // Per 4.3
29
+ if (typeof value === 'string') value = stringToBytes(value);
30
+ if (typeof namespace === 'string') namespace = uuidToBytes(namespace);
31
+
32
+ if (!Array.isArray(value)) {
33
+ throw TypeError('value must be an array of bytes');
34
+ }
35
+
36
+ if (!Array.isArray(namespace) || namespace.length !== 16) {
37
+ throw TypeError('namespace must be uuid string or an Array of 16 byte values');
38
+ } // Per 4.3
39
+
33
40
 
34
41
  var bytes = hashfunc(namespace.concat(value));
35
42
  bytes[6] = bytes[6] & 0x0f | version;
@@ -42,11 +49,11 @@ export default function (name, version, hashfunc) {
42
49
  }
43
50
 
44
51
  return buf || bytesToUuid(bytes);
45
- }; // Function#name is not settable on some platforms (#270)
52
+ } // Function#name is not settable on some platforms (#270)
46
53
 
47
54
 
48
55
  try {
49
- generateUUID.name = name;
56
+ generateUUID.name = name; // eslint-disable-next-line no-empty
50
57
  } catch (err) {} // For CommonJS default export support
51
58
 
52
59
 
@@ -2,10 +2,8 @@ import rng from './rng.js';
2
2
  import bytesToUuid from './bytesToUuid.js';
3
3
 
4
4
  function v4(options, buf, offset) {
5
- var i = buf && offset || 0;
6
-
7
- if (typeof options == 'string') {
8
- buf = options === 'binary' ? new Array(16) : null;
5
+ if (typeof options === 'string') {
6
+ buf = options === 'binary' ? new Uint8Array(16) : null;
9
7
  options = null;
10
8
  }
11
9
 
@@ -16,12 +14,16 @@ function v4(options, buf, offset) {
16
14
  rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
17
15
 
18
16
  if (buf) {
19
- for (var ii = 0; ii < 16; ++ii) {
20
- buf[i + ii] = rnds[ii];
17
+ var start = offset || 0;
18
+
19
+ for (var i = 0; i < 16; ++i) {
20
+ buf[start + i] = rnds[i];
21
21
  }
22
+
23
+ return buf;
22
24
  }
23
25
 
24
- return buf || bytesToUuid(rnds);
26
+ return bytesToUuid(rnds);
25
27
  }
26
28
 
27
29
  export default v4;
@@ -2,17 +2,18 @@
2
2
  * Convert array of 16 byte values to UUID string format of the form:
3
3
  * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
4
4
  */
5
- var byteToHex = [];
5
+ const byteToHex = [];
6
6
 
7
- for (var i = 0; i < 256; ++i) {
8
- byteToHex[i] = (i + 0x100).toString(16).substr(1);
7
+ for (let i = 0; i < 256; ++i) {
8
+ byteToHex.push((i + 0x100).toString(16).substr(1));
9
9
  }
10
10
 
11
11
  function bytesToUuid(buf, offset) {
12
- var i = offset || 0;
13
- var bth = byteToHex; // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
12
+ const i = offset || 0;
13
+ const bth = byteToHex; // Note: Be careful editing this code! It's been tuned for performance
14
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
14
15
 
15
- return [bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]]].join('');
16
+ return (bth[buf[i + 0]] + bth[buf[i + 1]] + bth[buf[i + 2]] + bth[buf[i + 3]] + '-' + bth[buf[i + 4]] + bth[buf[i + 5]] + '-' + bth[buf[i + 6]] + bth[buf[i + 7]] + '-' + bth[buf[i + 8]] + bth[buf[i + 9]] + '-' + bth[buf[i + 10]] + bth[buf[i + 11]] + bth[buf[i + 12]] + bth[buf[i + 13]] + bth[buf[i + 14]] + bth[buf[i + 15]]).toLowerCase();
16
17
  }
17
18
 
18
19
  export default bytesToUuid;
@@ -1,4 +1,5 @@
1
1
  import crypto from 'crypto';
2
+ const rnds8 = new Uint8Array(16);
2
3
  export default function rng() {
3
- return crypto.randomBytes(16);
4
+ return crypto.randomFillSync(rnds8);
4
5
  }
@@ -4,25 +4,25 @@ import bytesToUuid from './bytesToUuid.js'; // **`v1()` - Generate time-based UU
4
4
  // Inspired by https://github.com/LiosK/UUID.js
5
5
  // and http://docs.python.org/library/uuid.html
6
6
 
7
- var _nodeId;
7
+ let _nodeId;
8
8
 
9
- var _clockseq; // Previous uuid creation time
9
+ let _clockseq; // Previous uuid creation time
10
10
 
11
11
 
12
- var _lastMSecs = 0;
13
- var _lastNSecs = 0; // See https://github.com/uuidjs/uuid for API details
12
+ let _lastMSecs = 0;
13
+ let _lastNSecs = 0; // See https://github.com/uuidjs/uuid for API details
14
14
 
15
15
  function v1(options, buf, offset) {
16
- var i = buf && offset || 0;
17
- var b = buf || [];
16
+ let i = buf && offset || 0;
17
+ const b = buf || [];
18
18
  options = options || {};
19
- var node = options.node || _nodeId;
20
- var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; // node and clockseq need to be initialized to random values if they're not
19
+ let node = options.node || _nodeId;
20
+ let clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; // node and clockseq need to be initialized to random values if they're not
21
21
  // specified. We do this lazily to minimize issues related to insufficient
22
22
  // system entropy. See #189
23
23
 
24
24
  if (node == null || clockseq == null) {
25
- var seedBytes = options.random || (options.rng || rng)();
25
+ const seedBytes = options.random || (options.rng || rng)();
26
26
 
27
27
  if (node == null) {
28
28
  // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
@@ -39,12 +39,12 @@ function v1(options, buf, offset) {
39
39
  // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
40
40
 
41
41
 
42
- var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime(); // Per 4.2.1.2, use count of uuid's generated during the current clock
42
+ let msecs = options.msecs !== undefined ? options.msecs : Date.now(); // Per 4.2.1.2, use count of uuid's generated during the current clock
43
43
  // cycle to simulate higher resolution clock
44
44
 
45
- var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; // Time since last uuid creation (in msecs)
45
+ let nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; // Time since last uuid creation (in msecs)
46
46
 
47
- var dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; // Per 4.2.1.2, Bump clockseq on clock regression
47
+ const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; // Per 4.2.1.2, Bump clockseq on clock regression
48
48
 
49
49
  if (dt < 0 && options.clockseq === undefined) {
50
50
  clockseq = clockseq + 1 & 0x3fff;
@@ -67,13 +67,13 @@ function v1(options, buf, offset) {
67
67
 
68
68
  msecs += 12219292800000; // `time_low`
69
69
 
70
- var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
70
+ const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
71
71
  b[i++] = tl >>> 24 & 0xff;
72
72
  b[i++] = tl >>> 16 & 0xff;
73
73
  b[i++] = tl >>> 8 & 0xff;
74
74
  b[i++] = tl & 0xff; // `time_mid`
75
75
 
76
- var tmh = msecs / 0x100000000 * 10000 & 0xfffffff;
76
+ const tmh = msecs / 0x100000000 * 10000 & 0xfffffff;
77
77
  b[i++] = tmh >>> 8 & 0xff;
78
78
  b[i++] = tmh & 0xff; // `time_high_and_version`
79
79
 
@@ -85,11 +85,11 @@ function v1(options, buf, offset) {
85
85
 
86
86
  b[i++] = clockseq & 0xff; // `node`
87
87
 
88
- for (var n = 0; n < 6; ++n) {
88
+ for (let n = 0; n < 6; ++n) {
89
89
  b[i + n] = node[n];
90
90
  }
91
91
 
92
- return buf ? buf : bytesToUuid(b);
92
+ return buf || bytesToUuid(b);
93
93
  }
94
94
 
95
95
  export default v1;
@@ -2,7 +2,7 @@ import bytesToUuid from './bytesToUuid.js';
2
2
 
3
3
  function uuidToBytes(uuid) {
4
4
  // Note: We assume we're being passed a valid uuid string
5
- var bytes = [];
5
+ const bytes = [];
6
6
  uuid.replace(/[a-fA-F0-9]{2}/g, function (hex) {
7
7
  bytes.push(parseInt(hex, 16));
8
8
  });
@@ -12,10 +12,10 @@ function uuidToBytes(uuid) {
12
12
  function stringToBytes(str) {
13
13
  str = unescape(encodeURIComponent(str)); // UTF8 escape
14
14
 
15
- var bytes = new Array(str.length);
15
+ const bytes = [];
16
16
 
17
- for (var i = 0; i < str.length; i++) {
18
- bytes[i] = str.charCodeAt(i);
17
+ for (let i = 0; i < str.length; ++i) {
18
+ bytes.push(str.charCodeAt(i));
19
19
  }
20
20
 
21
21
  return bytes;
@@ -24,29 +24,36 @@ function stringToBytes(str) {
24
24
  export const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
25
25
  export const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
26
26
  export default function (name, version, hashfunc) {
27
- var generateUUID = function (value, namespace, buf, offset) {
28
- var off = buf && offset || 0;
29
- if (typeof value == 'string') value = stringToBytes(value);
30
- if (typeof namespace == 'string') namespace = uuidToBytes(namespace);
31
- if (!Array.isArray(value)) throw TypeError('value must be an array of bytes');
32
- if (!Array.isArray(namespace) || namespace.length !== 16) throw TypeError('namespace must be uuid string or an Array of 16 byte values'); // Per 4.3
33
-
34
- var bytes = hashfunc(namespace.concat(value));
27
+ function generateUUID(value, namespace, buf, offset) {
28
+ const off = buf && offset || 0;
29
+ if (typeof value === 'string') value = stringToBytes(value);
30
+ if (typeof namespace === 'string') namespace = uuidToBytes(namespace);
31
+
32
+ if (!Array.isArray(value)) {
33
+ throw TypeError('value must be an array of bytes');
34
+ }
35
+
36
+ if (!Array.isArray(namespace) || namespace.length !== 16) {
37
+ throw TypeError('namespace must be uuid string or an Array of 16 byte values');
38
+ } // Per 4.3
39
+
40
+
41
+ const bytes = hashfunc(namespace.concat(value));
35
42
  bytes[6] = bytes[6] & 0x0f | version;
36
43
  bytes[8] = bytes[8] & 0x3f | 0x80;
37
44
 
38
45
  if (buf) {
39
- for (var idx = 0; idx < 16; ++idx) {
46
+ for (let idx = 0; idx < 16; ++idx) {
40
47
  buf[off + idx] = bytes[idx];
41
48
  }
42
49
  }
43
50
 
44
51
  return buf || bytesToUuid(bytes);
45
- }; // Function#name is not settable on some platforms (#270)
52
+ } // Function#name is not settable on some platforms (#270)
46
53
 
47
54
 
48
55
  try {
49
- generateUUID.name = name;
56
+ generateUUID.name = name; // eslint-disable-next-line no-empty
50
57
  } catch (err) {} // For CommonJS default export support
51
58
 
52
59
 
@@ -2,26 +2,28 @@ import rng from './rng.js';
2
2
  import bytesToUuid from './bytesToUuid.js';
3
3
 
4
4
  function v4(options, buf, offset) {
5
- var i = buf && offset || 0;
6
-
7
- if (typeof options == 'string') {
8
- buf = options === 'binary' ? new Array(16) : null;
5
+ if (typeof options === 'string') {
6
+ buf = options === 'binary' ? new Uint8Array(16) : null;
9
7
  options = null;
10
8
  }
11
9
 
12
10
  options = options || {};
13
- var rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
11
+ const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
14
12
 
15
13
  rnds[6] = rnds[6] & 0x0f | 0x40;
16
14
  rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
17
15
 
18
16
  if (buf) {
19
- for (var ii = 0; ii < 16; ++ii) {
20
- buf[i + ii] = rnds[ii];
17
+ const start = offset || 0;
18
+
19
+ for (let i = 0; i < 16; ++i) {
20
+ buf[start + i] = rnds[i];
21
21
  }
22
+
23
+ return buf;
22
24
  }
23
25
 
24
- return buf || bytesToUuid(rnds);
26
+ return bytesToUuid(rnds);
25
27
  }
26
28
 
27
29
  export default v4;