Passed
Push — master ( ad9a28...55041a )
by Night
56s
created

src/ub.arrays.asserts.js   A

Complexity

Total Complexity 31
Complexity/F 2.38

Size

Lines of Code 122
Function Count 13

Duplication

Duplicated Lines 49
Ratio 40.16 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
eloc 73
nc 1
dl 49
loc 122
rs 9.92
c 1
b 0
f 0
wmc 31
mnd 2
bc 27
fnc 13
bpm 2.0769
cpm 2.3846
noi 7

13 Functions

Rating   Name   Duplication   Size   Complexity  
A arrayFuncs.prev 0 11 4
A arrayFuncs.endsWith 0 4 1
A arrayFuncs.beginsWith 0 3 1
A arrayFuncs.endsWithArray 19 19 4
B arrayFuncs.isEqual 19 19 6
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.next 11 11 5
A arrayFuncs.startsWith 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/** global: UB */
2
/** global: Buffer */
3
4
/*! ARRAY UTILS */
5
6
var arrayFuncs = {
7
8
9 View Code Duplication
	isEqual: function(list2){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
		var list = this;
11
		if (list == null || list2 == null) {
2 ignored issues
show
Best Practice introduced by
Comparing list to null using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing list2 to null using the == operator is not safe. Consider using === instead.
Loading history...
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;
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the != operator is not safe. Consider using !== instead.
Loading history...
36
	},
37
	isFirst: function(value){
38
		var list = this;
39
		return list != null && list[0] == value;
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the != operator is not safe. Consider using !== instead.
Loading history...
40
	},
41
	isLast: function(value){
42
		var list = this;
43
		return list != null && list[list.length - 1] == value;
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the != operator is not safe. Consider using !== instead.
Loading history...
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 View Code Duplication
	endsWithArray: function(check){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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 View Code Duplication
	next: function(obj, wrap = false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
100
		var list = this;
101
		if (list == null) {
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the == operator is not safe. Consider using === instead.
Loading history...
102
			return null;
103
		}
104
		var i = list.indexOf(obj);
105
		if (i > -1) {
106
			return (wrap && i >= (list.length - 1)) ? list[0] : list[i + 1];
107
		}
108
		return null;
109
	},
110
	prev: function(obj, wrap = false){
111
		var list = this;
112
		if (list == null) {
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the == operator is not safe. Consider using === instead.
Loading history...
113
			return null;
114
		}
115
		var i = list.indexOf(obj);
116
		if (i > 0) {
117
			return wrap ? list[list.length - 1] : list[i - 1];
118
		}
119
		return null;
120
	},
121
	
122
	
123
	none:null
124
};
125
126
// register funcs
127
UB.registerFuncs(Array.prototype, arrayFuncs);
128