Conditions | 11 |
Paths | 256 |
Total Lines | 103 |
Lines | 103 |
Ratio | 100 % |
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 template.js ➔ template 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 | View Code Duplication | var assignInWith = require('./assignInWith'), |
|
134 | function template(string, options, guard) { |
||
135 | // Based on John Resig's `tmpl` implementation |
||
136 | // (http://ejohn.org/blog/javascript-micro-templating/) |
||
137 | // and Laura Doktorova's doT.js (https://github.com/olado/doT). |
||
138 | var settings = templateSettings.imports._.templateSettings || templateSettings; |
||
139 | |||
140 | if (guard && isIterateeCall(string, options, guard)) { |
||
141 | options = undefined; |
||
142 | } |
||
143 | string = toString(string); |
||
144 | options = assignInWith({}, options, settings, customDefaultsAssignIn); |
||
145 | |||
146 | var imports = assignInWith({}, options.imports, settings.imports, customDefaultsAssignIn), |
||
147 | importsKeys = keys(imports), |
||
148 | importsValues = baseValues(imports, importsKeys); |
||
149 | |||
150 | var isEscaping, |
||
151 | isEvaluating, |
||
152 | index = 0, |
||
153 | interpolate = options.interpolate || reNoMatch, |
||
154 | source = "__p += '"; |
||
155 | |||
156 | // Compile the regexp to match each delimiter. |
||
157 | var reDelimiters = RegExp( |
||
158 | (options.escape || reNoMatch).source + '|' + |
||
159 | interpolate.source + '|' + |
||
160 | (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' + |
||
161 | (options.evaluate || reNoMatch).source + '|$' |
||
162 | , 'g'); |
||
163 | |||
164 | // Use a sourceURL for easier debugging. |
||
165 | var sourceURL = 'sourceURL' in options ? '//# sourceURL=' + options.sourceURL + '\n' : ''; |
||
166 | |||
167 | string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) { |
||
168 | interpolateValue || (interpolateValue = esTemplateValue); |
||
169 | |||
170 | // Escape characters that can't be included in string literals. |
||
171 | source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar); |
||
172 | |||
173 | // Replace delimiters with snippets. |
||
174 | if (escapeValue) { |
||
175 | isEscaping = true; |
||
176 | source += "' +\n__e(" + escapeValue + ") +\n'"; |
||
177 | } |
||
178 | if (evaluateValue) { |
||
179 | isEvaluating = true; |
||
180 | source += "';\n" + evaluateValue + ";\n__p += '"; |
||
181 | } |
||
182 | if (interpolateValue) { |
||
183 | source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'"; |
||
184 | } |
||
185 | index = offset + match.length; |
||
186 | |||
187 | // The JS engine embedded in Adobe products needs `match` returned in |
||
188 | // order to produce the correct `offset` value. |
||
189 | return match; |
||
190 | }); |
||
191 | |||
192 | source += "';\n"; |
||
193 | |||
194 | // If `variable` is not specified wrap a with-statement around the generated |
||
195 | // code to add the data object to the top of the scope chain. |
||
196 | var variable = options.variable; |
||
197 | if (!variable) { |
||
198 | source = 'with (obj) {\n' + source + '\n}\n'; |
||
199 | } |
||
200 | // Cleanup code by stripping empty strings. |
||
201 | source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source) |
||
202 | .replace(reEmptyStringMiddle, '$1') |
||
203 | .replace(reEmptyStringTrailing, '$1;'); |
||
204 | |||
205 | // Frame code as the function body. |
||
206 | source = 'function(' + (variable || 'obj') + ') {\n' + |
||
207 | (variable |
||
208 | ? '' |
||
209 | : 'obj || (obj = {});\n' |
||
210 | ) + |
||
211 | "var __t, __p = ''" + |
||
212 | (isEscaping |
||
213 | ? ', __e = _.escape' |
||
214 | : '' |
||
215 | ) + |
||
216 | (isEvaluating |
||
217 | ? ', __j = Array.prototype.join;\n' + |
||
218 | "function print() { __p += __j.call(arguments, '') }\n" |
||
219 | : ';\n' |
||
220 | ) + |
||
221 | source + |
||
222 | 'return __p\n}'; |
||
223 | |||
224 | var result = attempt(function() { |
||
225 | return Function(importsKeys, sourceURL + 'return ' + source) |
||
|
|||
226 | .apply(undefined, importsValues); |
||
227 | }); |
||
228 | |||
229 | // Provide the compiled function's source by its `toString` method or |
||
230 | // the `source` property as a convenience for inlining compiled templates. |
||
231 | result.source = source; |
||
232 | if (isError(result)) { |
||
233 | throw result; |
||
234 | } |
||
235 | return result; |
||
236 | } |
||
237 | |||
239 |