v6.js 817 B

1234567891011121314151617181920212223242526272829303132
  1. import { unsafeStringify } from './stringify.js';
  2. import v1 from './v1.js';
  3. import v1ToV6 from './v1ToV6.js';
  4. /**
  5. *
  6. * @param {object} options
  7. * @param {Uint8Array=} buf
  8. * @param {number=} offset
  9. * @returns
  10. */
  11. export default function v6(options = {}, buf, offset = 0) {
  12. // v6 is v1 with different field layout, so we start with a v1 UUID, albeit
  13. // with slightly different behavior around how the clock_seq and node fields
  14. // are randomized, which is why we call v1 with _v6: true.
  15. let bytes = v1({
  16. ...options,
  17. _v6: true
  18. }, new Uint8Array(16));
  19. // Reorder the fields to v6 layout.
  20. bytes = v1ToV6(bytes);
  21. // Return as a byte array if requested
  22. if (buf) {
  23. for (let i = 0; i < 16; i++) {
  24. buf[offset + i] = bytes[i];
  25. }
  26. return buf;
  27. }
  28. return unsafeStringify(bytes);
  29. }