stringify.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _validate = _interopRequireDefault(require("./validate.js"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. /**
  9. * Convert array of 16 byte values to UUID string format of the form:
  10. * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  11. */
  12. const byteToHex = [];
  13. for (let i = 0; i < 256; ++i) {
  14. byteToHex.push((i + 0x100).toString(16).substr(1));
  15. }
  16. function stringify(arr, offset = 0) {
  17. // Note: Be careful editing this code! It's been tuned for performance
  18. // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
  19. const uuid = (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase(); // Consistency check for valid UUID. If this throws, it's likely due to one
  20. // of the following:
  21. // - One or more input array values don't map to a hex octet (leading to
  22. // "undefined" in the uuid)
  23. // - Invalid input values for the RFC `version` or `variant` fields
  24. if (!(0, _validate.default)(uuid)) {
  25. throw TypeError('Stringified UUID is invalid');
  26. }
  27. return uuid;
  28. }
  29. var _default = stringify;
  30. exports.default = _default;