Passed
Push — master ( 8d78d8...331be9 )
by Eric
45s
created

dist/toObject.js   A

Complexity

Total Complexity 22
Complexity/F 2.75

Size

Lines of Code 72
Function Count 8

Duplication

Duplicated Lines 72
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 0
c 1
b 0
f 1
nc 32
dl 72
loc 72
rs 10
wmc 22
mnd 5
bc 18
fnc 8
bpm 2.25
cpm 2.75
noi 3

How to fix   Duplicated Code   

Duplicated Code

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:

1 View Code Duplication
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.toObject = f()}})(function(){var define,module,exports;module={exports:(exports={})};
4 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Unused Code introduced by
The variable define seems to be never used. Consider removing it.
Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
/**
3
 * Turns an Array into an associative Object (while keeping reference!)
4
 *
5
 * @param {Array}                 arr    Array of Objects to turn into an
6
 *                                       associative Object
7
 * @param {String|Array|Function} mapBy  optional mapping key, can be a
8
 *                                       simple string (property name in
9
 *                                       the abjects of arr), a list of
10
 *                                       property names (which are
11
 *                                       combined) or a function which
12
 *                                       returns a unique id to use
13
 *
14
 * @throws {TypeError} if arr is not an Array or mapBy is set but not of
15
 *                     correct type (String, Array, Function)
16
 *
17
 * @returns {Object}
18
 */
19
module.exports = function(arr, mapBy) {
20
  var obj = {};
21
22
  if (!Array.isArray(arr)) {
23
    throw new TypeError('arr argument is not of type Array');
24
  }
25
26
  if (mapBy !== undefined
27
    && typeof mapBy !== 'string'
28
    && !Array.isArray(mapBy)
29
    && typeof mapBy !== 'function'
30
  ) {
31
    throw new TypeError(
32
      'mapBy argument is not of type {String|Array|Function}'
33
    );
34
  }
35
36
  var methods = {
37
    string: function(val) {
38
      this.undefined(val, val[mapBy]);
39
    },
40
    object: function(val) {
41
      var newKey = mapBy.map(function(propertyName){
42
        return val[propertyName];
43
      }).join('_');
44
45
      this.undefined(val, newKey);
46
    },
47
    function: function(val, i, arr) {
48
      this.undefined(val, mapBy(val, i, arr));
49
    },
50
    undefined: function(val, newKey) {
51
      if (typeof newKey === 'string'
52
        || typeof newKey === 'number'
53
      ) {
54
        obj[newKey] = val;
55
      }
56
    }
57
  };
58
59
  /**
60
   * run the designated method by mapBy type from the methods object
61
   * it binds the methods object so we can use the undefined setter method
62
   * for different mapBy types and don't have to maintain multiple but
63
   * same conditions
64
   */
65
  arr.forEach(
66
    methods[(typeof mapBy)].bind(methods)
67
  );
68
69
  return obj;
70
};
71
72
return module.exports;});
73