Completed
Push — master ( f48291...a65f2c )
by greg
74:59
created

src/cli/cms/editor/handlebars/sourceAttr.js   A

Complexity

Total Complexity 21
Complexity/F 7

Size

Lines of Code 59
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 21
c 2
b 0
f 0
nc 1
mnd 4
bc 19
fnc 3
dl 0
loc 59
rs 10
bpm 6.3333
cpm 7
noi 3
1
2
export default function sourceAttr(val, params) {
3
  var hiddenVal = val
4
  var selected = ''
5
6
  if(typeof hiddenVal === 'object' && Object.prototype.toString.call(hiddenVal) === '[object Object]') {
7
    hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, ''')
8
9
    try {
10
      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...
11
      if(params.display != null&& displayVal != null) {
12
        val = displayVal
13
      }else {
14
        val = val[Object.keys(val)[0]]
15
      }
16
    }catch(e) {
17
      val = val[Object.keys(val)[0]]
18
    }
19
  }
20
21
  if(typeof params.value === 'object' && Object.prototype.toString.call(params.value) === '[object Array]') {
22
    Array.prototype.forEach.call(params.value, (v) => {
23
      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...
24
      try {
25
        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...
26
        if(params.display != null && displayV !== null) {
27
          item = displayV
28
        } else {
29
          if(typeof v === 'string') {
30
            item = v
31
          } else {
32
            item = v[Object.keys(v)[0]]
33
          }
34
        }
35
      } catch(e) {
36
        item = v[Object.keys(v)[0]]
37
      }
38
      
39
      if(typeof val === 'object' && Object.prototype.toString.call(val) === '[object Array]'
40
        && typeof item === 'object' && Object.prototype.toString.call(item) === '[object Array]') {
41
        
42
        Array.prototype.forEach.call(item, (i) => {
43
          if(val.includes(i)) {
44
            selected = 'selected="selected"'
45
          }
46
        })
47
      }else if(val === item) {
48
        selected = 'selected="selected"'
49
      }
50
    })
51
  }else if(params.value === hiddenVal) {
52
    selected = 'selected="selected"'
53
  }
54
55
  return {
56
    hiddenVal: hiddenVal,
57
    selected: selected,
58
    val: val
59
  }
60
}
61