-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
v35.js
69 lines (51 loc) · 1.68 KB
/
v35.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import bytesToUuid from './bytesToUuid.js';
function uuidToBytes(uuid) {
// Note: We assume we're being passed a valid uuid string
const bytes = [];
uuid.replace(/[a-fA-F0-9]{2}/g, function (hex) {
bytes.push(parseInt(hex, 16));
});
return bytes;
}
function stringToBytes(str) {
str = unescape(encodeURIComponent(str)); // UTF8 escape
const bytes = [];
for (let i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
}
return bytes;
}
export const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
export const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
export default function (name, version, hashfunc) {
function generateUUID(value, namespace, buf, offset) {
const off = (buf && offset) || 0;
if (typeof value === 'string') value = stringToBytes(value);
if (typeof namespace === 'string') namespace = uuidToBytes(namespace);
if (!Array.isArray(value)) {
throw TypeError('value must be an array of bytes');
}
if (!Array.isArray(namespace) || namespace.length !== 16) {
throw TypeError('namespace must be uuid string or an Array of 16 byte values');
}
// Per 4.3
const bytes = hashfunc(namespace.concat(value));
bytes[6] = (bytes[6] & 0x0f) | version;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
if (buf) {
for (let idx = 0; idx < 16; ++idx) {
buf[off + idx] = bytes[idx];
}
}
return buf || bytesToUuid(bytes);
}
// Function#name is not settable on some platforms (#270)
try {
generateUUID.name = name;
// eslint-disable-next-line no-empty
} catch (err) {}
// For CommonJS default export support
generateUUID.DNS = DNS;
generateUUID.URL = URL;
return generateUUID;
}