-
Notifications
You must be signed in to change notification settings - Fork 0
/
shareadFuns.js
60 lines (54 loc) · 1.57 KB
/
shareadFuns.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
var crypto = require("crypto");
var http = require("http");
exports.currentTimeMillis = function () {
return new Date().getTime();
}
exports.getMD5String= function(str) {
return crypto.createHash("md5").update(str).digest("hex");
}
exports.httpGet = function(url, callback, ua) {
if (callback == undefined)
return;
var options = require("url").parse(url);
if (ua != undefined)
options.headers = {"User-Agent":ua};
http.get(options,
function(res) {
var size = 0;
var chunks = [];
res.on('data',
function(chunk) {
size += chunk.length;
chunks.push(chunk);
});
res.on('end',
function() {
var data = Buffer.concat(chunks, size);
callback(data.toString());
});
})
.on('error',
function() {
callback(null);
});
}
exports.isContains=function (str, subStr) {
return str.indexOf(subStr)!=-1;
}
exports.first = function (arr,callback) {
if (callback == undefined)
return arr[0];
for (var i = 0; i < arr.length; i++) {
var x = arr[i];
if (callback(x)) {
return x;
}
}
return null;
}
exports.random= function(min,max) {
var a = max - min,
b = Math.random() * a + min,
c = Math.floor(b);
return c;
}