Passed
Push — master ( 2ebd67...097df7 )
by Night
01:12
created

arrayFuncs.isEqual   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 19
rs 9.3333
c 1
b 0
f 0
1
/** global: UB */
2
/** global: Buffer */
3
4
/*! ARRAY UTILS */
5
6
var arrayFuncs = {
7
8
9
	isEqual: function(list2){
10
		var list = this;
11
		if (list2 == null) {
12
			return list == list2;
13
		}	
14
		if(list.length != list2.length){
15
			return false;
16
		}
17
		
18
		var len = list.length;
19
		
20
		for (var i = 0;i<len;i++){
21
			if(list[i] !== list2[i]){
22
				return false;
23
			}
24
		}
25
		
26
		return true;
27
	},
28
	isNotEqual: function(list2){
29
		var list = this;
30
		return !list.isEqual(list2);
31
	},
32
33
	exists: function(){
34
		var list = this;
35
		return list.length > 0;
36
	},
37
	isFirst: function(value){
38
		var list = this;
39
		return list[0] == value;
40
	},
41
	isLast: function(value){
42
		var list = this;
43
		return list[list.length - 1] == value;
44
	},
45
	
46
47
	beginsWithArray: function(value){
48
		return this.startsWithArray(value);
49
	},
50
	startsWithArray: function(values){
51
		var list = this;
52
		
53
		// quickly test if length sufficient
54
		var clen = values.length;
55
		var mlen = list.length;
56
		if (mlen < clen) {
57
			return false;
58
		}
59
		
60
		// check if first slots match
61
		for (var b = 0;b<clen;b++){
62
			if (list[b] != values[b]) {
63
				return false;
64
			}
65
		}
66
		return true;
67
	},
68
	endsWithArray: function(values){
69
		var list = this;
70
		
71
		// quickly test if length sufficient
72
		var clen = values.length;
73
		var mlen = list.length;
74
		if (mlen < clen) {
75
			return false;
76
		}
77
		
78
		// check if last slots match
79
		var off = (mlen - clen);
80
		for (var b = 0;b<clen;b++){
81
			if (list[off + b] != values[b]) {
82
				return false;
83
			}
84
		}
85
		return true;
86
	},
87
	beginsWith: function(value){
88
		return this.startsWith(value);
89
	},
90
	startsWith: function(value){
91
		var list = this;
92
		return list[0] == value;
93
	},
94
	endsWith: function(value){
95
		var list = this;
96
		return list[list.length - 1] == value;
97
	},
98
	
99
	next: function(obj, wrap = false){
100
		var list = this;
101
		var i = list.indexOf(obj);
102
		if (i > -1) {
103
			return (wrap && i >= (list.length - 1)) ? list[0] : list[i + 1];
104
		}
105
		return null;
106
	},
107
	prev: function(obj, wrap = false){
108
		var list = this;
109
		var i = list.indexOf(obj);
110
		if (i > 0) {
111
			return wrap ? list[list.length - 1] : list[i - 1];
112
		}
113
		return null;
114
	},
115
	
116
	
117
	allSame: function(){
118
		var arr = this;
119
		
120
		// checks if all values in the array are same
121
		if (arr.length == 0) {
122
			return true;
123
		}
124
		var last = arr[0];
125
		for (var a = 1, al = arr.length;a<al;a++){
126
			if (arr[a] != last) {
127
				return false;
128
			}
129
		}
130
		return true;
131
	},
132
	
133
	allEqualTo: function(value){
134
		var arr = this;
135
		
136
		// Checks if all the values in the array are equal to the given value
137
		if (arr.length == 0) {
138
			return true;
139
		}
140
		for (var a = 0, al = arr.length;a<al;a++){
141
			if (arr[a] != value) {
142
				return false;
143
			}
144
		}
145
		return true;
146
	},
147
	
148
	
149
	none:null
150
};
151
152
// register funcs
153
UB.registerFuncs(Array.prototype, arrayFuncs);
154