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.

214 lines
5.8 KiB

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