UNPKG

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