v35.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. exports.URL = exports.DNS = void 0;
  7. var _bytesToUuid = _interopRequireDefault(require("./bytesToUuid.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function uuidToBytes(uuid) {
  10. // Note: We assume we're being passed a valid uuid string
  11. const bytes = [];
  12. uuid.replace(/[a-fA-F0-9]{2}/g, function (hex) {
  13. bytes.push(parseInt(hex, 16));
  14. });
  15. return bytes;
  16. }
  17. function stringToBytes(str) {
  18. str = unescape(encodeURIComponent(str)); // UTF8 escape
  19. const bytes = [];
  20. for (let i = 0; i < str.length; ++i) {
  21. bytes.push(str.charCodeAt(i));
  22. }
  23. return bytes;
  24. }
  25. const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
  26. exports.DNS = DNS;
  27. const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
  28. exports.URL = URL;
  29. function _default(name, version, hashfunc) {
  30. function generateUUID(value, namespace, buf, offset) {
  31. if (typeof value === 'string') {
  32. value = stringToBytes(value);
  33. }
  34. if (typeof namespace === 'string') {
  35. namespace = uuidToBytes(namespace);
  36. }
  37. if (!Array.isArray(value)) {
  38. throw TypeError('value must be an array of bytes');
  39. }
  40. if (!Array.isArray(namespace) || namespace.length !== 16) {
  41. throw TypeError('namespace must be uuid string or an Array of 16 byte values');
  42. } // Per 4.3
  43. const bytes = hashfunc(namespace.concat(value));
  44. bytes[6] = bytes[6] & 0x0f | version;
  45. bytes[8] = bytes[8] & 0x3f | 0x80;
  46. if (buf) {
  47. offset = offset || 0;
  48. for (let i = 0; i < 16; ++i) {
  49. buf[offset + i] = bytes[i];
  50. }
  51. return buf;
  52. }
  53. return (0, _bytesToUuid.default)(bytes);
  54. } // Function#name is not settable on some platforms (#270)
  55. try {
  56. generateUUID.name = name; // eslint-disable-next-line no-empty
  57. } catch (err) {} // For CommonJS default export support
  58. generateUUID.DNS = DNS;
  59. generateUUID.URL = URL;
  60. return generateUUID;
  61. }