No Description

v4.js 660B

123456789101112131415161718192021222324252627
  1. import rng from './rng.js';
  2. import bytesToUuid from './bytesToUuid.js';
  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)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
  11. rnds[6] = rnds[6] & 0x0f | 0x40;
  12. rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
  13. if (buf) {
  14. for (var ii = 0; ii < 16; ++ii) {
  15. buf[i + ii] = rnds[ii];
  16. }
  17. }
  18. return buf || bytesToUuid(rnds);
  19. }
  20. export default v4;