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

sourceAttr.js ➔ getKeys   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 1
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
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
  if (params.value === displayName) {
33
    selected = 'selected'
34
  }
35
36
  return {
37
    hiddenVal: JSON.stringify(obj).replace(/\'/g, '&quote;'),
38
    selected: selected,
39
    val: displayName
40
  }
41
}
42
43
/**
44
 * return the value from a json obj of a nested attribute
45
 * @param  {Object} obj  the json object
46
 * @param  {string} path the path to object (dot notation)
47
 * @return {[type]}      the object containing the path object or undefined
48
 */
49
export function get(obj, path) {
50
  if (path == null) {
51
    return obj
52
  }
53
  return path.split('.').reduce(function(prev, curr) {
54
    return prev ? prev[curr] : undefined
55
  }, obj || this)
56
}
57
58
/**
59
 * replace the variables in str by values from obj
60
 * corresponding to keys
61
 * @param  {Object} obj    the json object
62
 * @param  {string} str    the string
63
 * @return {string}        the string with values
64
 */
65
export function prepareDisplay(obj, str = null) {
66
  var keys = getKeys(str)
67
  Array.prototype.forEach.call(keys, (key) => {
68
    var val = get(obj, key)
69
    var pattern = new RegExp('{{'+key+'}}|'+key)
70
    str = str.replace(pattern, val)
71
  })
72
73
  if (str == null) {
74
    str = obj
75
  }
76
77
  return str
78
}
79
80
/**
81
 * return array of variables {{variable}} extracted from str
82
 * @param  {string} str the string containing variables
83
 * @return {Array}     the array of variables
84
 */
85
export function getKeys(str){
86
  var regex = /\{\{(.*?)\}\}/g
87
  var variables = []
88
  var match
89
90
  while ((match = regex.exec(str)) !== null) {
91
    if (match[1] != null) {
92
      variables.push(match[1])
93
    }
94
  }
95
  
96
  if (variables.length == 0 && str != null) {
97
    variables.push(str)
98
  }
99
100
  return variables
101
}
102