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.

29 lines
680 B

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