Completed
Pull Request — master (#58)
by
unknown
02:28
created

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

Complexity

Total Complexity 28
Complexity/F 2.55

Size

Lines of Code 105
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 0
c 1
b 1
f 0
nc 1
dl 0
loc 105
rs 10
wmc 28
mnd 3
bc 23
fnc 11
bpm 2.0909
cpm 2.5454
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A sourceAttr.js ➔ prepareDisplay 0 14 2
A sourceAttr.js ➔ get 0 8 2
B sourceAttr.js ➔ getKeys 0 17 5
A sourceAttr.js ➔ sourceAttr 0 18 2
1
export function isSelected(currentValue, values) {
2
  var isEqual = false
3
  if(typeof currentValue === 'object' && Object.prototype.toString.call(currentValue) === '[object Object]') {
4
    Array.prototype.forEach.call(values, (value) => {
5
      if (value != null) {
6
        var checkAllEqual = false
7
        Array.prototype.forEach.call(Object.keys(value), (key) => {
8
          if (currentValue[key] != null && currentValue[key] == value[key] && checkAllEqual == false) {
9
            checkAllEqual = true
10
          }
11
        })
12
        if (checkAllEqual) {
13
          isEqual = true
14
        }
15
      }
16
    })
17
  }else {
18
    Array.prototype.forEach.call(values, (value) => {
19
      if (currentValue == value) {
20
        isEqual = true
21
      }
22
    })
23
  }
24
25
  return isEqual
26
}
27
28
export default function sourceAttr(obj, params) {
29
  var str = params.display
30
  var selected = ''
31
  var displayName = prepareDisplay(obj, str)
32
  
33
  Array.prototype.forEach.call(params.value, (pValue) => {
34
    var pDisplay = prepareDisplay(pValue, str)
35
    if (pDisplay === displayName) {
36
      selected = 'selected'
37
    }
38
  })
39
  
40
  return {
41
    hiddenVal: (typeof obj == 'object') ? JSON.stringify(obj).replace(/\'/g, '&quote;') : obj,
42
    selected: selected,
43
    val: displayName
44
  }
45
}
46
47
/**
48
 * return the value from a json obj of a nested attribute
49
 * @param  {Object} obj  the json object
50
 * @param  {string} path the path to object (dot notation)
51
 * @return {[type]}      the object containing the path object or undefined
52
 */
53
export function get(obj, path) {
54
  if (path == null) {
55
    return obj
56
  }
57
  return path.split('.').reduce(function(prev, curr) {
58
    return prev ? prev[curr] : undefined
59
  }, obj || this)
60
}
61
62
/**
63
 * replace the variables in str by values from obj
64
 * corresponding to keys
65
 * @param  {Object} obj    the json object
66
 * @param  {string} str    the string
67
 * @return {string}        the string with values
68
 */
69
export function prepareDisplay(obj, str = null) {
70
  var keys = getKeys(str)
71
  Array.prototype.forEach.call(keys, (key) => {
72
    var val = get(obj, key)
73
    var pattern = new RegExp('{{'+key+'}}|'+key)
74
    str = str.replace(pattern, val)
75
  })
76
77
  if (str == null) {
78
    str = obj
79
  }
80
81
  return str
82
}
83
84
/**
85
 * return array of variables {{variable}} extracted from str
86
 * @param  {string} str the string containing variables
87
 * @return {Array}     the array of variables
88
 */
89
export function getKeys(str){
90
  var regex = /\{\{(.*?)\}\}/g
91
  var variables = []
92
  var match
93
94
  while ((match = regex.exec(str)) !== null) {
95
    if (match[1] != null) {
96
      variables.push(match[1])
97
    }
98
  }
99
  
100
  if (variables.length == 0 && str != null) {
101
    variables.push(str)
102
  }
103
104
  return variables
105
}
106