Passed
Push — master ( 501c3e...cc7abf )
by Night
01:09
created

stringFuncs.restrictToLettersDigits   C

Complexity

Conditions 11
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 13
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 5.4

How to fix   Complexity   

Complexity

Complex classes like stringFuncs.restrictToLettersDigits 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
	*	Returns a string truncated to a specified length with optional suffix
7
	*
8
	*	@param len The length the string should be shortend to
9
	*
10
	*	@param suffix (optional, default=...) The string to append to the end of the truncated string.
11
	*/
12
	truncate: function(len, suffix = "..."){
13
		var text = this;
14
		len -= suffix.length;
15
		var trunc = text;
16
		if (trunc.length > len) {
17
			trunc = trunc.substr(0, len);
18
			if (new RegExp("[^\\s]", "").test(text.charAt(len))) {
19
				trunc = trunc.replace(new RegExp("\\w+$|\\s+$", ""), '').trimRight();
20
			}
21
			trunc += suffix;
22
		}
23
	
24
		return trunc;
25
	},
26
	
27
	toKeywords: function(){
28
		var title = this;
29
		/* INPUT 		My Product List
30
		 * OUTPUT		product-list	 	*/
31
		
32
		// go thru all words
33
		var words = title.trim().split(" ");
34
		for (var i = 0;i<words.length;i++){
35
			
36
			// only lowercase
37
			words[i] = words[i].toLowerCase().trim();
38
			
39
			// delete word if blank or junk
40
			if (words[i] == "" ||
41
				words[i] == "," ||
42
				words[i] == ")" ||
43
				words[i] == "(" ||
44
				words[i] == "/" ||
45
				words[i] == "\\" ||
46
				words[i] == "&" ||
47
				words[i] == "\t" ||
48
					(words.length > 1 &&
49
						(words[i] == "our" ||
50
						words[i] == "us" ||
51
						words[i] == "my" ||
52
						words[i] == "it" ||
53
						words[i] == "your" ||
54
						words[i] == "new" ||
55
						words[i] == "newest" ||
56
						words[i] == "latest" ||
57
						words[i] == "and" ||
58
						words[i] == "or" ||
59
						words[i] == "the" ||
60
						words[i] == "best" ||
61
						words[i] == "all" ||
62
						words[i] == "in" ||
63
						words[i] == "on" ||
64
						words[i] == "out" ||
65
						words[i] == "top"||
66
						words[i] == "how" ||
67
						words[i] == "why" ||
68
						words[i] == "what" ||
69
						words[i] == "where" ||
70
						words[i] == "when" ||
71
						words[i] == "which" ||
72
						words[i] == "who" ||
73
						words[i] == "whom"
74
						)
75
					)
76
				) {
77
				words.splice(i, 1);
78
				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...
79
			}
80
		}
81
		
82
		// calculate an ID from title words
83
		return words.join("-");
84
	},
85
	
86
	
87
	toCodeWord: function(useCamelCase = false, firstCharCaps = false, keepSymbols = false){
88
		var str = this;
89
		/* INPUT 		~!@#$%&* {:'\"<>[ © € my characters 2007
90
		 * OUTPUT		MyCharacters2007				*/
91
		
92
		 if (keepSymbols) {
93
			str = str.flattenSymbols();
94
		 }
95
		 
96
		var result = "";
97
		for (var a = 0;a<str.length;a++){
98
			var c = str.charAt(a);
99
			if (c.isAlphaNumeric() || (useCamelCase && c == " ") || c == "_"){
100
				result += c;
101
			}
102
		}
103
		if (result.charAt(0).isDigit()) {
104
			result = "_" + result;
105
		}
106
		return useCamelCase ? result.camelCaseMerge(firstCharCaps) : result;
107
	}, 
108
	restrictToLettersDigits: function(spacesOk = false){
109
		var str = this;
110
		/* INPUT 		~!@#$%&* {:'\"<>[ © € Characters 2007
111
		 * OUTPUT		Characters2007								*/
112
		if (str == null) {
1 ignored issue
show
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
113
			return null;
114
		}
115
		var result = [];
116
		for (var a = 0;a<str.length;a++){
117
			var char = str.charCodeAt(a);
118
			if ((char >= UB.charCodes._0 && char <= UB.charCodes._9) ||
119
				(char >= UB.charCodes.A && char <= UB.charCodes.Z) ||
120
				(char >= UB.charCodes.a && char <= UB.charCodes.z) ||
121
				(spacesOk && char == UB.charCodes.Space)) {
122
				result.push(str.charAt(a));
123
			}
124
		}
125
		return result.join("");
126
	}, 
127
	restrictToLetters: function(spacesOk = false){
128
		var str = this;
129
		/* INPUT 		~!@#$%&* {:'\"<>[ © € Characters 2007
130
		 * OUTPUT		Characters								*/
131
		if (str == null) {
1 ignored issue
show
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
132
			return null;
133
		}
134
		var result = [];
135
		for (var a = 0;a<str.length;a++){
136
			var char = str.charCodeAt(a);
137
			if ((char >= UB.charCodes.A && char <= UB.charCodes.Z) ||
138
				(char >= UB.charCodes.a && char <= UB.charCodes.z) ||
139
				(spacesOk && char == UB.charCodes.Space)) {
140
				result.push(str.charAt(a));
141
			}
142
		}
143
		return result.join("");
144
	}, 
145
	restrictToDigits: function(spacesOk = false){
146
		var str = this;
147
		/* INPUT 		~!@#$%&* {:'\"<>[ © € Characters 2007
148
		 * OUTPUT		2007								*/
149
		if (str == null) {
1 ignored issue
show
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
150
			return null;
151
		}
152
		var result = [];
153
		for (var a = 0;a<str.length;a++){
154
			var char = str.charCodeAt(a);
155
			if ((char >= UB.charCodes._0 && char <= UB.charCodes._9) ||
156
				(spacesOk && char == UB.charCodes.Space)) {
157
				result.push(str.charAt(a));
158
			}
159
		}
160
		return result.join("");
161
	},
162
	restrictToFilename: function(replaceWith = "_"){
163
		var path = this;
164
		return P.EnsureValidFilename(path, replaceWith);
0 ignored issues
show
Bug introduced by
The variable P seems to be never declared. If this is a global, consider adding a /** global: P */ 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...
165
	},
166
	none:null
167
};
168
169
// register funcs
170
UB.registerFuncs(String.prototype, stringFuncs);