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.

216 lines
5.9 KiB

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