src/ub.strings.whitespace.js   B
last analyzed

Complexity

Total Complexity 52
Complexity/F 5.78

Size

Lines of Code 192
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 88
c 0
b 0
f 0
nc 2
dl 0
loc 192
rs 7.44
wmc 52
mnd 3
bc 34
fnc 9
bpm 3.7777
cpm 5.7777
noi 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A stringFuncs.removeMultiSpaces 0 25 4
A stringFuncs.removeSpaces 0 7 2
D stringFuncs.removeWhitespace 0 34 12
B stringFuncs.trimEdgesSome 0 16 6
A stringFuncs.trimEdges 0 8 1
A stringFuncs.trimLines 0 4 1
D stringFuncs.smartTrim 0 36 13
B stringFuncs.trimRight 0 20 6
B stringFuncs.trimLeft 0 20 6

How to fix   Complexity   

Complexity

Complex classes like src/ub.strings.whitespace.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
	removeWhitespace: function(spaces = true, tabs = true, doubleSpace = true, doubleNewLine = true){
7
		var str = this;
8
		
9
		if (spaces) {
10
			str = str.split(" ").join("");
11
		}
12
		
13
		if (tabs){
14
			str = str.split("\t").join("");
15
		}
16
		
17
		if (doubleSpace && !spaces){
18
			while (str.indexOf("  ") > -1) {
19
				str = str.split("  ").join(" ");
20
			}
21
		}
22
		
23
		if (doubleNewLine){
24
			while (str.indexOf("\r\n\r\n") > -1) {
25
				str = str.split("\r\n\r\n").join("\r\n");
26
			}
27
			while (str.indexOf("\n\n") > -1) {
28
				str = str.split("\n\n").join("\n");
29
			}
30
			
31
			if (doubleSpace && !spaces) {
32
				while (str.indexOf("\n \n") > -1) {
33
					str = str.split("\n \n").join("\n");
34
				}
35
			}
36
		}
37
		
38
		return str;
39
	},
40
	
41
	removeSpaces: function(onlySpaces = false){
42
		var str = this;
43
		if (onlySpaces) {
44
			return str.split(" ").join("");
45
		}
46
		return str.replace(new RegExp("\\s+", "g"), '');
47
	},
48
	removeMultiSpaces: function(){
49
		var word = this;
50
		
51
		var lastIsSpace = false;
52
		
53
		// per char
54
		var chars = [];
55
		for (var c = 0, cl = word.length;c<cl;c++){
56
			var ch = word.charAt(c);
57
			var isSpace = ch == " " || ch == "\t";
58
			
59
			// add as CAPS if wanted
60
			if (isSpace && lastIsSpace) {
61
				continue;
62
			}
63
			
64
			// next char should have caps
65
			lastIsSpace = isSpace;
66
			
67
			// add normally
68
			chars.push(ch);
69
		}
70
		
71
		return chars.join("");
72
	},
73
	
74
	// trimming
75
	smartTrim: function(inbetween = false){
76
		var str = this;
77
		
78
		// exit if blank
79
		if (str == null) {
80
			return "";
81
		}
82
		if (str.length === 0) {
83
			return str;
84
		}
85
		
86
		// Removes double / triple whitespace and replaces with single spaces
87
		if (inbetween) {
88
			return str.replace(UB.regex.Trim, '').replace(new RegExp("\\s+", "g"), ' ');
89
		}
90
		
91
		// MEM FIX: exit if no need to trim
92
		if (str.length >= 2) {
93
			var char0 = str.charCodeAt(0);
94
			var char1 = str.charCodeAt(str.length - 1);
95
			if (char0 == UB.charCodes.Space || char0 == UB.charCodes.NewlineR || char0 == UB.charCodes.NewlineN || char0 == UB.charCodes.Tab ||
96
				char1 == UB.charCodes.Space || char1 == UB.charCodes.NewlineR || char1 == UB.charCodes.NewlineN || char1 == UB.charCodes.Tab) {
97
				
98
				// trim from edges
99
				return str.replace(UB.regex.Trim, '');
100
				
101
			}
102
103
			// exit with the same object (no trimming needed!)
104
			// reduces the amount of string objects created!
105
			return str;
106
		}
107
		
108
		// trim from edges
109
		return str.replace(UB.regex.Trim, '');
110
	},
111
	trimLeft: function(){
112
		var str = this;
113
		
114
		// MEM FIX: exit if no need to trim
115
		if (str.length >= 1) {
116
			var char0 = str.charCodeAt(0);
117
			if (char0 == UB.charCodes.Space || char0 == UB.charCodes.NewlineR || char0 == UB.charCodes.NewlineN || char0 == UB.charCodes.Tab) {
118
				
119
				// trim from left edge
120
				return str.replace(UB.regex.TrimLeft, '');
121
				
122
			}
123
			
124
			// exit with the same object (no trimming needed!)
125
			// reduces the amount of string objects created!
126
			return str;
127
		}
128
		
129
		return "";
130
	},
131
	trimRight: function(){
132
		var str = this;
133
		
134
		// MEM FIX: exit if no need to trim
135
		if (str.length >= 1) {
136
			var char1 = str.charCodeAt(str.length - 1);
137
			if (char1 == UB.charCodes.Space || char1 == UB.charCodes.NewlineR || char1 == UB.charCodes.NewlineN || char1 == UB.charCodes.Tab) {
138
				
139
				// trim from right edge
140
				return str.replace(UB.regex.TrimRight, '');
141
				
142
			}
143
			
144
			// exit with the same object (no trimming needed!)
145
			// reduces the amount of string objects created!
146
			return str;
147
		}
148
		
149
		return "";
150
	},
151
	trimLines: function(removeBlanks){
152
		var str = this;
153
		return str.splitLines(removeBlanks, true).joinLines();
154
	},
155
	
156
	/**
157
	 * Strip given chars from both sides of string
158
	 */
159
	trimEdgesSome: function(list = null){
160
		var element = this;
161
		
162
		if (list == null) {
163
			list = ['\n', '\r', ' ', '\t'];
164
		}
165
		var listStr = list.join("");
166
		
167
		while ( listStr.indexOf ( element.substr ( 0, 1 ) ) > -1 && element.length > 0 ){
168
			element = element.substr ( 1 );
169
		}
170
		while ( listStr.indexOf ( element.substr ( element.length - 1 ) ) > -1 && element.length > 0){
171
			element = element.substr ( 0, element.length - 1 );
172
		}
173
		return element;
174
	},
175
	trimEdges: function(){
176
		var str = this;
177
		/**
178
		* This is a special trim which only removes first and last empty lines
179
		* and doesn't affect valid leading space on the first line.
180
		*/
181
		return str.replace(UB.regex.TrimEdges, '');
182
	},
183
	
184
	none:null
185
};
186
187
// register funcs
188
UB.registerFuncs(String.prototype, stringFuncs);
189
190
191
// register trim only if not existing already
192
if ("".trim == null){
193
	UB.registerFuncs(String.prototype, {trim: stringFuncs.smartTrim});
194
}
195