Total Complexity | 97 |
Complexity/F | 2.16 |
Lines of Code | 488 |
Function Count | 45 |
Duplicated Lines | 463 |
Ratio | 94.88 % |
Changes | 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 view/js/bootstrap-wysihtml5/amd/handlebars.runtime.amd.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 | /*! |
||
28 | define( |
||
29 | 'handlebars/safe-string',["exports"], |
||
30 | function(__exports__) { |
||
31 | |||
32 | // Build out our basic SafeString type |
||
33 | function SafeString(string) { |
||
34 | this.string = string; |
||
35 | } |
||
36 | |||
37 | SafeString.prototype.toString = function() { |
||
38 | return "" + this.string; |
||
39 | }; |
||
40 | |||
41 | __exports__["default"] = SafeString; |
||
42 | }); |
||
43 | define( |
||
44 | 'handlebars/utils',["./safe-string","exports"], |
||
45 | View Code Duplication | function(__dependency1__, __exports__) { |
|
|
|||
46 | |||
47 | /*jshint -W004 */ |
||
48 | var SafeString = __dependency1__["default"]; |
||
49 | |||
50 | var escape = { |
||
51 | "&": "&", |
||
52 | "<": "<", |
||
53 | ">": ">", |
||
54 | '"': """, |
||
55 | "'": "'", |
||
56 | "`": "`" |
||
57 | }; |
||
58 | |||
59 | var badChars = /[&<>"'`]/g; |
||
60 | var possible = /[&<>"'`]/; |
||
61 | |||
62 | function escapeChar(chr) { |
||
63 | return escape[chr] || "&"; |
||
64 | } |
||
65 | |||
66 | function extend(obj, value) { |
||
67 | for(var key in value) { |
||
68 | if(Object.prototype.hasOwnProperty.call(value, key)) { |
||
69 | obj[key] = value[key]; |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | __exports__.extend = extend;var toString = Object.prototype.toString; |
||
75 | __exports__.toString = toString; |
||
76 | // Sourced from lodash |
||
77 | // https://github.com/bestiejs/lodash/blob/master/LICENSE.txt |
||
78 | var isFunction = function(value) { |
||
79 | return typeof value === 'function'; |
||
80 | }; |
||
81 | // fallback for older versions of Chrome and Safari |
||
82 | if (isFunction(/x/)) { |
||
83 | isFunction = function(value) { |
||
84 | return typeof value === 'function' && toString.call(value) === '[object Function]'; |
||
85 | }; |
||
86 | } |
||
87 | var isFunction; |
||
88 | __exports__.isFunction = isFunction; |
||
89 | var isArray = Array.isArray || function(value) { |
||
90 | return (value && typeof value === 'object') ? toString.call(value) === '[object Array]' : false; |
||
91 | }; |
||
92 | __exports__.isArray = isArray; |
||
93 | |||
94 | function escapeExpression(string) { |
||
95 | // don't escape SafeStrings, since they're already safe |
||
96 | if (string instanceof SafeString) { |
||
97 | return string.toString(); |
||
98 | } else if (!string && string !== 0) { |
||
99 | return ""; |
||
100 | } |
||
101 | |||
102 | // Force a string conversion as this will be done by the append regardless and |
||
103 | // the regex test will do this transparently behind the scenes, causing issues if |
||
104 | // an object's to string has escaped characters in it. |
||
105 | string = "" + string; |
||
106 | |||
107 | if(!possible.test(string)) { return string; } |
||
108 | return string.replace(badChars, escapeChar); |
||
109 | } |
||
110 | |||
111 | __exports__.escapeExpression = escapeExpression;function isEmpty(value) { |
||
112 | if (!value && value !== 0) { |
||
113 | return true; |
||
114 | } else if (isArray(value) && value.length === 0) { |
||
115 | return true; |
||
116 | } else { |
||
117 | return false; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | __exports__.isEmpty = isEmpty; |
||
122 | }); |
||
123 | define( |
||
124 | 'handlebars/exception',["exports"], |
||
125 | View Code Duplication | function(__exports__) { |
|
126 | |||
127 | |||
128 | var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack']; |
||
129 | |||
130 | function Exception(message, node) { |
||
131 | var line; |
||
132 | if (node && node.firstLine) { |
||
133 | line = node.firstLine; |
||
134 | |||
135 | message += ' - ' + line + ':' + node.firstColumn; |
||
136 | } |
||
137 | |||
138 | var tmp = Error.prototype.constructor.call(this, message); |
||
139 | |||
140 | // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work. |
||
141 | for (var idx = 0; idx < errorProps.length; idx++) { |
||
142 | this[errorProps[idx]] = tmp[errorProps[idx]]; |
||
143 | } |
||
144 | |||
145 | if (line) { |
||
146 | this.lineNumber = line; |
||
147 | this.column = node.firstColumn; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | Exception.prototype = new Error(); |
||
152 | |||
153 | __exports__["default"] = Exception; |
||
154 | }); |
||
155 | define( |
||
156 | 'handlebars/base',["./utils","./exception","exports"], |
||
157 | View Code Duplication | function(__dependency1__, __dependency2__, __exports__) { |
|
158 | |||
159 | var Utils = __dependency1__; |
||
160 | var Exception = __dependency2__["default"]; |
||
161 | |||
162 | var VERSION = "1.3.0"; |
||
163 | __exports__.VERSION = VERSION;var COMPILER_REVISION = 4; |
||
164 | __exports__.COMPILER_REVISION = COMPILER_REVISION; |
||
165 | var REVISION_CHANGES = { |
||
166 | 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it |
||
167 | 2: '== 1.0.0-rc.3', |
||
168 | 3: '== 1.0.0-rc.4', |
||
169 | 4: '>= 1.0.0' |
||
170 | }; |
||
171 | __exports__.REVISION_CHANGES = REVISION_CHANGES; |
||
172 | var isArray = Utils.isArray, |
||
173 | isFunction = Utils.isFunction, |
||
174 | toString = Utils.toString, |
||
175 | objectType = '[object Object]'; |
||
176 | |||
177 | function HandlebarsEnvironment(helpers, partials) { |
||
178 | this.helpers = helpers || {}; |
||
179 | this.partials = partials || {}; |
||
180 | |||
181 | registerDefaultHelpers(this); |
||
182 | } |
||
183 | |||
184 | __exports__.HandlebarsEnvironment = HandlebarsEnvironment;HandlebarsEnvironment.prototype = { |
||
185 | constructor: HandlebarsEnvironment, |
||
186 | |||
187 | logger: logger, |
||
188 | log: log, |
||
189 | |||
190 | registerHelper: function(name, fn, inverse) { |
||
191 | if (toString.call(name) === objectType) { |
||
192 | if (inverse || fn) { throw new Exception('Arg not supported with multiple helpers'); } |
||
193 | Utils.extend(this.helpers, name); |
||
194 | } else { |
||
195 | if (inverse) { fn.not = inverse; } |
||
196 | this.helpers[name] = fn; |
||
197 | } |
||
198 | }, |
||
199 | |||
200 | registerPartial: function(name, str) { |
||
201 | if (toString.call(name) === objectType) { |
||
202 | Utils.extend(this.partials, name); |
||
203 | } else { |
||
204 | this.partials[name] = str; |
||
205 | } |
||
206 | } |
||
207 | }; |
||
208 | |||
209 | function registerDefaultHelpers(instance) { |
||
210 | instance.registerHelper('helperMissing', function(arg) { |
||
211 | if(arguments.length === 2) { |
||
212 | return undefined; |
||
213 | } else { |
||
214 | throw new Exception("Missing helper: '" + arg + "'"); |
||
215 | } |
||
216 | }); |
||
217 | |||
218 | instance.registerHelper('blockHelperMissing', function(context, options) { |
||
219 | var inverse = options.inverse || function() {}, fn = options.fn; |
||
220 | |||
221 | if (isFunction(context)) { context = context.call(this); } |
||
222 | |||
223 | if(context === true) { |
||
224 | return fn(this); |
||
225 | } else if(context === false || context == null) { |
||
226 | return inverse(this); |
||
227 | } else if (isArray(context)) { |
||
228 | if(context.length > 0) { |
||
229 | return instance.helpers.each(context, options); |
||
230 | } else { |
||
231 | return inverse(this); |
||
232 | } |
||
233 | } else { |
||
234 | return fn(context); |
||
235 | } |
||
236 | }); |
||
237 | |||
238 | instance.registerHelper('each', function(context, options) { |
||
239 | var fn = options.fn, inverse = options.inverse; |
||
240 | var i = 0, ret = "", data; |
||
241 | |||
242 | if (isFunction(context)) { context = context.call(this); } |
||
243 | |||
244 | if (options.data) { |
||
245 | data = createFrame(options.data); |
||
246 | } |
||
247 | |||
248 | if(context && typeof context === 'object') { |
||
249 | if (isArray(context)) { |
||
250 | for(var j = context.length; i<j; i++) { |
||
251 | if (data) { |
||
252 | data.index = i; |
||
253 | data.first = (i === 0); |
||
254 | data.last = (i === (context.length-1)); |
||
255 | } |
||
256 | ret = ret + fn(context[i], { data: data }); |
||
257 | } |
||
258 | } else { |
||
259 | for(var key in context) { |
||
260 | if(context.hasOwnProperty(key)) { |
||
261 | if(data) { |
||
262 | data.key = key; |
||
263 | data.index = i; |
||
264 | data.first = (i === 0); |
||
265 | } |
||
266 | ret = ret + fn(context[key], {data: data}); |
||
267 | i++; |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | } |
||
272 | |||
273 | if(i === 0){ |
||
274 | ret = inverse(this); |
||
275 | } |
||
276 | |||
277 | return ret; |
||
278 | }); |
||
279 | |||
280 | instance.registerHelper('if', function(conditional, options) { |
||
281 | if (isFunction(conditional)) { conditional = conditional.call(this); } |
||
282 | |||
283 | // Default behavior is to render the positive path if the value is truthy and not empty. |
||
284 | // The `includeZero` option may be set to treat the condtional as purely not empty based on the |
||
285 | // behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative. |
||
286 | if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) { |
||
287 | return options.inverse(this); |
||
288 | } else { |
||
289 | return options.fn(this); |
||
290 | } |
||
291 | }); |
||
292 | |||
293 | instance.registerHelper('unless', function(conditional, options) { |
||
294 | return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash}); |
||
295 | }); |
||
296 | |||
297 | instance.registerHelper('with', function(context, options) { |
||
298 | if (isFunction(context)) { context = context.call(this); } |
||
299 | |||
300 | if (!Utils.isEmpty(context)) return options.fn(context); |
||
301 | }); |
||
302 | |||
303 | instance.registerHelper('log', function(context, options) { |
||
304 | var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1; |
||
305 | instance.log(level, context); |
||
306 | }); |
||
307 | } |
||
308 | |||
309 | var logger = { |
||
310 | methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' }, |
||
311 | |||
312 | // State enum |
||
313 | DEBUG: 0, |
||
314 | INFO: 1, |
||
315 | WARN: 2, |
||
316 | ERROR: 3, |
||
317 | level: 3, |
||
318 | |||
319 | // can be overridden in the host environment |
||
320 | log: function(level, obj) { |
||
321 | if (logger.level <= level) { |
||
322 | var method = logger.methodMap[level]; |
||
323 | if (typeof console !== 'undefined' && console[method]) { |
||
324 | console[method].call(console, obj); |
||
325 | } |
||
326 | } |
||
327 | } |
||
328 | }; |
||
329 | __exports__.logger = logger; |
||
330 | function log(level, obj) { logger.log(level, obj); } |
||
331 | |||
332 | __exports__.log = log;var createFrame = function(object) { |
||
333 | var obj = {}; |
||
334 | Utils.extend(obj, object); |
||
335 | return obj; |
||
336 | }; |
||
337 | __exports__.createFrame = createFrame; |
||
338 | }); |
||
339 | define( |
||
340 | 'handlebars/runtime',["./utils","./exception","./base","exports"], |
||
341 | View Code Duplication | function(__dependency1__, __dependency2__, __dependency3__, __exports__) { |
|
342 | |||
343 | var Utils = __dependency1__; |
||
344 | var Exception = __dependency2__["default"]; |
||
345 | var COMPILER_REVISION = __dependency3__.COMPILER_REVISION; |
||
346 | var REVISION_CHANGES = __dependency3__.REVISION_CHANGES; |
||
347 | |||
348 | function checkRevision(compilerInfo) { |
||
349 | var compilerRevision = compilerInfo && compilerInfo[0] || 1, |
||
350 | currentRevision = COMPILER_REVISION; |
||
351 | |||
352 | if (compilerRevision !== currentRevision) { |
||
353 | if (compilerRevision < currentRevision) { |
||
354 | var runtimeVersions = REVISION_CHANGES[currentRevision], |
||
355 | compilerVersions = REVISION_CHANGES[compilerRevision]; |
||
356 | throw new Exception("Template was precompiled with an older version of Handlebars than the current runtime. "+ |
||
357 | "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+")."); |
||
358 | } else { |
||
359 | // Use the embedded version info since the runtime doesn't know about this revision yet |
||
360 | throw new Exception("Template was precompiled with a newer version of Handlebars than the current runtime. "+ |
||
361 | "Please update your runtime to a newer version ("+compilerInfo[1]+")."); |
||
362 | } |
||
363 | } |
||
364 | } |
||
365 | |||
366 | __exports__.checkRevision = checkRevision;// TODO: Remove this line and break up compilePartial |
||
367 | |||
368 | function template(templateSpec, env) { |
||
369 | if (!env) { |
||
370 | throw new Exception("No environment passed to template"); |
||
371 | } |
||
372 | |||
373 | // Note: Using env.VM references rather than local var references throughout this section to allow |
||
374 | // for external users to override these as psuedo-supported APIs. |
||
375 | var invokePartialWrapper = function(partial, name, context, helpers, partials, data) { |
||
376 | var result = env.VM.invokePartial.apply(this, arguments); |
||
377 | if (result != null) { return result; } |
||
378 | |||
379 | if (env.compile) { |
||
380 | var options = { helpers: helpers, partials: partials, data: data }; |
||
381 | partials[name] = env.compile(partial, { data: data !== undefined }, env); |
||
382 | return partials[name](context, options); |
||
383 | } else { |
||
384 | throw new Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); |
||
385 | } |
||
386 | }; |
||
387 | |||
388 | // Just add water |
||
389 | var container = { |
||
390 | escapeExpression: Utils.escapeExpression, |
||
391 | invokePartial: invokePartialWrapper, |
||
392 | programs: [], |
||
393 | program: function(i, fn, data) { |
||
394 | var programWrapper = this.programs[i]; |
||
395 | if(data) { |
||
396 | programWrapper = program(i, fn, data); |
||
397 | } else if (!programWrapper) { |
||
398 | programWrapper = this.programs[i] = program(i, fn); |
||
399 | } |
||
400 | return programWrapper; |
||
401 | }, |
||
402 | merge: function(param, common) { |
||
403 | var ret = param || common; |
||
404 | |||
405 | if (param && common && (param !== common)) { |
||
406 | ret = {}; |
||
407 | Utils.extend(ret, common); |
||
408 | Utils.extend(ret, param); |
||
409 | } |
||
410 | return ret; |
||
411 | }, |
||
412 | programWithDepth: env.VM.programWithDepth, |
||
413 | noop: env.VM.noop, |
||
414 | compilerInfo: null |
||
415 | }; |
||
416 | |||
417 | return function(context, options) { |
||
418 | options = options || {}; |
||
419 | var namespace = options.partial ? options : env, |
||
420 | helpers, |
||
421 | partials; |
||
422 | |||
423 | if (!options.partial) { |
||
424 | helpers = options.helpers; |
||
425 | partials = options.partials; |
||
426 | } |
||
427 | var result = templateSpec.call( |
||
428 | container, |
||
429 | namespace, context, |
||
430 | helpers, |
||
431 | partials, |
||
432 | options.data); |
||
433 | |||
434 | if (!options.partial) { |
||
435 | env.VM.checkRevision(container.compilerInfo); |
||
436 | } |
||
437 | |||
438 | return result; |
||
439 | }; |
||
440 | } |
||
441 | |||
442 | __exports__.template = template;function programWithDepth(i, fn, data /*, $depth */) { |
||
443 | var args = Array.prototype.slice.call(arguments, 3); |
||
444 | |||
445 | var prog = function(context, options) { |
||
446 | options = options || {}; |
||
447 | |||
448 | return fn.apply(this, [context, options.data || data].concat(args)); |
||
449 | }; |
||
450 | prog.program = i; |
||
451 | prog.depth = args.length; |
||
452 | return prog; |
||
453 | } |
||
454 | |||
455 | __exports__.programWithDepth = programWithDepth;function program(i, fn, data) { |
||
456 | var prog = function(context, options) { |
||
457 | options = options || {}; |
||
458 | |||
459 | return fn(context, options.data || data); |
||
460 | }; |
||
461 | prog.program = i; |
||
462 | prog.depth = 0; |
||
463 | return prog; |
||
464 | } |
||
465 | |||
466 | __exports__.program = program;function invokePartial(partial, name, context, helpers, partials, data) { |
||
467 | var options = { partial: true, helpers: helpers, partials: partials, data: data }; |
||
468 | |||
469 | if(partial === undefined) { |
||
470 | throw new Exception("The partial " + name + " could not be found"); |
||
471 | } else if(partial instanceof Function) { |
||
472 | return partial(context, options); |
||
473 | } |
||
474 | } |
||
475 | |||
476 | __exports__.invokePartial = invokePartial;function noop() { return ""; } |
||
477 | |||
478 | __exports__.noop = noop; |
||
479 | }); |
||
480 | define( |
||
481 | 'handlebars.runtime',["./handlebars/base","./handlebars/safe-string","./handlebars/exception","./handlebars/utils","./handlebars/runtime","exports"], |
||
482 | View Code Duplication | function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __exports__) { |
|
483 | |||
484 | /*globals Handlebars: true */ |
||
485 | var base = __dependency1__; |
||
486 | |||
487 | // Each of these augment the Handlebars object. No need to setup here. |
||
488 | // (This is done to easily share code between commonjs and browse envs) |
||
489 | var SafeString = __dependency2__["default"]; |
||
490 | var Exception = __dependency3__["default"]; |
||
491 | var Utils = __dependency4__; |
||
492 | var runtime = __dependency5__; |
||
493 | |||
494 | // For compatibility and usage outside of module systems, make the Handlebars object a namespace |
||
495 | var create = function() { |
||
496 | var hb = new base.HandlebarsEnvironment(); |
||
497 | |||
498 | Utils.extend(hb, base); |
||
499 | hb.SafeString = SafeString; |
||
500 | hb.Exception = Exception; |
||
501 | hb.Utils = Utils; |
||
502 | |||
503 | hb.VM = runtime; |
||
504 | hb.template = function(spec) { |
||
505 | return runtime.template(spec, hb); |
||
506 | }; |
||
507 | |||
508 | return hb; |
||
509 | }; |
||
510 | |||
511 | var Handlebars = create(); |
||
512 | Handlebars.create = create; |
||
513 | |||
514 | __exports__["default"] = Handlebars; |
||
515 | }); |