UNPKG

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