src/ub.arrays.strings.js   A
last analyzed

Complexity

Total Complexity 34
Complexity/F 4.86

Size

Lines of Code 106
Function Count 7

Duplication

Duplicated Lines 30
Ratio 28.3 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
eloc 61
c 1
b 0
f 0
nc 1
dl 30
loc 106
rs 9.68
wmc 34
mnd 4
bc 26
fnc 7
bpm 3.7142
cpm 4.8571
noi 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
B arrayFuncs.lastIndexOfCI 15 15 6
B arrayFuncs.removeCI 0 22 6
B arrayFuncs.indexOfCI 15 15 6
A arrayFuncs.removeIfContains 0 13 5
A arrayFuncs.removeIfStartsWith 0 13 5
A arrayFuncs.removeIfBeginsWith 0 3 1
A arrayFuncs.removeIfEndsWith 0 13 5

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
	
3
/*! STRING ARRAY UTILS */
4
5
var arrayFuncs = {
6
	
7 View Code Duplication
	indexOfCI: function(value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8
		var list = this;
9
		if (list.length === 0) {
10
			return -1;
11
		}
12
		for (var i = 0, il = list.length;i<il;i++){
13
			var str = list[i];
14
			if (str && str.constructor === String) {
15
				if (str.equalsCI(value)){
16
					return i;
17
				}
18
			}
19
		}
20
		return -1;
21
	},
22
23 View Code Duplication
	lastIndexOfCI: function(value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
24
		var list = this;
25
		if (list.length === 0) {
26
			return -1;
27
		}
28
		for (var i = list.length-1;i>=0;i--){
29
			var str = list[i];
30
			if (str && str.constructor === String) {
31
				if (str.equalsCI(value)){
32
					return i;
33
				}
34
			}
35
		}
36
		return -1;
37
	},
38
39
	removeCI: function(value, stringReplace = false){
40
		var list = this;
41
		for (var i = 0, il = list.length;i<il;i++){
42
			var str = list[i];
43
			if (str && str.constructor === String) {
44
				if (stringReplace){
45
					
46
					// remove substring within string items
47
					list[i] = str.removeAll(value, false, false);
48
49
				}else{
50
51
					// remove entire items
52
					if (str.equalsCI(value)) {
53
						list.splice(i, 1);
54
						i--;
55
						il--;
56
					}
57
				}
58
			}
59
		}
60
	},
61
62
	removeIfContains: function(value, caseSensitive = true){
63
		var list = this;
64
		for (var i = 0, il = list.length;i<il;i++){
65
66
			// remove entire items if string item contains the given substring 
67
			var str = list[i];
68
			if (str && str.constructor === String && str.smartContains(value, caseSensitive)) {
69
				list.splice(i, 1);
70
				i--;
71
				il--;
72
			}
73
		}
74
	},
75
	removeIfBeginsWith: function(value, caseSensitive = true, trimAndCheck = false){
76
		return this.removeIfStartsWith(value, caseSensitive, trimAndCheck);
77
	},
78
	removeIfStartsWith: function(value, caseSensitive = true, trimAndCheck = false){
79
		var list = this;
80
		for (var i = 0, il = list.length;i<il;i++){
81
82
			// remove entire items if string item starts with the given substring 
83
			var str = list[i];
84
			if (str && str.constructor === String && str.smartStartsWith(value, null, caseSensitive, trimAndCheck)) {
85
				list.splice(i, 1);
86
				i--;
87
				il--;
88
			}
89
		}
90
	},
91
	removeIfEndsWith: function(value, caseSensitive = true, trimAndCheck = false){
92
		var list = this;
93
		for (var i = 0, il = list.length;i<il;i++){
94
95
			// remove entire items if string item ends with the given substring 
96
			var str = list[i];
97
			if (str && str.constructor === String && str.smartEndsWith(value, null, caseSensitive, trimAndCheck)) {
98
				list.splice(i, 1);
99
				i--;
100
				il--;
101
			}
102
		}
103
	},
104
105
	
106
	none:null
107
};
108
109
// register funcs
110
UB.registerFuncs(Array.prototype, arrayFuncs);
111