forked from basharmadi/node-whatsapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
85 lines (66 loc) · 1.52 KB
/
common.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var url = require('url');
var http = require('http');
var https = require('https');
function tstamp() {
return Math.round(Date.now() / 1000);
}
function winTimestamp() {
var unixtimestamp = tstamp();
return ((unixtimestamp + 11644477200) * 10000000);
}
function objSize(obj) {
var size = 0;
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
++size;
}
}
return size;
}
function toArray(iterable) {
var arr = [];
for(var i = 0, len = iterable.length; i < len; i++) {
arr.push(iterable[i]);
}
return arr;
}
function extend(dest) {
var args = toArray(arguments),
target = args.shift();
for(var i = 0, len = args.length, source; i < len; i++) {
source = args[i];
for(var key in source) {
if(source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
}
return target;
}
function fetch(target, callback) {
var protocol = url.parse(target).protocol === 'https:' ? https : http;
protocol.get(target, function(res) {
var buffers = [];
res.on('data', function(buf) {
buffers.push(buf);
});
res.on('end', function() {
callback(false, Buffer.concat(buffers));
});
}).on('error', function(e) {
callback(e);
});
}
function isWindows() {
return /^win/.test(process.platform);;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
exports.tstamp = tstamp;
exports.winTimestamp = winTimestamp;
exports.objSize = objSize;
exports.extend = extend;
exports.fetch = fetch;
exports.isWindows = isWindows;
exports.getRandomInt = getRandomInt;