Completed
Pull Request — master (#104)
by greg
02:00
created

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

Complexity

Total Complexity 19
Complexity/F 2.38

Size

Lines of Code 99
Function Count 8

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 99
rs 10
wmc 19
mnd 2
bc 16
fnc 8
bpm 2
cpm 2.375
noi 0

4 Functions

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