Passed
Push — master ( 2a3177...723b76 )
by Night
01:11
created

arrayFuncs.removeIfStartsWith   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 13
rs 9.3333
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) {
1 ignored issue
show
Best Practice introduced by
Comparing list.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
10
			return -1;
11
		}
12
		var list = this;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable list already seems to be declared on line 8. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
13
		for (var i = 0, il = list.length;i<il;i++){
14
			var str = list[i];
15
			if (str && str.constructor === String) {
16
				if (str.equalsCI(value)){
17
					return i;
18
				}
19
			}
20
		}
21
		return -1;
22
	},
23
24 View Code Duplication
	lastIndexOfCI: function(value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
		var list = this;
26
		if (list.length == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing list.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
27
			return -1;
28
		}
29
		for (var i = list.length-1;i>=0;i--){
30
			var str = list[i];
31
			if (str && str.constructor === String) {
32
				if (str.equalsCI(value)){
33
					return i;
34
				}
35
			}
36
		}
37
		return -1;
38
	},
39
40
	removeCI: function(value, stringReplace = false){
41
		var list = this;
42
		for (var i = 0, il = list.length;i<il;i++){
43
			var str = list[i];
44
			if (str && str.constructor === String) {
45
				if (stringReplace){
46
					
47
					// remove substring within string items
48
					list[i] = str.removeAll(find, false, false);
0 ignored issues
show
Bug introduced by
The variable find seems to be never declared. If this is a global, consider adding a /** global: find */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
49
50
				}else{
51
52
					// remove entire items
53
					if (str.equalsCI(value)) {
54
						list.splice(i, 1);
55
						i--;
1 ignored issue
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
56
						il--;
57
					}
58
				}
59
			}
60
		}
61
	},
62
63
	removeIfContains: function(value, caseSensitive = true){
64
		var list = this;
65
		for (var i = 0, il = list.length;i<il;i++){
66
67
			// remove entire items if string item contains the given substring 
68
			var str = list[i];
69
			if (str && str.constructor === String && str.smartContains(value, caseSensitive)) {
70
				list.splice(i, 1);
71
				i--;
1 ignored issue
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
72
				il--;
73
			}
74
		}
75
	},
76
	removeIfBeginsWith: function(value, caseSensitive = true, trimAndCheck = false){
77
		return this.removeIfStartsWith(value, caseSensitive, trimAndCheck);
78
	},
79
	removeIfStartsWith: function(value, caseSensitive = true, trimAndCheck = false){
80
		var list = this;
81
		for (var i = 0, il = list.length;i<il;i++){
82
83
			// remove entire items if string item starts with the given substring 
84
			var str = list[i];
85
			if (str && str.constructor === String && str.smartStartsWith(value, null, caseSensitive, trimAndCheck)) {
86
				list.splice(i, 1);
87
				i--;
1 ignored issue
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
88
				il--;
89
			}
90
		}
91
	},
92
	removeIfEndsWith: function(value, caseSensitive = true, trimAndCheck = false){
93
		var list = this;
94
		for (var i = 0, il = list.length;i<il;i++){
95
96
			// remove entire items if string item ends with the given substring 
97
			var str = list[i];
98
			if (str && str.constructor === String && str.smartEndsWith(value, null, caseSensitive, trimAndCheck)) {
99
				list.splice(i, 1);
100
				i--;
1 ignored issue
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
101
				il--;
102
			}
103
		}
104
	},
105
106
	
107
	none:null
108
};
109
110
// register funcs
111
UB.registerFuncs(Array.prototype, arrayFuncs);
112