-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
27 lines (24 loc) · 1.16 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
const crypto = require('crypto')
class SequentialUUID {
constructor (c) {
let config = c || {}
this.dashes = (config.dashes === undefined) ? true : config.dashes
this.valid = (config.valid === undefined) ? false : config.valid
this.unsafeBuffer = (config.unsafeBuffer === undefined) ? false : config.unsafeBuffer
}
generate () {
const timeBytes = (!this.unsafeBuffer) ? Buffer.alloc(4) : Buffer.allocUnsafe(4)
timeBytes.writeUInt32BE(Math.round(+new Date() / 1000))
let uuid = Buffer.concat([timeBytes, crypto.randomBytes(12)], 16).toString('hex')
const parts = [uuid.slice(0, 8), uuid.slice(8, 12), uuid.slice(12, 16), uuid.slice(16, 20), uuid.slice(20, 32)]
if (!this.valid) {
if (this.dashes) return parts[0] + '-' + parts[1] + '-' + parts[2] + '-' + parts[3] + '-' + parts[4]
else return uuid
}
parts[2] = '4' + parts[2].slice(1)
parts[3] = [8, 9, 'a', 'b'][Math.floor(Math.random() * 4)] + parts[3].slice(1)
if (!this.dashes) return parts[0] + parts[1] + parts[2] + parts[3] + parts[4]
return parts[0] + '-' + parts[1] + '-' + parts[2] + '-' + parts[3] + '-' + parts[4]
}
}
module.exports = SequentialUUID