v1ToV6.js 1.3 KB

1234567891011121314151617181920212223242526
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = v1ToV6;
  6. var _parse = _interopRequireDefault(require("./parse.js"));
  7. var _stringify = require("./stringify.js");
  8. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  9. /**
  10. * Convert a v1 UUID to a v6 UUID
  11. *
  12. * @param {string|Uint8Array} uuid - The v1 UUID to convert to v6
  13. * @returns {string|Uint8Array} The v6 UUID as the same type as the `uuid` arg
  14. * (string or Uint8Array)
  15. */
  16. function v1ToV6(uuid) {
  17. const v1Bytes = typeof uuid === 'string' ? (0, _parse.default)(uuid) : uuid;
  18. const v6Bytes = _v1ToV6(v1Bytes);
  19. return typeof uuid === 'string' ? (0, _stringify.unsafeStringify)(v6Bytes) : v6Bytes;
  20. }
  21. // Do the field transformation needed for v1 -> v6
  22. function _v1ToV6(v1Bytes, randomize = false) {
  23. return Uint8Array.of((v1Bytes[6] & 0x0f) << 4 | v1Bytes[7] >> 4 & 0x0f, (v1Bytes[7] & 0x0f) << 4 | (v1Bytes[4] & 0xf0) >> 4, (v1Bytes[4] & 0x0f) << 4 | (v1Bytes[5] & 0xf0) >> 4, (v1Bytes[5] & 0x0f) << 4 | (v1Bytes[0] & 0xf0) >> 4, (v1Bytes[0] & 0x0f) << 4 | (v1Bytes[1] & 0xf0) >> 4, (v1Bytes[1] & 0x0f) << 4 | (v1Bytes[2] & 0xf0) >> 4, 0x60 | v1Bytes[2] & 0x0f, v1Bytes[3], v1Bytes[8], v1Bytes[9], v1Bytes[10], v1Bytes[11], v1Bytes[12], v1Bytes[13], v1Bytes[14], v1Bytes[15]);
  24. }