Completed
Push — master ( 3b3ae1...61dd3f )
by greg
01:50
created

src/cli/cms/data/abe-remove-duplicate-attr.js   A

Complexity

Total Complexity 14
Complexity/F 2.8

Size

Lines of Code 46
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 46
rs 10
noi 2
wmc 14
mnd 2
bc 11
fnc 5
bpm 2.2
cpm 2.8

2 Functions

Rating   Name   Duplication   Size   Complexity  
A abe-remove-duplicate-attr.js ➔ recurseDeleteKey 0 22 2
A abe-remove-duplicate-attr.js ➔ removeDuplicateAttr 0 19 3
1
import {
2
  getAttr
3
} from '../../'
4
5
function recurseDeleteKey(currentLevel, arrayKeyAttr) {
6
  var currentArray = arrayKeyAttr.slice(0)
7
8
  if (arrayKeyAttr.length === 1) {
9
    delete currentLevel[arrayKeyAttr[0]]
10
  }
11
12
  Array.prototype.forEach.call(currentArray, (key) => {
13
    if(typeof currentLevel[key] !== 'undefined' && currentLevel[key] !== null) {
14
      currentLevel = currentLevel[key]
15
      currentArray.shift()
16
      recurseDeleteKey(currentLevel, currentArray)
17
      if(typeof currentLevel === 'object' && Object.prototype.toString.call(currentLevel) === '[object Array]') {
18
        Array.prototype.forEach.call(currentLevel, (item) => {
19
          recurseDeleteKey(item, currentArray)
20
        })
21
      }else {
22
        recurseDeleteKey(currentLevel, currentArray)
23
      }
24
    }
25
  })
26
}
27
28
export default function removeDuplicateAttr(text, json) {
29
  var regAbe = /{{abe[\S\s].*?duplicate=['|"]([\S\s].*?['|"| ]}})/g
30
  var matches = text.match(regAbe)
31
  var requiredValue = 0
0 ignored issues
show
Unused Code introduced by
The variable requiredValue seems to be never used. Consider removing it.
Loading history...
32
  var complete = 0
0 ignored issues
show
Unused Code introduced by
The variable complete seems to be never used. Consider removing it.
Loading history...
33
  if(typeof matches !== 'undefined' && matches !== null){
34
35
    Array.prototype.forEach.call(matches, (match) => {
36
      var keyAttr = getAttr(match, 'key')
37
38
      if(typeof match !== 'undefined' && match !== null) {
39
        var arrayKeyAttr = keyAttr.split('.')
40
        recurseDeleteKey(json, arrayKeyAttr)
41
      }
42
    })
43
  }
44
45
  return json
46
}