vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.9 KiB

  1. /* eslint-disable require-jsdoc */
  2. var _rng;
  3. var globalVar = typeof window !== 'undefined'
  4. ? window
  5. : typeof global !== 'undefined' ? global : null;
  6. if (globalVar && globalVar.crypto && crypto.getRandomValues) {
  7. // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
  8. // Moderately fast, high quality
  9. var _rnds8 = new Uint8Array(16);
  10. _rng = function whatwgRNG() {
  11. crypto.getRandomValues(_rnds8);
  12. return _rnds8;
  13. };
  14. }
  15. if (!_rng) {
  16. // Math.random()-based (RNG)
  17. //
  18. // If all else fails, use Math.random(). It's fast, but is of unspecified
  19. // quality.
  20. var _rnds = new Array(16);
  21. _rng = function () {
  22. for (var i = 0, r; i < 16; i++) {
  23. if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
  24. _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
  25. }
  26. return _rnds;
  27. };
  28. }
  29. // uuid.js
  30. //
  31. // Copyright (c) 2010-2012 Robert Kieffer
  32. // MIT License - http://opensource.org/licenses/mit-license.php
  33. // Unique ID creation requires a high quality random # generator. We feature
  34. // detect to determine the best RNG source, normalizing to a function that
  35. // returns 128-bits of randomness, since that's what's usually required
  36. //var _rng = require('./rng');
  37. // Maps for number <-> hex string conversion
  38. var _byteToHex = [];
  39. var _hexToByte = {};
  40. for (var i = 0; i < 256; i++) {
  41. _byteToHex[i] = (i + 0x100).toString(16).substr(1);
  42. _hexToByte[_byteToHex[i]] = i;
  43. }
  44. // **`parse()` - Parse a UUID into it's component bytes**
  45. function parse(s, buf, offset) {
  46. var i = (buf && offset) || 0, ii = 0;
  47. buf = buf || [];
  48. s.toLowerCase().replace(/[0-9a-f]{2}/g, function (oct) {
  49. if (ii < 16) { // Don't overflow!
  50. buf[i + ii++] = _hexToByte[oct];
  51. }
  52. });
  53. // Zero out remaining bytes if string was short
  54. while (ii < 16) {
  55. buf[i + ii++] = 0;
  56. }
  57. return buf;
  58. }
  59. // **`unparse()` - Convert UUID byte array (ala parse()) into a string**
  60. function unparse(buf, offset) {
  61. var i = offset || 0, bth = _byteToHex;
  62. return bth[buf[i++]] + bth[buf[i++]] +
  63. bth[buf[i++]] + bth[buf[i++]] + '-' +
  64. bth[buf[i++]] + bth[buf[i++]] + '-' +
  65. bth[buf[i++]] + bth[buf[i++]] + '-' +
  66. bth[buf[i++]] + bth[buf[i++]] + '-' +
  67. bth[buf[i++]] + bth[buf[i++]] +
  68. bth[buf[i++]] + bth[buf[i++]] +
  69. bth[buf[i++]] + bth[buf[i++]];
  70. }
  71. // **`v1()` - Generate time-based UUID**
  72. //
  73. // Inspired by https://github.com/LiosK/UUID.js
  74. // and http://docs.python.org/library/uuid.html
  75. // random #'s we need to init node and clockseq
  76. var _seedBytes = _rng();
  77. // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
  78. var _nodeId = [
  79. _seedBytes[0] | 0x01,
  80. _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
  81. ];
  82. // Per 4.2.2, randomize (14 bit) clockseq
  83. var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
  84. // Previous uuid creation time
  85. var _lastMSecs = 0, _lastNSecs = 0;
  86. // See https://github.com/broofa/node-uuid for API details
  87. function v1(options, buf, offset) {
  88. var i = buf && offset || 0;
  89. var b = buf || [];
  90. options = options || {};
  91. var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
  92. // UUID timestamps are 100 nano-second units since the Gregorian epoch,
  93. // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
  94. // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
  95. // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
  96. var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();
  97. // Per 4.2.1.2, use count of uuid's generated during the current clock
  98. // cycle to simulate higher resolution clock
  99. var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
  100. // Time since last uuid creation (in msecs)
  101. var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs) / 10000;
  102. // Per 4.2.1.2, Bump clockseq on clock regression
  103. if (dt < 0 && options.clockseq === undefined) {
  104. clockseq = clockseq + 1 & 0x3fff;
  105. }
  106. // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
  107. // time interval
  108. if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
  109. nsecs = 0;
  110. }
  111. // Per 4.2.1.2 Throw error if too many uuids are requested
  112. if (nsecs >= 10000) {
  113. throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
  114. }
  115. _lastMSecs = msecs;
  116. _lastNSecs = nsecs;
  117. _clockseq = clockseq;
  118. // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
  119. msecs += 12219292800000;
  120. // `time_low`
  121. var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
  122. b[i++] = tl >>> 24 & 0xff;
  123. b[i++] = tl >>> 16 & 0xff;
  124. b[i++] = tl >>> 8 & 0xff;
  125. b[i++] = tl & 0xff;
  126. // `time_mid`
  127. var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
  128. b[i++] = tmh >>> 8 & 0xff;
  129. b[i++] = tmh & 0xff;
  130. // `time_high_and_version`
  131. b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
  132. b[i++] = tmh >>> 16 & 0xff;
  133. // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
  134. b[i++] = clockseq >>> 8 | 0x80;
  135. // `clock_seq_low`
  136. b[i++] = clockseq & 0xff;
  137. // `node`
  138. var node = options.node || _nodeId;
  139. for (var n = 0; n < 6; n++) {
  140. b[i + n] = node[n];
  141. }
  142. return buf ? buf : unparse(b);
  143. }
  144. // **`v4()` - Generate random UUID**
  145. // See https://github.com/broofa/node-uuid for API details
  146. function v4(options, buf, offset) {
  147. // Deprecated - 'format' argument, as supported in v1.2
  148. var i = buf && offset || 0;
  149. if (typeof(options) == 'string') {
  150. buf = options == 'binary' ? new Array(16) : null;
  151. options = null;
  152. }
  153. options = options || {};
  154. var rnds = options.random || (options.rng || _rng)();
  155. // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
  156. rnds[6] = (rnds[6] & 0x0f) | 0x40;
  157. rnds[8] = (rnds[8] & 0x3f) | 0x80;
  158. // Copy bytes to buffer, if provided
  159. if (buf) {
  160. for (var ii = 0; ii < 16; ii++) {
  161. buf[i + ii] = rnds[ii];
  162. }
  163. }
  164. return buf || unparse(rnds);
  165. }
  166. // Export public API
  167. var uuid = v4;
  168. uuid.v1 = v1;
  169. uuid.v4 = v4;
  170. uuid.parse = parse;
  171. uuid.unparse = unparse;
  172. module.exports = uuid;