Passed
Push — master ( 61dd3f...482d1b )
by greg
01:51
created

sourceAttr.js ➔ sourceAttr   C

Complexity

Conditions 11
Paths 126

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
c 1
b 0
f 0
nc 126
dl 0
loc 61
rs 5.7758
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
C sourceAttr.js ➔ ... ➔ ??? 0 30 12

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like sourceAttr.js ➔ sourceAttr 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
2
import {Sql} from '../../../'
3
4
export default function sourceAttr(val, params) {
5
  var hiddenVal = val
6
  var selected = ''
7
8
  if(typeof hiddenVal === 'object' && Object.prototype.toString.call(hiddenVal) === '[object Object]') {
9
    hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, ''')
10
11
    try {
12
      var displayVal = eval('val.' + params.display)
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
13
      if(typeof params.display !== 'undefined' && params.display !== null
14
        && typeof displayVal !== 'undefined' && displayVal !== null) {
15
        val = displayVal
16
      }else {
17
        val = val[Object.keys(val)[0]]
18
      }
19
    }catch(e) {
20
      val = val[Object.keys(val)[0]]
21
    }
22
  }
23
24
  if(typeof params.value === 'object' && Object.prototype.toString.call(params.value) === '[object Array]') {
25
    Array.prototype.forEach.call(params.value, (v) => {
26
      var item = v
0 ignored issues
show
Unused Code introduced by
The assignment to variable item seems to be never used. Consider removing it.
Loading history...
27
      try {
28
        var displayV = eval('item.' + params.display)
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
29
        if(typeof params.display !== 'undefined' && params.display !== null
30
          && typeof displayV !== 'undefined' && displayV !== null) {
31
          item = displayV
32
        }else {
33
          if(typeof v === 'string') {
34
            item = v
35
          }else {
36
            item = v[Object.keys(v)[0]]
37
          }
38
        }
39
      }catch(e) {
40
        item = v[Object.keys(v)[0]]
41
      }
42
      
43
      if(typeof val === 'object' && Object.prototype.toString.call(val) === '[object Array]'
44
        && typeof item === 'object' && Object.prototype.toString.call(item) === '[object Array]') {
45
        
46
        Array.prototype.forEach.call(item, (i) => {
47
          if(val.includes(i)) {
48
            selected = 'selected="selected"'
49
          }
50
        })
51
      }else if(val === item) {
52
        selected = 'selected="selected"'
53
      }
54
    })
55
  }else if(params.value === hiddenVal) {
56
    selected = 'selected="selected"'
57
  }
58
59
  return {
60
    hiddenVal: hiddenVal,
61
    selected: selected,
62
    val: val
63
  }
64
}
65