-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.js
67 lines (61 loc) · 1.41 KB
/
utility.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
exports.clone = function(source) {
if(source == null) {
return null;
}
if (Object.prototype.toString.call(source) === '[object Array]') {
var clone = [];
for (var i=0; i<source.length; i++) {
clone[i] = exports.clone(source[i]);
}
return clone;
} else if (typeof(source)=="object") {
var clone = {};
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
clone[prop] = exports.clone(source[prop]);
}
}
return clone;
} else {
return source;
}
}
exports.inArray = function(array, el) {
for ( var i = array.length; i--; ) {
if ( array[i] === el ) return true;
}
return false;
}
exports.isEqArrays = function(arr1, arr2) {
if ( arr1.length !== arr2.length ) {
return false;
}
for ( var i = arr1.length; i--; ) {
if ( !exports.inArray( arr2, arr1[i] ) ) {
return false;
}
}
return true;
}
exports.arrayInArray = function(array, find) {
for (var i = array.length; i--;) {
if (exports.isEqArrays(array[i], find)) return true;
}
return false;
}
Array.prototype.remove = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
}
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};