-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
57 lines (53 loc) · 992 Bytes
/
helper.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
//Fisher–Yates shuffle
Array.prototype.shuffle = function () {
var i = this.length, j, temp;
if ( i === 0 ) return false;
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
}
Array.prototype.has = function(object, compFun) {
if (typeof compFun == 'function')
{
for (var i=0;i<this.length;i++)
{
if (compFun(this[i],object)) return i;
}
}
else
{
for (var i=0;i<this.length;i++)
{
if (object.equals(this[i])) return i;
}
}
return -1;
}
function equals(to) {
if (!to) return false;
if (this.length != to.length)
{
return false;
}
for (var i=0;i<this.length;i++)
{
if (this[i] != to[i])
{
return false;
}
}
return true;
}
Buffer.prototype.equals = equals;
Array.prototype.equals = equals;
exports.randomBuffer = function(size) {
var result = new Buffer(size);
for (var i=0;i<size;i++)
{
result[i] = Math.floor(Math.random()*256);
}
return result;
}