Passed
Push — master ( 097df7...bb6e1a )
by Night
01:18
created

stringFuncs.smartContains   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 54
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 8
nop 4
dl 0
loc 54
rs 7.3333
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/** global: UB */
2
3
var stringFuncs = {
4
	
5
	
6
	beginsWith: function(prefix, length = 0){
7
		return this.smartStartsWith(prefix, length, true);
8
	},
9
	beginsWithCI: function(prefix, length = 0){
10
		return this.smartStartsWith(prefix, length, false);
11
	},
12
	startsWith: function(prefix, length = 0){
13
		return this.smartStartsWith(prefix, length, true);
14
	},
15
	startsWithCI: function(prefix, length = 0){
16
		return this.smartStartsWith(prefix, length, false);
17
	},
18
	smartBeginsWith: function(prefix, length = 0, caseSensitive = true, trimAndCheck = false){
19
		return this.smartStartsWith(prefix, length, caseSensitive, trimAndCheck);
20
	},
21
	smartStartsWith: function(prefix, length = 0, caseSensitive = true, trimAndCheck = false){
22
		var text = this;
23
	
24
		// trim and check if needed
25
		if (trimAndCheck) {
26
			text = text.replace(UB.regex.Trim, '');
27
		}
28
		
29
		// "starts with any"
30
		if (prefix.constructor == Array){
31
			
32
			// per search term
33
			for (var s = 0, sl = prefix.length;s<sl;s++){
34
				var pre = prefix[s];
35
				
36
				// ends with one
37
				if (text.smartStartsWith(pre, length, caseSensitive)) {
38
					return true;
39
				}
40
			}
41
			
42
			// doesnt end with any given
43
			return false;
44
		}
45
46
47
		// JS startsWith() API support
48
		if (length > 0 && prefix.length > length){
49
			prefix = prefix.substr(0, length);
50
		}
51
52
		// "starts with"
53
		var l1 = text.length;
54
		var l2 = prefix.length;
55
		
56
		// check quickly if same/less length
57
		if (l1 < l2) {
58
			return false;
59
		}
60
		if (l1 == l2) {
61
			if (caseSensitive) {
62
				return text == prefix;
63
			}
64
			return text.toLowerCase() == prefix.toLowerCase();
65
		}
66
		
67
		// check using substring if main string longer
68
		if (caseSensitive) {
69
			//return text.substr(0, l2) == prefix;
70
			return text.lastIndexOf(prefix, 0) === 0;
71
		}
72
		return text.substr(0, l2).toLowerCase() == prefix.toLowerCase();
73
	},
74
	endsWith: function(postfix, length = 0){
75
		return this.smartEndsWith(postfix, length, true);
76
	},
77
	endsWithCI: function(postfix, length = 0){
78
		return this.smartEndsWith(postfix, length, false);
79
	},
80
	smartEndsWith: function(postfix, length = 0, caseSensitive = true, trimAndCheck = false){
81
		var text = this;
82
	
83
84
		// trim and check if needed
85
		if (trimAndCheck) {
86
			text = text.replace(UB.regex.Trim, '');
87
		}
88
		
89
		// "ends with any"
90
		if (postfix.constructor == Array){
91
			
92
			// per search term
93
			for (var s = 0, sl = postfix.length;s<sl;s++){
94
				var post = postfix[s];
95
				
96
				// ends with one
97
				if (text.smartEndsWith(post, length, caseSensitive)) {
98
					return true;
99
				}
100
			}
101
			
102
			// doesnt end with any given
103
			return false;
104
		}
105
106
		// JS endsWith() API support
107
		if (length > 0 && postfix.length > length){
108
			postfix = postfix.substr(0, length);
109
		}
110
111
		// "ends with"
112
		var l1 = text.length;
113
		var l2 = postfix.length;
114
		
115
		// check quickly if same/less length
116
		if (l1 < l2) {
117
			return false;
118
		}
119
		if (l1 == l2) {
120
			if (caseSensitive) {
121
				return text == postfix;
122
			}
123
			return text.toLowerCase() == postfix.toLowerCase();
124
		}
125
		
126
		// check using substring if main string longer
127
		if (caseSensitive) {
128
			//return text.substr(l1 - l2, l2) == postfix;
129
			var index = l1 - l2;
130
			return text.indexOf(postfix, index) == index;
131
		}
132
		return text.substr(l1 - l2, l2).toLowerCase() == postfix.toLowerCase();
133
	},
134
	contains: function(substring){
135
		return this.smartContains(substring, true);
136
	},
137
	containsCI: function(substring){
138
		return this.smartContains(substring, false);
139
	},
140
	smartContains: function(substring, caseSensitive = true, substringIsLower = false, wholeWords = false){
141
		var text = this;
142
		
143
		// "contains any"
144
		if (substring.constructor == Array){
145
			
146
			// per search term
147
			for (var s = 0, sl = substring.length;s<sl;s++){
148
				var sub = substring[s];
149
				
150
				// ends with one
151
				if (text.smartContains(sub, caseSensitive, substringIsLower, wholeWords)) {
152
					return true;
153
				}
154
			}
155
			
156
			// doesnt end with any given
157
			return false;
158
		}
159
160
		// "contains"
161
		if (wholeWords) {
162
			return text.match(UB.regex.New(substring, wholeWords, caseSensitive, true)).length > 0;
163
		}
164
		if (caseSensitive) {
165
			return text.indexOf(substring) != -1;
166
		}
167
		/*
168
		text = text.toLowerCase();
169
		substring = substring.toLowerCase();
170
		
171
		return text.indexOf(substring) != -1;*/
172
		
173
		
174
		// quick checks
175
		if (text.length < substring.length) {
176
			return false;
177
		}
178
		
179
		// init casing tables
180
		if (UB.UTF_lowerToUpper == null){
181
			UB.initCasing();
182
		}
183
		
184
		//temps
185
		var sl = substring.length;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable sl already seems to be declared on line 147. 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...
186
		var ml = (text.length - sl);
187
		
188
		// very fast CI comparison
189
		return text._indexOfCI(substring, 0, substringIsLower, sl, ml) > -1;
190
191
		// much faster than this:
192
		/*return text.toLowerCase().indexOf(substring.toLowerCase()) != -1;*/
193
	},
194
	beginningOf: function(text, caseSensitive = true){
195
		var prefix = this;
196
		return text.startsWith(prefix, caseSensitive);
197
	},
198
	endingOf: function(text, caseSensitive = true){
199
		var postfix = this;
200
		return text.endsWith(postfix, caseSensitive);
201
	},
202
	containedIn: function(text, caseSensitive = true, substringIsLower = false){
203
		var substring = this;
204
		return text.contains(substring, caseSensitive, substringIsLower);
205
	},
206
	
207
	// contains
208
	containsAll: function(checkPerWord, searchFor, caseSensitive = true){
209
		var str = this;
210
		
211
		// split haystack into words
212
		var words = checkPerWord?str.split(" "):[str];
213
		
214
		// compare each word of needle seperately
215
		var matched = 0;
216
		for (var w = 0, wl = words.length;w<wl;w++){
217
			
218
			var word = words[w];
219
			
220
			// return true if any haystack S.Contains the needle
221
			for (var s = 0, sl = searchFor.length; s < sl; s++) {
222
				var sWord = searchFor[s];
223
				if (word.smartContains(sWord, caseSensitive)) {
224
					matched++;
225
				}
226
			}
227
		}
228
		
229
		return matched == searchFor.length;
230
	},
231
	containsNumber: function(){
232
		var str = this;
233
		for (var c = 0, cl = str.length;c<cl;c++){
234
			var char = str.charAt(c);
235
			if (char.isNumber()) {
236
				return true;
237
			}
238
		}
239
		return false;
240
	},
241
	containsAlphaNumeric: function(){
242
		var str = this;
243
		for (var c = 0, cl = str.length;c<cl;c++){
244
			var char = str.charAt(c);
245
			if (char.isAlphaNumeric()) {
246
				return true;
247
			}
248
		}
249
		return false;
250
	},
251
	
252
	
253
	endsWithAny_Index: function(searchFor, caseSensitive = true, trimAndCheck = false){
254
		var str = this;
255
		
256
		// trim and check if needed
257
		if (trimAndCheck) {
258
			str = str.replace(UB.regex.Trim, '');
259
		}
260
		
261
		// per search term
262
		var indices = [];
0 ignored issues
show
Unused Code introduced by
The variable indices seems to be never used. Consider removing it.
Loading history...
263
		for (var s = 0, sl = searchFor.length;s<sl;s++){
264
			var sWord = searchFor[s];
265
			
266
			// ends with one
267
			if (str.endsWith(sWord, caseSensitive)) {
268
				return s;
269
			}
270
		}
271
		
272
		// doesnt end with any given
273
		return -1;
274
	},
275
	/** checks if the string begins with any of the given terms, and returns the char index of the first found term */
276
	beginsWithAny_Index: function(searchFor, caseSensitive = true, trimAndCheck = false){
277
		return this.startsWithAny_Index(searchFor, caseSensitive, trimAndCheck);
278
	},
279
	/** checks if the string begins with any of the given terms, and returns the char index of the first found term */
280
	startsWithAny_Index: function(searchFor, caseSensitive = true, trimAndCheck = false){
281
		var str = this;
282
		
283
		// trim and check if needed
284
		if (trimAndCheck) {
285
			str = str.replace(UB.regex.Trim, '');
286
		}
287
		
288
		// per search term
289
		var indices = [];
0 ignored issues
show
Unused Code introduced by
The variable indices seems to be never used. Consider removing it.
Loading history...
290
		for (var s = 0, sl = searchFor.length;s<sl;s++){
291
			var sWord = searchFor[s];
292
			
293
			// ends with one
294
			if (str.startsWith(sWord, caseSensitive)) {
295
				return s;
296
			}
297
		}
298
		
299
		// doesnt end with any given
300
		return -1;
301
	},
302
	
303
	/** checks if the string contains any of the given terms
304
	 * @param	searchFor		an array of search terms
305
	 */
306
	containsAny_Index: function(searchFor, caseSensitive = true){
307
		var str = this;
308
		
309
		// return index if found
310
		for (var s = 0, sl = searchFor.length; s < sl; s++) {
311
			var sWord = searchFor[s];
312
			var i = str.smartIndexOf(sWord, caseSensitive);
313
			if (i > -1){
314
				return i;
315
			}
316
		}
317
		
318
		return -1;
319
	},
320
	
321
	none:null
322
};
323
324
// register funcs
325
UB.registerFuncs(String.prototype, stringFuncs);