forked from therne/instauuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (54 loc) · 1.9 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
// native generator
var generator = require('./build/Release/generator');
// long support (64-bit number)
var Long = require('long');
function paramDefault(param, defaultValue) {
return param == undefined ? defaultValue : param;
}
function unLittleEndian(buf) {
for (var i=0;i<4;i++) {
buf[i] ^= buf[7-i];
buf[7-i] ^= buf[i];
buf[i] ^= buf[7-i];
}
return buf;
}
function toLong(buf) {
return new Long(buf.readInt32LE(0), buf.readInt32LE(4), true);
}
/**
* Generate Insta-UUID.
*/
function generate(options) {
options = options || {};
if (typeof options === 'string') options = { type: options };
var type = paramDefault(options.type, 'base64');
var additional = paramDefault(options.additional, 0);
var count = paramDefault(options.countNumber, parseInt(Math.random() * 10000 % 1024));
var rawBuffer = generator.generate(count, additional);
if (type === 'base64') {
// NOTE: this is URL-safe Base64Url format. not pure base64.
// + since UUID is fixed 64bit we don't have to worry about padding(=)
return unLittleEndian(rawBuffer)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
} else if (type === 'hash' || type == 'hex') {
return unLittleEndian(rawBuffer).toString('hex');
} else if (type === 'decimal' || type == 'number') {
return toLong(rawBuffer).toString();
} else if (type === 'long') {
return toLong(rawBuffer);
} else if (type == 'buffer') {
return rawBuffer;
} else if (type == 'buffer_be') {
// to big endian
return unLittleEndian(rawBuffer);
} else if (type == 'raw') {
// OMG, R u serious?
return rawBuffer.toString();
} else throw new TypeError("Unknown encoding: " + type);
}
module.exports = generate;