Total Complexity | 67 |
Complexity/F | 5.15 |
Lines of Code | 214 |
Function Count | 13 |
Duplicated Lines | 74 |
Ratio | 34.58 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
Complex classes like src/ub.strings.charrange.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 | |||
2 | var stringFuncs = { |
||
3 | |||
4 | // char type |
||
5 | isOfCharRange: function(type){ |
||
6 | var char = this; |
||
7 | |||
8 | // needs to be exactly 1 char! |
||
9 | if (char.length != 1) { |
||
1 ignored issue
–
show
|
|||
10 | return false; |
||
11 | } |
||
12 | |||
13 | // check type |
||
14 | if (type == charCharRange.digit) { |
||
15 | var code = char.charCodeAt(0); |
||
16 | return code >= 48 && code <= 57; |
||
17 | } |
||
18 | if (type == charCharRange.letter) { |
||
19 | var code = char.charCodeAt(0); |
||
20 | if (code >= 65 && code <= 90){ |
||
21 | return true; |
||
22 | } |
||
23 | if (code >= 97 && code <= 122){ |
||
24 | return true; |
||
25 | } |
||
26 | } |
||
27 | if (type == charCharRange.space) { |
||
28 | return char == " " || char == "\t" || char == "\r" || char == "\n"; |
||
29 | } |
||
30 | if (type == charCharRange.symbol) { |
||
31 | return symbolsCommon.indexOf(char) > -1; |
||
32 | } |
||
33 | |||
34 | return false; |
||
35 | }, |
||
36 | isNoneOfCharRange: function(type){ |
||
37 | var str = this; |
||
38 | return !str.containsCharRange(type); |
||
39 | }, |
||
40 | isAllOfCharRange: function(type, ifNoChars = false){ |
||
41 | var str = this; |
||
42 | |||
43 | // exit quickly if blank string |
||
44 | if (str == null || str.length == 0) { |
||
2 ignored issues
–
show
|
|||
45 | return ifNoChars; |
||
46 | } |
||
47 | |||
48 | // find char of type |
||
49 | for (var c = 0, cl = str.length;c<cl;c++){ |
||
50 | if (!str.charAt(c).isOfCharRange(type)) { |
||
51 | return false; |
||
52 | } |
||
53 | } |
||
54 | return true; |
||
55 | }, |
||
56 | View Code Duplication | isAllOfCharRange2: function(type1, type2, ifNoChars = false){ |
|
57 | var str = this; |
||
58 | |||
59 | // exit quickly if blank string |
||
60 | if (str == null || str.length == 0) { |
||
2 ignored issues
–
show
|
|||
61 | return ifNoChars; |
||
62 | } |
||
63 | |||
64 | // find char of type |
||
65 | for (var c = 0, cl = str.length;c<cl;c++){ |
||
66 | if (!(str.charAt(c).isOfCharRange(type1) || str.charAt(c).isOfCharRange(type2))) { |
||
67 | return false; |
||
68 | } |
||
69 | } |
||
70 | return true; |
||
71 | }, |
||
72 | containsCharRange: function(type, startAt = 0, ifNoChars = false){ |
||
73 | var str = this; |
||
74 | |||
75 | // exit quickly if blank string |
||
76 | if (str == null || str.length == 0) { |
||
2 ignored issues
–
show
|
|||
77 | return ifNoChars; |
||
78 | } |
||
79 | |||
80 | // find char of type |
||
81 | for (var c = startAt, cl = str.length;c<cl;c++){ |
||
82 | if (str.charAt(c).isOfCharRange(type)) { |
||
83 | return true; |
||
84 | } |
||
85 | } |
||
86 | return false; |
||
87 | }, |
||
88 | startsWithCharRange: function(type, ifNoChars = false){ |
||
89 | var str = this; |
||
90 | |||
91 | // exit quickly if blank string |
||
92 | if (str == null || str.length == 0) { |
||
2 ignored issues
–
show
|
|||
93 | return ifNoChars; |
||
94 | } |
||
95 | |||
96 | // check if begins with char of type |
||
97 | return str.charAt(0).isOfCharRange(type); |
||
98 | }, |
||
99 | endsWithCharRange: function(type, ifNoChars = false){ |
||
100 | var str = this; |
||
101 | |||
102 | // exit quickly if blank string |
||
103 | if (str == null || str.length == 0) { |
||
2 ignored issues
–
show
|
|||
104 | return ifNoChars; |
||
105 | } |
||
106 | |||
107 | // check if ends with char of type |
||
108 | return str.charAt(str.length - 1).isOfCharRange(type); |
||
109 | }, |
||
110 | View Code Duplication | indexOfCharRange: function(type, startAt = null, forwards = true){ |
|
111 | var str = this; |
||
112 | |||
113 | // exit quickly if blank string |
||
114 | if (str == null || str.length == 0) { |
||
2 ignored issues
–
show
|
|||
115 | return -1; |
||
116 | } |
||
117 | |||
118 | // find char of type |
||
119 | if (forwards) { |
||
120 | if (startAt == null) { |
||
1 ignored issue
–
show
|
|||
121 | startAt = 0; |
||
122 | } |
||
123 | for (var c = startAt, cl = str.length;c<cl;c++){ |
||
124 | if (str.charAt(c).isOfCharRange(type)) { |
||
125 | return c; |
||
126 | } |
||
127 | } |
||
128 | }else { |
||
129 | if (startAt == null) { |
||
130 | startAt = str.length - 1; |
||
131 | } |
||
132 | for (var c = startAt;c >= 0;c--){ |
||
133 | if (str.charAt(c).isOfCharRange(type)) { |
||
134 | return c; |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | return -1; |
||
139 | }, |
||
140 | lastIndexOfCharRange: function(type){ |
||
141 | var str = this; |
||
142 | return str.indexOfCharRange(type, null, false); |
||
143 | }, |
||
144 | restrictToCharRange: function(type, startAt = 0){ |
||
145 | var str = this; |
||
146 | |||
147 | // exit quickly if blank string |
||
148 | if (str == null || str.length == 0) { |
||
2 ignored issues
–
show
|
|||
149 | return ""; |
||
150 | } |
||
151 | |||
152 | // extract all chars of given type |
||
153 | var output = []; |
||
154 | for (var c = startAt, cl = str.length;c<cl;c++){ |
||
155 | var char = str.charAt(c); |
||
156 | if (char.isOfCharRange(type)) { |
||
157 | output.push(char); |
||
158 | } |
||
159 | } |
||
160 | return output.join(""); |
||
161 | }, |
||
162 | View Code Duplication | afterCharRange: function(type, inclusive = true, startAt = 0, forward = true, returnAll = false){ |
|
163 | var text = this; |
||
164 | if (text == null) { return returnAll ? text : ''; } |
||
1 ignored issue
–
show
|
|||
165 | if (!forward) { |
||
166 | var idx = S.LastIndexOfCharRange(text, type); |
||
167 | }else { |
||
168 | idx = S.IndexOfCharRange(text, type, startAt); |
||
169 | } |
||
170 | if (idx == -1) { return returnAll ? text : ''; } |
||
171 | if (!inclusive) { |
||
172 | idx++; |
||
173 | } |
||
174 | return text.substr(idx); |
||
175 | }, |
||
176 | View Code Duplication | beforeCharRange: function(type, inclusive = true, startAt = 0, forward = true, returnAll = false){ |
|
177 | var text = this; |
||
178 | if (text == null) { return returnAll ? text : ''; } |
||
1 ignored issue
–
show
|
|||
179 | if (forward) { |
||
180 | var idx = S.IndexOfCharRange(text, type, startAt); |
||
181 | }else { |
||
182 | idx = S.LastIndexOfCharRange(text, type); |
||
183 | } |
||
184 | if (idx == -1) { return returnAll ? text : ''; } |
||
185 | if (!inclusive) { |
||
186 | idx--; |
||
187 | } |
||
188 | return text.substr(0, idx); |
||
189 | }, |
||
190 | |||
191 | // digits |
||
192 | removeDigits: function(){ |
||
193 | var str = this; |
||
194 | |||
195 | // exit quickly if blank string |
||
196 | if (str == null || str.length == 0) { |
||
2 ignored issues
–
show
|
|||
197 | return ""; |
||
198 | } |
||
199 | |||
200 | // extract all chars that are not digits |
||
201 | var output = []; |
||
202 | for (var c = 0, cl = str.length;c<cl;c++){ |
||
203 | var char = str.charAt(c); |
||
204 | if (!S.IsDigit(char)) { |
||
205 | output.push(char); |
||
206 | } |
||
207 | } |
||
208 | return output.join(""); |
||
209 | }, |
||
210 | |||
211 | none:null |
||
212 | }; |
||
213 | |||
214 | // register funcs |
||
215 | UB.registerFuncs(String.prototype, stringFuncs); |
||