Total Complexity | 42 |
Complexity/F | 1.83 |
Lines of Code | 526 |
Function Count | 23 |
Duplicated Lines | 426 |
Ratio | 80.99 % |
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 third-party/angularjs/angular-1.6.4/angular-loader.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 | /** |
||
7 | (function() {'use strict'; |
||
8 | function isFunction(value) {return typeof value === 'function';} |
||
9 | function isDefined(value) {return typeof value !== 'undefined';} |
||
10 | function isObject(value) {return value !== null && typeof value === 'object';} |
||
11 | |||
12 | /* global toDebugString: true */ |
||
13 | |||
14 | function serializeObject(obj, maxDepth) { |
||
15 | var seen = []; |
||
16 | |||
17 | // There is no direct way to stringify object until reaching a specific depth |
||
18 | // and a very deep object can cause a performance issue, so we copy the object |
||
19 | // based on this specific depth and then stringify it. |
||
20 | if (isValidObjectMaxDepth(maxDepth)) { |
||
21 | obj = copy(obj, null, maxDepth); |
||
22 | } |
||
23 | return JSON.stringify(obj, function(key, val) { |
||
24 | val = toJsonReplacer(key, val); |
||
25 | if (isObject(val)) { |
||
26 | |||
27 | if (seen.indexOf(val) >= 0) return '...'; |
||
28 | |||
29 | seen.push(val); |
||
30 | } |
||
31 | return val; |
||
32 | }); |
||
33 | } |
||
34 | |||
35 | function toDebugString(obj, maxDepth) { |
||
36 | if (typeof obj === 'function') { |
||
37 | return obj.toString().replace(/ \{[\s\S]*$/, ''); |
||
38 | } else if (isUndefined(obj)) { |
||
39 | return 'undefined'; |
||
40 | } else if (typeof obj !== 'string') { |
||
41 | return serializeObject(obj, maxDepth); |
||
42 | } |
||
43 | return obj; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @description |
||
48 | * |
||
49 | * This object provides a utility for producing rich Error messages within |
||
50 | * Angular. It can be called as follows: |
||
51 | * |
||
52 | * var exampleMinErr = minErr('example'); |
||
53 | * throw exampleMinErr('one', 'This {0} is {1}', foo, bar); |
||
54 | * |
||
55 | * The above creates an instance of minErr in the example namespace. The |
||
56 | * resulting error will have a namespaced error code of example.one. The |
||
57 | * resulting error will replace {0} with the value of foo, and {1} with the |
||
58 | * value of bar. The object is not restricted in the number of arguments it can |
||
59 | * take. |
||
60 | * |
||
61 | * If fewer arguments are specified than necessary for interpolation, the extra |
||
62 | * interpolation markers will be preserved in the final string. |
||
63 | * |
||
64 | * Since data will be parsed statically during a build step, some restrictions |
||
65 | * are applied with respect to how minErr instances are created and called. |
||
66 | * Instances should have names of the form namespaceMinErr for a minErr created |
||
67 | * using minErr('namespace') . Error codes, namespaces and template strings |
||
68 | * should all be static strings, not variables or general expressions. |
||
69 | * |
||
70 | * @param {string} module The namespace to use for the new minErr instance. |
||
71 | * @param {function} ErrorConstructor Custom error constructor to be instantiated when returning |
||
72 | * error from returned function, for cases when a particular type of error is useful. |
||
73 | * @returns {function(code:string, template:string, ...templateArgs): Error} minErr instance |
||
74 | */ |
||
75 | |||
76 | View Code Duplication | function minErr(module, ErrorConstructor) { |
|
77 | ErrorConstructor = ErrorConstructor || Error; |
||
78 | return function() { |
||
79 | var code = arguments[0], |
||
80 | template = arguments[1], |
||
81 | message = '[' + (module ? module + ':' : '') + code + '] ', |
||
82 | templateArgs = sliceArgs(arguments, 2).map(function(arg) { |
||
83 | return toDebugString(arg, minErrConfig.objectMaxDepth); |
||
84 | }), |
||
85 | paramPrefix, i; |
||
86 | |||
87 | message += template.replace(/\{\d+\}/g, function(match) { |
||
88 | var index = +match.slice(1, -1); |
||
89 | |||
90 | if (index < templateArgs.length) { |
||
91 | return templateArgs[index]; |
||
92 | } |
||
93 | |||
94 | return match; |
||
95 | }); |
||
96 | |||
97 | message += '\nhttp://errors.angularjs.org/1.6.4/' + |
||
98 | (module ? module + '/' : '') + code; |
||
99 | |||
100 | for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') { |
||
101 | message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]); |
||
102 | } |
||
103 | |||
104 | return new ErrorConstructor(message); |
||
105 | }; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @ngdoc type |
||
110 | * @name angular.Module |
||
111 | * @module ng |
||
112 | * @description |
||
113 | * |
||
114 | * Interface for configuring angular {@link angular.module modules}. |
||
115 | */ |
||
116 | |||
117 | View Code Duplication | function setupModuleLoader(window) { |
|
118 | |||
119 | var $injectorMinErr = minErr('$injector'); |
||
120 | var ngMinErr = minErr('ng'); |
||
121 | |||
122 | function ensure(obj, name, factory) { |
||
123 | return obj[name] || (obj[name] = factory()); |
||
124 | } |
||
125 | |||
126 | var angular = ensure(window, 'angular', Object); |
||
127 | |||
128 | // We need to expose `angular.$$minErr` to modules such as `ngResource` that reference it during bootstrap |
||
129 | angular.$$minErr = angular.$$minErr || minErr; |
||
130 | |||
131 | return ensure(angular, 'module', function() { |
||
132 | /** @type {Object.<string, angular.Module>} */ |
||
133 | var modules = {}; |
||
134 | |||
135 | /** |
||
136 | * @ngdoc function |
||
137 | * @name angular.module |
||
138 | * @module ng |
||
139 | * @description |
||
140 | * |
||
141 | * The `angular.module` is a global place for creating, registering and retrieving Angular |
||
142 | * modules. |
||
143 | * All modules (angular core or 3rd party) that should be available to an application must be |
||
144 | * registered using this mechanism. |
||
145 | * |
||
146 | * Passing one argument retrieves an existing {@link angular.Module}, |
||
147 | * whereas passing more than one argument creates a new {@link angular.Module} |
||
148 | * |
||
149 | * |
||
150 | * # Module |
||
151 | * |
||
152 | * A module is a collection of services, directives, controllers, filters, and configuration information. |
||
153 | * `angular.module` is used to configure the {@link auto.$injector $injector}. |
||
154 | * |
||
155 | * ```js |
||
156 | * // Create a new module |
||
157 | * var myModule = angular.module('myModule', []); |
||
158 | * |
||
159 | * // register a new service |
||
160 | * myModule.value('appName', 'MyCoolApp'); |
||
161 | * |
||
162 | * // configure existing services inside initialization blocks. |
||
163 | * myModule.config(['$locationProvider', function($locationProvider) { |
||
164 | * // Configure existing providers |
||
165 | * $locationProvider.hashPrefix('!'); |
||
166 | * }]); |
||
167 | * ``` |
||
168 | * |
||
169 | * Then you can create an injector and load your modules like this: |
||
170 | * |
||
171 | * ```js |
||
172 | * var injector = angular.injector(['ng', 'myModule']) |
||
173 | * ``` |
||
174 | * |
||
175 | * However it's more likely that you'll just use |
||
176 | * {@link ng.directive:ngApp ngApp} or |
||
177 | * {@link angular.bootstrap} to simplify this process for you. |
||
178 | * |
||
179 | * @param {!string} name The name of the module to create or retrieve. |
||
180 | * @param {!Array.<string>=} requires If specified then new module is being created. If |
||
181 | * unspecified then the module is being retrieved for further configuration. |
||
182 | * @param {Function=} configFn Optional configuration function for the module. Same as |
||
183 | * {@link angular.Module#config Module#config()}. |
||
184 | * @returns {angular.Module} new module with the {@link angular.Module} api. |
||
185 | */ |
||
186 | return function module(name, requires, configFn) { |
||
187 | |||
188 | var info = {}; |
||
189 | |||
190 | var assertNotHasOwnProperty = function(name, context) { |
||
191 | if (name === 'hasOwnProperty') { |
||
192 | throw ngMinErr('badname', 'hasOwnProperty is not a valid {0} name', context); |
||
193 | } |
||
194 | }; |
||
195 | |||
196 | assertNotHasOwnProperty(name, 'module'); |
||
197 | if (requires && modules.hasOwnProperty(name)) { |
||
198 | modules[name] = null; |
||
199 | } |
||
200 | return ensure(modules, name, function() { |
||
201 | if (!requires) { |
||
202 | throw $injectorMinErr('nomod', 'Module \'{0}\' is not available! You either misspelled ' + |
||
203 | 'the module name or forgot to load it. If registering a module ensure that you ' + |
||
204 | 'specify the dependencies as the second argument.', name); |
||
205 | } |
||
206 | |||
207 | /** @type {!Array.<Array.<*>>} */ |
||
208 | var invokeQueue = []; |
||
209 | |||
210 | /** @type {!Array.<Function>} */ |
||
211 | var configBlocks = []; |
||
212 | |||
213 | /** @type {!Array.<Function>} */ |
||
214 | var runBlocks = []; |
||
215 | |||
216 | var config = invokeLater('$injector', 'invoke', 'push', configBlocks); |
||
217 | |||
218 | /** @type {angular.Module} */ |
||
219 | var moduleInstance = { |
||
220 | // Private state |
||
221 | _invokeQueue: invokeQueue, |
||
222 | _configBlocks: configBlocks, |
||
223 | _runBlocks: runBlocks, |
||
224 | |||
225 | /** |
||
226 | * @ngdoc method |
||
227 | * @name angular.Module#info |
||
228 | * @module ng |
||
229 | * |
||
230 | * @param {Object=} info Information about the module |
||
231 | * @returns {Object|Module} The current info object for this module if called as a getter, |
||
232 | * or `this` if called as a setter. |
||
233 | * |
||
234 | * @description |
||
235 | * Read and write custom information about this module. |
||
236 | * For example you could put the version of the module in here. |
||
237 | * |
||
238 | * ```js |
||
239 | * angular.module('myModule', []).info({ version: '1.0.0' }); |
||
240 | * ``` |
||
241 | * |
||
242 | * The version could then be read back out by accessing the module elsewhere: |
||
243 | * |
||
244 | * ``` |
||
245 | * var version = angular.module('myModule').info().version; |
||
246 | * ``` |
||
247 | * |
||
248 | * You can also retrieve this information during runtime via the |
||
249 | * {@link $injector#modules `$injector.modules`} property: |
||
250 | * |
||
251 | * ```js |
||
252 | * var version = $injector.modules['myModule'].info().version; |
||
253 | * ``` |
||
254 | */ |
||
255 | info: function(value) { |
||
256 | if (isDefined(value)) { |
||
257 | if (!isObject(value)) throw ngMinErr('aobj', 'Argument \'{0}\' must be an object', 'value'); |
||
258 | info = value; |
||
259 | return this; |
||
260 | } |
||
261 | return info; |
||
262 | }, |
||
263 | |||
264 | /** |
||
265 | * @ngdoc property |
||
266 | * @name angular.Module#requires |
||
267 | * @module ng |
||
268 | * |
||
269 | * @description |
||
270 | * Holds the list of modules which the injector will load before the current module is |
||
271 | * loaded. |
||
272 | */ |
||
273 | requires: requires, |
||
274 | |||
275 | /** |
||
276 | * @ngdoc property |
||
277 | * @name angular.Module#name |
||
278 | * @module ng |
||
279 | * |
||
280 | * @description |
||
281 | * Name of the module. |
||
282 | */ |
||
283 | name: name, |
||
284 | |||
285 | |||
286 | /** |
||
287 | * @ngdoc method |
||
288 | * @name angular.Module#provider |
||
289 | * @module ng |
||
290 | * @param {string} name service name |
||
291 | * @param {Function} providerType Construction function for creating new instance of the |
||
292 | * service. |
||
293 | * @description |
||
294 | * See {@link auto.$provide#provider $provide.provider()}. |
||
295 | */ |
||
296 | provider: invokeLaterAndSetModuleName('$provide', 'provider'), |
||
297 | |||
298 | /** |
||
299 | * @ngdoc method |
||
300 | * @name angular.Module#factory |
||
301 | * @module ng |
||
302 | * @param {string} name service name |
||
303 | * @param {Function} providerFunction Function for creating new instance of the service. |
||
304 | * @description |
||
305 | * See {@link auto.$provide#factory $provide.factory()}. |
||
306 | */ |
||
307 | factory: invokeLaterAndSetModuleName('$provide', 'factory'), |
||
308 | |||
309 | /** |
||
310 | * @ngdoc method |
||
311 | * @name angular.Module#service |
||
312 | * @module ng |
||
313 | * @param {string} name service name |
||
314 | * @param {Function} constructor A constructor function that will be instantiated. |
||
315 | * @description |
||
316 | * See {@link auto.$provide#service $provide.service()}. |
||
317 | */ |
||
318 | service: invokeLaterAndSetModuleName('$provide', 'service'), |
||
319 | |||
320 | /** |
||
321 | * @ngdoc method |
||
322 | * @name angular.Module#value |
||
323 | * @module ng |
||
324 | * @param {string} name service name |
||
325 | * @param {*} object Service instance object. |
||
326 | * @description |
||
327 | * See {@link auto.$provide#value $provide.value()}. |
||
328 | */ |
||
329 | value: invokeLater('$provide', 'value'), |
||
330 | |||
331 | /** |
||
332 | * @ngdoc method |
||
333 | * @name angular.Module#constant |
||
334 | * @module ng |
||
335 | * @param {string} name constant name |
||
336 | * @param {*} object Constant value. |
||
337 | * @description |
||
338 | * Because the constants are fixed, they get applied before other provide methods. |
||
339 | * See {@link auto.$provide#constant $provide.constant()}. |
||
340 | */ |
||
341 | constant: invokeLater('$provide', 'constant', 'unshift'), |
||
342 | |||
343 | /** |
||
344 | * @ngdoc method |
||
345 | * @name angular.Module#decorator |
||
346 | * @module ng |
||
347 | * @param {string} name The name of the service to decorate. |
||
348 | * @param {Function} decorFn This function will be invoked when the service needs to be |
||
349 | * instantiated and should return the decorated service instance. |
||
350 | * @description |
||
351 | * See {@link auto.$provide#decorator $provide.decorator()}. |
||
352 | */ |
||
353 | decorator: invokeLaterAndSetModuleName('$provide', 'decorator', configBlocks), |
||
354 | |||
355 | /** |
||
356 | * @ngdoc method |
||
357 | * @name angular.Module#animation |
||
358 | * @module ng |
||
359 | * @param {string} name animation name |
||
360 | * @param {Function} animationFactory Factory function for creating new instance of an |
||
361 | * animation. |
||
362 | * @description |
||
363 | * |
||
364 | * **NOTE**: animations take effect only if the **ngAnimate** module is loaded. |
||
365 | * |
||
366 | * |
||
367 | * Defines an animation hook that can be later used with |
||
368 | * {@link $animate $animate} service and directives that use this service. |
||
369 | * |
||
370 | * ```js |
||
371 | * module.animation('.animation-name', function($inject1, $inject2) { |
||
372 | * return { |
||
373 | * eventName : function(element, done) { |
||
374 | * //code to run the animation |
||
375 | * //once complete, then run done() |
||
376 | * return function cancellationFunction(element) { |
||
377 | * //code to cancel the animation |
||
378 | * } |
||
379 | * } |
||
380 | * } |
||
381 | * }) |
||
382 | * ``` |
||
383 | * |
||
384 | * See {@link ng.$animateProvider#register $animateProvider.register()} and |
||
385 | * {@link ngAnimate ngAnimate module} for more information. |
||
386 | */ |
||
387 | animation: invokeLaterAndSetModuleName('$animateProvider', 'register'), |
||
388 | |||
389 | /** |
||
390 | * @ngdoc method |
||
391 | * @name angular.Module#filter |
||
392 | * @module ng |
||
393 | * @param {string} name Filter name - this must be a valid angular expression identifier |
||
394 | * @param {Function} filterFactory Factory function for creating new instance of filter. |
||
395 | * @description |
||
396 | * See {@link ng.$filterProvider#register $filterProvider.register()}. |
||
397 | * |
||
398 | * <div class="alert alert-warning"> |
||
399 | * **Note:** Filter names must be valid angular {@link expression} identifiers, such as `uppercase` or `orderBy`. |
||
400 | * Names with special characters, such as hyphens and dots, are not allowed. If you wish to namespace |
||
401 | * your filters, then you can use capitalization (`myappSubsectionFilterx`) or underscores |
||
402 | * (`myapp_subsection_filterx`). |
||
403 | * </div> |
||
404 | */ |
||
405 | filter: invokeLaterAndSetModuleName('$filterProvider', 'register'), |
||
406 | |||
407 | /** |
||
408 | * @ngdoc method |
||
409 | * @name angular.Module#controller |
||
410 | * @module ng |
||
411 | * @param {string|Object} name Controller name, or an object map of controllers where the |
||
412 | * keys are the names and the values are the constructors. |
||
413 | * @param {Function} constructor Controller constructor function. |
||
414 | * @description |
||
415 | * See {@link ng.$controllerProvider#register $controllerProvider.register()}. |
||
416 | */ |
||
417 | controller: invokeLaterAndSetModuleName('$controllerProvider', 'register'), |
||
418 | |||
419 | /** |
||
420 | * @ngdoc method |
||
421 | * @name angular.Module#directive |
||
422 | * @module ng |
||
423 | * @param {string|Object} name Directive name, or an object map of directives where the |
||
424 | * keys are the names and the values are the factories. |
||
425 | * @param {Function} directiveFactory Factory function for creating new instance of |
||
426 | * directives. |
||
427 | * @description |
||
428 | * See {@link ng.$compileProvider#directive $compileProvider.directive()}. |
||
429 | */ |
||
430 | directive: invokeLaterAndSetModuleName('$compileProvider', 'directive'), |
||
431 | |||
432 | /** |
||
433 | * @ngdoc method |
||
434 | * @name angular.Module#component |
||
435 | * @module ng |
||
436 | * @param {string} name Name of the component in camel-case (i.e. myComp which will match as my-comp) |
||
437 | * @param {Object} options Component definition object (a simplified |
||
438 | * {@link ng.$compile#directive-definition-object directive definition object}) |
||
439 | * |
||
440 | * @description |
||
441 | * See {@link ng.$compileProvider#component $compileProvider.component()}. |
||
442 | */ |
||
443 | component: invokeLaterAndSetModuleName('$compileProvider', 'component'), |
||
444 | |||
445 | /** |
||
446 | * @ngdoc method |
||
447 | * @name angular.Module#config |
||
448 | * @module ng |
||
449 | * @param {Function} configFn Execute this function on module load. Useful for service |
||
450 | * configuration. |
||
451 | * @description |
||
452 | * Use this method to register work which needs to be performed on module loading. |
||
453 | * For more about how to configure services, see |
||
454 | * {@link providers#provider-recipe Provider Recipe}. |
||
455 | */ |
||
456 | config: config, |
||
457 | |||
458 | /** |
||
459 | * @ngdoc method |
||
460 | * @name angular.Module#run |
||
461 | * @module ng |
||
462 | * @param {Function} initializationFn Execute this function after injector creation. |
||
463 | * Useful for application initialization. |
||
464 | * @description |
||
465 | * Use this method to register work which should be performed when the injector is done |
||
466 | * loading all modules. |
||
467 | */ |
||
468 | run: function(block) { |
||
469 | runBlocks.push(block); |
||
470 | return this; |
||
471 | } |
||
472 | }; |
||
473 | |||
474 | if (configFn) { |
||
475 | config(configFn); |
||
476 | } |
||
477 | |||
478 | return moduleInstance; |
||
479 | |||
480 | /** |
||
481 | * @param {string} provider |
||
482 | * @param {string} method |
||
483 | * @param {String=} insertMethod |
||
484 | * @returns {angular.Module} |
||
485 | */ |
||
486 | function invokeLater(provider, method, insertMethod, queue) { |
||
487 | if (!queue) queue = invokeQueue; |
||
488 | return function() { |
||
489 | queue[insertMethod || 'push']([provider, method, arguments]); |
||
490 | return moduleInstance; |
||
491 | }; |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * @param {string} provider |
||
496 | * @param {string} method |
||
497 | * @returns {angular.Module} |
||
498 | */ |
||
499 | function invokeLaterAndSetModuleName(provider, method, queue) { |
||
500 | if (!queue) queue = invokeQueue; |
||
501 | return function(recipeName, factoryFunction) { |
||
502 | if (factoryFunction && isFunction(factoryFunction)) factoryFunction.$$moduleName = name; |
||
503 | queue.push([provider, method, arguments]); |
||
504 | return moduleInstance; |
||
505 | }; |
||
506 | } |
||
507 | }); |
||
508 | }; |
||
509 | }); |
||
510 | |||
511 | } |
||
512 | |||
513 | setupModuleLoader(window); |
||
514 | })(window); |
||
515 | |||
516 | /** |
||
517 | * Closure compiler type information |
||
518 | * |
||
519 | * @typedef { { |
||
520 | * requires: !Array.<string>, |
||
521 | * invokeQueue: !Array.<Array.<*>>, |
||
522 | * |
||
523 | * service: function(string, Function):angular.Module, |
||
524 | * factory: function(string, Function):angular.Module, |
||
525 | * value: function(string, *):angular.Module, |
||
526 | * |
||
527 | * filter: function(string, Function):angular.Module, |
||
528 | * |
||
529 | * init: function(Function):angular.Module |
||
530 | * } } |
||
531 | */ |
||
532 | angular.Module; |
||
|
|||
533 | |||
534 |