UNPKG

2.01 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 const off = buf && offset || 0;
42 if (typeof value === 'string') value = stringToBytes(value);
43 if (typeof namespace === 'string') namespace = uuidToBytes(namespace);
44
45 if (!Array.isArray(value)) {
46 throw TypeError('value must be an array of bytes');
47 }
48
49 if (!Array.isArray(namespace) || namespace.length !== 16) {
50 throw TypeError('namespace must be uuid string or an Array of 16 byte values');
51 } // Per 4.3
52
53
54 const bytes = hashfunc(namespace.concat(value));
55 bytes[6] = bytes[6] & 0x0f | version;
56 bytes[8] = bytes[8] & 0x3f | 0x80;
57
58 if (buf) {
59 for (let idx = 0; idx < 16; ++idx) {
60 buf[off + idx] = bytes[idx];
61 }
62 }
63
64 return buf || (0, _bytesToUuid.default)(bytes);
65 } // Function#name is not settable on some platforms (#270)
66
67
68 try {
69 generateUUID.name = name; // eslint-disable-next-line no-empty
70 } catch (err) {} // For CommonJS default export support
71
72
73 generateUUID.DNS = DNS;
74 generateUUID.URL = URL;
75 return generateUUID;
76}
\No newline at end of file