Passed
Push — master ( 3772d6...966d0d )
by Night
01:13
created

src/ub.arrays.asserts.js   A

Complexity

Total Complexity 30
Complexity/F 2.31

Size

Lines of Code 130
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
eloc 74
c 1
b 0
f 0
nc 1
dl 0
loc 130
rs 10
wmc 30
mnd 2
bc 29
fnc 13
bpm 2.2307
cpm 2.3076
noi 0

13 Functions

Rating   Name   Duplication   Size   Complexity  
A arrayFuncs.endsWith 0 4 1
A arrayFuncs.beginsWith 0 3 1
A arrayFuncs.exists 0 4 1
A arrayFuncs.isNotEqual 0 4 1
A arrayFuncs.startsWithArray 0 18 4
A arrayFuncs.isLast 0 4 1
A arrayFuncs.beginsWithArray 0 3 1
A arrayFuncs.isFirst 0 4 1
A arrayFuncs.startsWith 0 4 1
A arrayFuncs.endsWithArray 0 19 4
B arrayFuncs.isEqual 0 19 6
A arrayFuncs.allEqualTo 0 14 4
A arrayFuncs.allSame 0 15 4
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 (list == null || 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 !IsEqual(list, list2);
31
	},
32
33
	exists: function(){
34
		var list = this;
35
		return list != null && list.length > 0;
36
	},
37
	isFirst: function(value){
38
		var list = this;
39
		return list != null && list[0] == value;
40
	},
41
	isLast: function(value){
42
		var list = this;
43
		return list != null && list[list.length - 1] == value;
44
	},
45
	
46
47
	beginsWithArray: function(value){
48
		return this.startsWithArray(value);
49
	},
50
	startsWithArray: function(check){
51
		var list = this;
52
		
53
		// quickly test if length sufficient
54
		var clen = check.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] != check[b]) {
63
				return false;
64
			}
65
		}
66
		return true;
67
	},
68
	endsWithArray: function(check){
69
		var list = this;
70
		
71
		// quickly test if length sufficient
72
		var clen = check.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] != check[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
	allSame: function(){
100
		var arr = this;
101
		
102
		// checks if all values in the array are same
103
		if (arr.length == 0) {
104
			return true;
105
		}
106
		var last = arr[0];
107
		for (var a = 1, al = arr.length;a<al;a++){
108
			if (arr[a] != last) {
109
				return false;
110
			}
111
		}
112
		return true;
113
	},
114
	
115
	allEqualTo: function(value){
116
		var arr = this;
117
		
118
		// Checks if all the values in the array are equal to the given value
119
		if (arr.length == 0) {
120
			return true;
121
		}
122
		for (var a = 0, al = arr.length;a<al;a++){
123
			if (arr[a] != value) {
124
				return false;
125
			}
126
		}
127
		return true;
128
	},
129
	
130
	
131
	none:null
132
};
133
134
// register funcs
135
UB.registerFuncs(Array.prototype, arrayFuncs);
136