1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = _default;
|
7 | exports.URL = exports.DNS = void 0;
|
8 |
|
9 | var _bytesToUuid = _interopRequireDefault(require("./bytesToUuid.js"));
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12 |
|
13 | function uuidToBytes(uuid) {
|
14 |
|
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 |
|
22 | function stringToBytes(str) {
|
23 | str = unescape(encodeURIComponent(str));
|
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 |
|
34 | const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
35 | exports.DNS = DNS;
|
36 | const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
|
37 | exports.URL = URL;
|
38 |
|
39 | function _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');
|
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 | };
|
59 |
|
60 |
|
61 | try {
|
62 | generateUUID.name = name;
|
63 | } catch (err) {}
|
64 |
|
65 |
|
66 | generateUUID.DNS = DNS;
|
67 | generateUUID.URL = URL;
|
68 | return generateUUID;
|
69 | } |
\ | No newline at end of file |