uuid.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.uuidV4 = void 0;
  4. /**
  5. * Explain UUID and link to RFC here.
  6. *
  7. * @_subsection: api/utils:UUID [about-uuid]
  8. */
  9. const data_js_1 = require("./data.js");
  10. /**
  11. * Returns the version 4 [[link-uuid]] for the %%randomBytes%%.
  12. *
  13. * @see: https://www.ietf.org/rfc/rfc4122.txt (Section 4.4)
  14. */
  15. function uuidV4(randomBytes) {
  16. const bytes = (0, data_js_1.getBytes)(randomBytes, "randomBytes");
  17. // Section: 4.1.3:
  18. // - time_hi_and_version[12:16] = 0b0100
  19. bytes[6] = (bytes[6] & 0x0f) | 0x40;
  20. // Section 4.4
  21. // - clock_seq_hi_and_reserved[6] = 0b0
  22. // - clock_seq_hi_and_reserved[7] = 0b1
  23. bytes[8] = (bytes[8] & 0x3f) | 0x80;
  24. const value = (0, data_js_1.hexlify)(bytes);
  25. return [
  26. value.substring(2, 10),
  27. value.substring(10, 14),
  28. value.substring(14, 18),
  29. value.substring(18, 22),
  30. value.substring(22, 34),
  31. ].join("-");
  32. }
  33. exports.uuidV4 = uuidV4;
  34. //# sourceMappingURL=uuid.js.map