v4.js 546 B

123456789101112131415161718192021222324
  1. import rng from './rng.js';
  2. import stringify from './stringify.js';
  3. function v4(options, buf, offset) {
  4. options = options || {};
  5. const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
  6. rnds[6] = rnds[6] & 0x0f | 0x40;
  7. rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
  8. if (buf) {
  9. offset = offset || 0;
  10. for (let i = 0; i < 16; ++i) {
  11. buf[offset + i] = rnds[i];
  12. }
  13. return buf;
  14. }
  15. return stringify(rnds);
  16. }
  17. export default v4;