-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
43 lines (37 loc) · 997 Bytes
/
util.js
File metadata and controls
43 lines (37 loc) · 997 Bytes
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
/****************************
* Util Functions *
****************************/
Array.prototype.shuffle = function() {
for(var j, x, i = this.length-1; i; j = randomNumber(i), x = this[--i], this[i] = this[j], this[j] = x);
return this;
};
Array.prototype.swap = function (x, y) {
if(x>this.length || y>this.length || x === y) {return}
var tem = this[x];
this[x] = this[y];
this[y] = tem;
}
Array.prototype.clone = function() {
return this.slice(0);
}
Array.prototype.deleteByValue = function (value) {
var pos = this.indexOf(value);
this.splice(pos, 1);
}
Array.prototype.next = function (index) {
if(index === this.length-1) {
return this[0];
} else {
return this[index+1];
}
}
Array.prototype.previous = function (index) {
if(index === 0) {
return this[this.length-1];
} else {
return this[index-1];
}
}
function randomNumber(boundary) {
return parseInt(Math.random() * boundary);
}