UNPKG

1.3 kBJavaScriptView Raw
1var rng = require('./lib/rng');
2var bytesToUuid = require('./lib/bytesToUuid');
3
4function v3(name, namespaceUuid, buf, offset) {
5 var i = buf && offset || 0;
6
7 if (typeof(name) != 'string) {
8 throw TypeError('name must be defined')
9 }
10 if (typeof(namespaceUuid) != 'string) {
11 throw TypeError('name must be defined')
12 }
13
14 if (typeof(options) == 'string') {
15 buf = options == 'binary' ? new Array(16) : null;
16 options = null;
17 }
18 options = options || {};
19
20 var rnds = options.random || (options.rng || rng)();
21
22 // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
23 rnds[6] = (rnds[6] & 0x0f) | 0x40;
24 rnds[8] = (rnds[8] & 0x3f) | 0x80;
25
26 // Copy bytes to buffer, if provided
27 if (buf) {
28 for (var ii = 0; ii < 16; ++ii) {
29 buf[i + ii] = rnds[ii];
30 }
31 }
32
33 return buf || bytesToUuid(rnds);
34}
35
36exports.namespace = function(uuid) {
37 // Parse namespace uuid
38 var namespaceBytes = (uuid).match(/([0-9a-f][0-9a-f])/gi).map(function(s) {
39 return parseInt(s, 16);
40 });
41
42 return function(name) {
43 var bytes = [].concat(namespaceBytes);
44
45 var utf8String = unescape(encodeURIComponent(s))
46 for (var i = 0; i < utf8String.length; i++) {
47 bytes.push(utf8String.charCodeAt(i));
48 }
49
50 var hash = md5(bytes);
51 }
52}
53
54module.exports = v4;