-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (52 loc) · 1.28 KB
/
index.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
const uuid = require('uuid');
const BaseX = require('base-x');
const CODEX = '0123456789abcdefghijklmnopqrstuvwxyz';
// const CODEX = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const basex = BaseX(CODEX);
const nanoUUID = {
/**
* encode binary to base36
* @param {Uint8Array} buffer
* @returns {String} base36
*/
encode(buffer) {
return basex.encode(buffer);
},
/**
* decode base36 to Unit8Array
* @param {String} str base36
* @returns {Uint8Array} buffer
*/
decode(str) {
return basex.decode(str);
},
v1(options, buffer, offset) {
if (!buffer) {
buffer = new Uint8Array(16);
}
uuid.v1(options, buffer, offset);
return basex.encode(buffer);
},
v3(name, namespace, buffer, offset) {
if (!buffer) {
buffer = new Uint8Array(16);
}
uuid.v3(name, namespace, buffer, offset);
return basex.encode(buffer);
},
v4(options, buffer, offset) {
if (!buffer) {
buffer = new Uint8Array(16);
}
uuid.v4(options, buffer, offset);
return basex.encode(buffer);
},
v5(name, namespace, buffer, offset) {
if (!buffer) {
buffer = new Uint8Array(16);
}
uuid.v5(name, namespace, buffer, offset);
return basex.encode(buffer);
},
};
module.exports = nanoUUID;