Conditions | 32 |
Paths | 453 |
Total Lines | 164 |
Code Lines | 65 |
Lines | 95 |
Ratio | 57.93 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
Complex classes like stringFuncs.smartIndexOf 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 */ |
||
13 | smartIndexOf: function(search, caseSensitive = true, first = true, startAt = null, substringIsLower = false, wholeWords = false){ |
||
14 | var text = this; |
||
15 | |||
16 | // temps |
||
17 | var sl = search.length; |
||
18 | var ml = (text.length - sl); |
||
19 | |||
20 | // quick checks |
||
21 | if (ml < sl) { |
||
22 | return -1; |
||
23 | } |
||
24 | |||
25 | // only check equality of both strings of equal length |
||
26 | if (ml == sl) { |
||
27 | if (caseSensitive && !wholeWords) { |
||
28 | return (text == search) ? 0 : -1; |
||
29 | } |
||
30 | return text.isEqual(search, caseSensitive, false, substringIsLower) ? 0 : -1; |
||
31 | } |
||
32 | |||
33 | |||
34 | // default start at |
||
35 | if (first) { |
||
36 | if (startAt == null) { |
||
1 ignored issue
–
show
|
|||
37 | startAt = 0; |
||
38 | } |
||
39 | } else { |
||
40 | if (startAt == null) { |
||
41 | startAt = ml - sl; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | |||
46 | // if using whole words, slower version is used |
||
47 | if (wholeWords) { |
||
48 | var regex = UB.regex.New(search, wholeWords, caseSensitive, true); |
||
49 | if (first) { |
||
50 | var i = startAt == 0 ? text.search(regex) : text.substring(startAt).search(regex); |
||
51 | } else { |
||
52 | /*var i:int = startAt == (ml - sl) ? text.search(regex) : text.substring(startAt).search(regex);*/ |
||
53 | /// unsupported |
||
54 | i = -1; |
||
55 | } |
||
56 | return i; |
||
57 | } |
||
58 | |||
59 | |||
60 | |||
61 | if (first) { |
||
62 | |||
63 | //--------------------------------------------- |
||
64 | // FIRST INDEX |
||
65 | |||
66 | // CASE INSENSITIVE |
||
67 | View Code Duplication | if (!caseSensitive) { |
|
68 | |||
69 | // init casing tables |
||
70 | if (UB.UTF_lowerToUpper == null){ |
||
1 ignored issue
–
show
|
|||
71 | UB.initCasing(); |
||
72 | } |
||
73 | |||
74 | |||
75 | // very fast CI comparison |
||
76 | |||
77 | // per main char |
||
78 | for (var m = startAt;m <= ml;m++){ |
||
79 | |||
80 | |||
81 | // per substring char |
||
82 | var match = true; |
||
83 | for (var s = 0;s<sl;s++){ |
||
84 | |||
85 | var c1 = text.charCodeAt(m + s); |
||
86 | var c2 = search.charCodeAt(s); |
||
87 | |||
88 | // CI |
||
89 | if (c1 <= UB.UTF_casingTablesMax){ /// CI |
||
90 | c1 = UB.UTF_upperToLower[c1]; |
||
91 | } |
||
92 | if (!substringIsLower){ |
||
93 | if (c2 <= UB.UTF_casingTablesMax){ /// CI |
||
94 | c2 = UB.UTF_upperToLower[c2]; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | if (c1 != c2) { |
||
99 | match = false; |
||
100 | break; |
||
101 | } |
||
102 | |||
103 | } |
||
104 | |||
105 | if (match){ |
||
106 | return m; |
||
107 | } |
||
108 | |||
109 | } |
||
110 | |||
111 | return -1; |
||
112 | |||
113 | |||
114 | //return text.toUpperCase().indexOf(search.toUpperCase(), startAt); |
||
115 | } |
||
116 | |||
117 | // CASE SENSITIVE |
||
118 | return text.indexOf(search, startAt); |
||
119 | |||
120 | }else { |
||
121 | |||
122 | //--------------------------------------------- |
||
123 | // LAST INDEX |
||
124 | |||
125 | // CASE INSENSITIVE |
||
126 | View Code Duplication | if (!caseSensitive) { |
|
127 | |||
128 | // init casing tables |
||
129 | if (UB.UTF_lowerToUpper == null){ |
||
130 | UB.initCasing(); |
||
131 | } |
||
132 | |||
133 | |||
134 | // very fast CI comparison |
||
135 | |||
136 | // per main char |
||
137 | for (var m = startAt;m >= 0;m--){ |
||
138 | |||
139 | |||
140 | // per substring char |
||
141 | match = true; |
||
142 | for (var s = 0;s<sl;s++){ |
||
143 | |||
144 | c1 = text.charCodeAt(m + s); |
||
145 | c2 = search.charCodeAt(s); |
||
146 | |||
147 | // CI |
||
148 | if (c1 <= UB.UTF_casingTablesMax){ /// CI |
||
149 | c1 = UB.UTF_upperToLower[c1]; |
||
150 | } |
||
151 | if (!substringIsLower){ |
||
152 | if (c2 <= UB.UTF_casingTablesMax){ /// CI |
||
153 | c2 = UB.UTF_upperToLower[c2]; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | if (c1 != c2) { |
||
158 | match = false; |
||
159 | break; |
||
160 | } |
||
161 | |||
162 | } |
||
163 | |||
164 | if (match){ |
||
165 | return m; |
||
166 | } |
||
167 | |||
168 | } |
||
169 | |||
170 | return -1; |
||
171 | } |
||
172 | |||
173 | // CASE SENSITIVE |
||
174 | return text.lastIndexOf(search, startAt); |
||
175 | } |
||
176 | }, |
||
177 | countOf: function(find, caseSensitive = true){ |
||
393 | UB.registerFuncs(String.prototype, stringFuncs); |