Passed
Push — master ( 90e1d7...cffac5 )
by Night
01:52
created

src/ub.strings.find.substring.js   F

Complexity

Total Complexity 64
Complexity/F 4.92

Size

Lines of Code 190
Function Count 13

Duplication

Duplicated Lines 34
Ratio 17.89 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
eloc 130
c 1
b 0
f 0
nc 1
dl 34
loc 190
rs 3.28
wmc 64
mnd 3
bc 53
fnc 13
bpm 4.0769
cpm 4.923
noi 0

13 Functions

Rating   Name   Duplication   Size   Complexity  
A stringFuncs.beforeIndex 0 13 4
A stringFuncs.beforeLast 0 6 3
A stringFuncs.afterIndex 0 13 4
B stringFuncs.after 17 17 8
A stringFuncs.afterFirst 0 7 3
B stringFuncs.before 17 17 8
A stringFuncs.beforeFirst 0 6 3
B stringFuncs.afterNth 0 18 7
B stringFuncs.beforeNth 0 18 7
B stringFuncs.between 0 23 7
A stringFuncs.betweenLast 0 4 1
A stringFuncs.afterLast 0 7 3
B stringFuncs.betweenAll 0 26 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.strings.find.substring.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
3
var stringFuncs = {
4
5
	
6
	// other search functions
7 View Code Duplication
	after: function(find, returnAll = true, inclusive = false, startAt = 0, forward = true){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8
		var text = this.toString();
9
		if (text == null) { return returnAll ? text : ''; }
10
		if (text.length === 0) {
11
			return text;
12
		}
13
		if (!forward) {
14
			var idx = text.lastIndexOf(find, startAt);
15
		}else {
16
			idx = text.indexOf(find, startAt);
17
		}
18
		if (idx === -1) { return returnAll ? text : ''; }
19
		if (!inclusive) {
20
			idx += find.length;
21
		}
22
		return text.substr(idx);
23
	},
24 View Code Duplication
	before: function(find, returnAll = true, inclusive = false, startAt = 0, forward = true){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
		var text = this.toString();
26
		if (text == null) { return returnAll ? text : ''; }
27
		if (text.length === 0) {
28
			return text;
29
		}
30
		if (forward) {
31
			var idx = text.indexOf(find, startAt);
32
		}else {
33
			idx = text.lastIndexOf(find, startAt);
34
		}
35
		if (idx === -1) { return returnAll ? text : ''; }
36
		if (inclusive) {
37
			idx += find.length;
38
		}
39
		return text.substr(0, idx);
40
	},
41
	
42
	afterIndex: function(index, inclusive){
43
		var text = this.toString();
44
		if (!inclusive) {
45
			index++;
46
		}
47
		if (index <= 0) {
48
			return text;
49
		}
50
		if (index >= text.length) {
51
			return "";
52
		}
53
		return text.substr(index);
54
	},
55
	beforeIndex: function(index, inclusive){
56
		var text = this.toString();
57
		if (inclusive) {
58
			index++;
59
		}
60
		if (index <= 0) {
61
			return "";
62
		}
63
		if (index >= text.length) {
64
			return text;
65
		}
66
		return text.substr(0, index);
67
	},
68
	
69
	afterFirst: function(find, returnAll = true){
70
		var text = this.toString();
71
		var idx = text.indexOf(find);
72
		if (idx === -1) { return returnAll ? text : ''; }
73
		idx += find.length;
74
		return text.substr(idx);
75
	},
76
	afterLast: function(find, returnAll = true){
77
		var text = this.toString();
78
		var idx = text.lastIndexOf(find);
79
		if (idx === -1) { return returnAll ? text : ''; }
80
		idx += find.length;
81
		return text.substr(idx);
82
	},
83
	afterNth: function(find, n, returnAll = true){
84
		var text = this.toString();
85
		if (n === 0) {
86
			return text;
87
		}
88
		var i = -2;
89
		var c = 0;
90
		while (i != -1) {
91
			i = text.indexOf(find, i < 0 ? 0 : (i + 1));
92
			c++;
93
			if (i > -1) {
94
				if (c == n) {
95
					return text.substr(i + find.length);
96
				}
97
			}
98
		}
99
		return returnAll ? text : '';
100
	},
101
	
102
	beforeFirst: function(find, returnAll = true){
103
		var text = this.toString();
104
		var idx = text.indexOf(find);
105
	        	if (idx === -1) { return returnAll ? text : ''; }
106
	        	return text.substr(0, idx);
107
	},
108
	beforeLast: function(find, returnAll = true){
109
		var text = this.toString();
110
		var idx = text.lastIndexOf(find);
111
	        	if (idx === -1) { return returnAll ? text : ''; }
112
	        	return text.substr(0, idx);
113
	},
114
	beforeNth: function(find, n, returnAll = true){
115
		var text = this.toString();
116
		if (n === 0) {
117
			return text;
118
		}
119
		var i = -2;
120
		var c = 0;
121
		while (i != -1) {
122
			i = text.indexOf(find, i < 0 ? 0 : (i + 1));
123
			c++;
124
			if (i > -1) {
125
				if (c == n) {
126
					return text.substring(0, i);
127
				}
128
			}
129
		}
130
		return returnAll ? text : '';
131
	},
132
133
	between: function(p_start, p_end, startAt = 0, forward = true, returnAll = true){
134
		var text = this.toString();
135
		if (text.length === 0) {
136
			return text;
137
		}
138
		if (forward) {
139
			var startIdx = text.indexOf(p_start, startAt);
140
		}else {
141
			startIdx = text.lastIndexOf(p_start, startAt);
142
		}
143
		if (startIdx != -1) {
144
			startIdx += p_start.length;
145
			var endIdx = text.indexOf(p_end, startIdx);
146
			if (endIdx != -1) {
147
				return text.substr(startIdx, endIdx - startIdx);
148
			}else if (returnAll) {
149
				return text;
150
			}
151
		}else if (returnAll) {
152
			return text;
153
		}
154
		return '';
155
	},
156
	betweenLast: function(p_start, p_end){
157
		var text = this.toString();
158
		return text.between(p_start, p_end, text.length, false);
159
	},
160
	betweenAll: function(textStart, textEnd, returnAll = true, returnAllSplitBy = "\n"){
161
		var text = this.toString();
162
	
163
		// return if blank
164
		var betweens = [];
165
		if (textStart.length === 0 || textEnd.length === 0) {
166
			return returnAll ? text.split(returnAllSplitBy) : betweens;
167
		}
168
	
169
		// per occurance of start marker
170
		var start = 0;
171
		while ((start = text.indexOf(textStart, start)) > -1) {
172
			start += textStart.length;
173
	
174
			// find end marker
175
			var end = text.indexOf(textEnd, start + 1);
176
			if (end === -1) {
177
				break;
178
			}
179
	
180
			// add text between start and end marker
181
			betweens.push(text.substr(start, end - start));
182
			start = end + 1;
183
		}
184
		return betweens;
185
	},
186
	
187
188
	none:null
189
};
190
191
// register funcs
192
UB.registerFuncs(String.prototype, stringFuncs);