• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var rng = require('./lib/rng');
2var bytesToUuid = require('./lib/bytesToUuid');
3
4function v4(options, buf, offset) {
5  var i = buf && offset || 0;
6
7  if (typeof(options) == 'string') {
8    buf = options === 'binary' ? new Array(16) : null;
9    options = null;
10  }
11  options = options || {};
12
13  var rnds = options.random || (options.rng || rng)();
14
15  // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
16  rnds[6] = (rnds[6] & 0x0f) | 0x40;
17  rnds[8] = (rnds[8] & 0x3f) | 0x80;
18
19  // Copy bytes to buffer, if provided
20  if (buf) {
21    for (var ii = 0; ii < 16; ++ii) {
22      buf[i + ii] = rnds[ii];
23    }
24  }
25
26  return buf || bytesToUuid(rnds);
27}
28
29module.exports = v4;
30