src/ub.arrays.find.js   B
last analyzed

Complexity

Total Complexity 46
Complexity/F 4.18

Size

Lines of Code 160
Function Count 11

Duplication

Duplicated Lines 24
Ratio 15 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
eloc 91
c 1
b 0
f 0
nc 1
dl 24
loc 160
rs 8.72
wmc 46
mnd 3
bc 43
fnc 11
bpm 3.909
cpm 4.1818
noi 0

11 Functions

Rating   Name   Duplication   Size   Complexity  
A arrayFuncs.replaceOnce 0 9 3
A arrayFuncs.replaceMany 0 15 5
B arrayFuncs.indexOfArray 0 26 6
A arrayFuncs.lastIndexOfNot 12 12 4
A arrayFuncs.lastIndexOf 12 12 4
A arrayFuncs.indexOfNot 0 12 4
B arrayFuncs.containsAll 0 16 7
A arrayFuncs.contains 0 7 2
A arrayFuncs.indexOf 0 12 4
A arrayFuncs.containsAny 0 4 1
B arrayFuncs.replace 0 19 6

How to fix   Duplicated Code    Complexity   

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:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like src/ub.arrays.find.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/** global: UB */
2
/** global: Buffer */
3
4
/*! ARRAY UTILS */
5
6
var arrayFuncs = {
7
8
	indexOf: function(value){
9
		var list = this;
10
		if (list.length === 0) {
11
			return -1;
12
		}
13
		for (var a = 0, al = list.length;a<al;a++){
14
			if (list[a] == value){
15
				return a;
16
			}
17
		}
18
		return -1;
19
	},
20
	indexOfNot: function(value){
21
		var list = this;
22
		if (list.length === 0) {
23
			return -1;
24
		}
25
		for (var a = 0, al = list.length;a<al;a++){
26
			if (list[a] != value){
27
				return a;
28
			}
29
		}
30
		return -1;
31
	},
32 View Code Duplication
	lastIndexOf: function(value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
33
		var list = this;
34
		if (list.length === 0) {
35
			return -1;
36
		}
37
		for (var a = list.length-1;a>=0;a--){
38
			if (list[a] == value){
39
				return a;
40
			}
41
		}
42
		return -1;
43
	},
44 View Code Duplication
	lastIndexOfNot: function(value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
45
		var list = this;
46
		if (list.length === 0) {
47
			return -1;
48
		}
49
		for (var a = list.length-1;a>=0;a--){
50
			if (list[a] != value){
51
				return a;
52
			}
53
		}
54
		return -1;
55
	},
56
	
57
	
58
	replace: function(find, replace, stringReplace = false){
59
		var list = this;
60
		for (var i = 0, il = list.length;i<il;i++){
61
			if (stringReplace){
62
63
				// replace substring within string items
64
				var str = list[i];
65
				if (str && str.constructor === String) {
66
					list[i] = str.replaceAll(find, replace);
67
				}
68
			}else{
69
70
				// replace entire items
71
				if (list[i] == find) {
72
					list[i] = replace;
73
				}
74
			}
75
		}
76
	},
77
	replaceOnce: function(find, replace){
78
		var list = this;
79
		for (var i = 0, il = list.length;i<il;i++){
80
			if (list[i] == find) {
81
				list[i] = replace;
82
				return;
83
			}
84
		}
85
	},
86
	replaceMany: function(findArray, replaceArray){
87
		var list = this;
88
		if (findArray.length != replaceArray.length){
89
			return;
90
		}
91
		for (var i = 0, il = list.length;i<il;i++){
92
			for (var f = 0, fl = findArray.length;f<fl;f++){
93
				var find = findArray[f];
94
				if (list[i] == find) {
95
					list[i] = replaceArray[f];
96
					break;
97
				}
98
			}
99
		}
100
	},
101
	
102
	
103
	contains: function(value){
104
		var list = this;
105
		if (list.length === 1) {
106
			return list[0] == value;
107
		}
108
		return list.indexOf(value) > -1;
109
	},
110
	containsAny: function(values){
111
		var list = this;
112
		return IndexOfAny(list, values) > -1;
113
	},
114
	containsAll: function(values){
115
		var list = this;
116
		
117
		// exit if either null
118
		if (list == null || values == null || list.length === 0 || values.length === 0) {
119
			return false;
120
		}
121
		
122
		// check if all found
123
		for (var f = 0, fl = values.length;f<fl;f++){
124
			if (list.indexOf(values[f]) === -1) {
125
				return false;
126
			}
127
		}
128
		return true;
129
	},
130
	
131
132
	indexOfArray: function(containsArr){
133
		var list = this;
134
		
135
		// returns index of containing list in main array
136
		
137
		if (list.length < containsArr.length) {
138
			return -1;
139
		}
140
		
141
		var cl = containsArr.length;
142
		for (var a = 0, al = list.length - (cl - 1);a<al;a++){
143
			
144
			var allMatch = true;
145
			for (var c = 0;c<cl;c++){
146
				if (list[a+c] != containsArr[c]) {
147
					allMatch = false;
148
					break;
149
				}
150
			}
151
			
152
			if (allMatch) {
153
				return a;
154
			}
155
		}
156
		return -1;
157
	},
158
159
	
160
	
161
	none:null
162
};
163
164
// register funcs
165
UB.registerFuncs(Array.prototype, arrayFuncs);
166