-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
66 lines (54 loc) · 1.94 KB
/
util.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
const crypto = require('crypto');
var Util = function() {}
Util.prototype.makeId = function(length, prefix, isUpperCase) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
if (!length || length == 0)
length = 8;
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
if (prefix)
text = prefix + text;
if (isUpperCase)
return text;
return text.toLowerCase();
};
Util.prototype.makeId_UpperCase = function(length, prefix) {
return this.makeId(length, prefix, true);
};
//https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
Util.prototype.generateUUID = function() {
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
Util.prototype.generateUUID2 = function() {
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
d += performance.now(); //use high-precision timer if available
}
return d + '-' + 'xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
Util.prototype.convert2Array = function(data) {
var retData = [];
for (var item in data) {
var itemSelf = data[item];
itemSelf.code = item;
retData.push(itemSelf)
}
return retData;
}
Util.prototype.getHash = function(data) {
return crypto.createHash('md5').update(data).digest("hex");
}
module.exports = new Util();