Conditions | 24 |
Paths | 844 |
Total Lines | 133 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
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.decodeCSV 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 */ |
||
104 | decodeCSV: function (headers, trimValues, columnar = false, seperator = ",") { |
||
105 | var csvString = this.toString(); |
||
106 | |||
107 | // cut String into lines |
||
108 | var lines = csvString.trim().splitLines(); |
||
109 | var sep = seperator == "auto" ? UB.CSV_detectSeperator(csvString, lines.length) : seperator; |
||
110 | |||
111 | // config |
||
112 | var hasHeaders = headers != null; |
||
113 | var returnAsObjs = hasHeaders && headers.exists(); |
||
114 | |||
115 | // status |
||
116 | var inQuoted = false; |
||
117 | |||
118 | // result |
||
119 | var linesData = []; |
||
120 | var word = []; |
||
121 | var tempHeaders = []; |
||
122 | var lineWords = []; |
||
123 | |||
124 | // per line |
||
125 | for (var l = 0, ll = lines.length; l < ll; l++) { |
||
126 | var line = lines[l]; |
||
127 | var isHeader = (l === 0 && hasHeaders); |
||
128 | |||
129 | // if we are in quoted text |
||
130 | if (inQuoted) { |
||
|
|||
131 | |||
132 | // keep taking chars |
||
133 | |||
134 | }else{ |
||
135 | |||
136 | // save words into headers array / new array |
||
137 | lineWords = []; |
||
138 | if (isHeader){ |
||
139 | if (returnAsObjs){ |
||
140 | lineWords = tempHeaders; |
||
141 | }else{ |
||
142 | lineWords = headers; |
||
143 | } |
||
144 | } |
||
145 | if (!isHeader) { |
||
146 | linesData.push(lineWords); |
||
147 | } |
||
148 | |||
149 | } |
||
150 | |||
151 | // per char |
||
152 | for (var c = 0, clast = line.length - 1; c <= clast; c++) { |
||
153 | var ch = line.charAt(c); |
||
154 | |||
155 | // if we are in quoted text |
||
156 | if (inQuoted) { |
||
157 | |||
158 | // quotes.. |
||
159 | if (ch == "\"") { |
||
160 | |||
161 | // quote may be escaped |
||
162 | if (line.charAt(c + 1) == "\"") { |
||
163 | c++; |
||
164 | word.push("\""); |
||
165 | }else { |
||
166 | |||
167 | // quote means ending quoted text |
||
168 | inQuoted = false; |
||
169 | } |
||
170 | |||
171 | continue; |
||
172 | } |
||
173 | |||
174 | // normal char |
||
175 | word.push(ch); |
||
176 | |||
177 | |||
178 | }else { |
||
179 | |||
180 | // quote means beginning quoted text |
||
181 | if (ch == "\""){ |
||
182 | inQuoted = true; |
||
183 | continue; |
||
184 | } |
||
185 | |||
186 | // comma means end of word |
||
187 | if (ch == sep) { |
||
188 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
189 | word = []; |
||
190 | continue; |
||
191 | } |
||
192 | |||
193 | // normal char |
||
194 | word.push(ch); |
||
195 | |||
196 | // newline means end of word |
||
197 | if (c == clast) { |
||
198 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
199 | word = []; |
||
200 | } |
||
201 | |||
202 | } |
||
203 | |||
204 | } |
||
205 | |||
206 | // at end of line take word |
||
207 | if (!inQuoted && word.Length > 0) { |
||
208 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
209 | word = []; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | // convert array to objs |
||
214 | if (returnAsObjs){ |
||
215 | |||
216 | // go thru all rows |
||
217 | for (var l = 0, ll = linesData.length; l < ll; l++) { |
||
218 | var row = linesData[l]; |
||
219 | var obj = {}; |
||
220 | |||
221 | // convert all cells to obj props |
||
222 | for (var h = 0, hl = headers.length; h < hl; h++) { |
||
223 | var header = headers[h]; |
||
224 | obj[header] = row[h]; |
||
225 | } |
||
226 | linesData[l] = obj; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | // convert 2D array to columnar |
||
231 | if (columnar && !returnAsObjs) { |
||
232 | linesData = linesData.transpose(); |
||
233 | } |
||
234 | |||
235 | return linesData; |
||
236 | }, |
||
237 | |||
382 |