Completed
Push — master ( 991ce7...64aac0 )
by greg
01:57
created

compileAbe.js ➔ compileAbe   C

Complexity

Conditions 19
Paths 25

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 19
c 2
b 0
f 0
nc 25
dl 0
loc 49
rs 5.098
nop 0

How to fix   Complexity   

Complexity

Complex classes like compileAbe.js ➔ compileAbe often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import Handlebars from 'handlebars'
2
import abeEngine from './abeEngine'
3
import xss from 'xss'
4
5
import {
6
  config
7
} from '../../../'
8
9
/**
10
 * Abe handlebar helper, that retrieve text to add to handlebars templating engine
11
 * @return {String} the string to replace {{ handlebars_key }}
12
 */
13
export default function compileAbe(){
14
  var content = abeEngine.instance.content
15
  if(typeof arguments[0].hash['key'] === 'undefined' || arguments[0].hash['key'] === null) return ''
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
16
  var key
17
  var hash
18
  var value
19
  var testXSS
20
  
21
  if(arguments[0].hash['key'].indexOf('}-') > 0){
22
    key = arguments[0].hash['key'].split('-')
23
    key = key[key.length - 1]
24
    hash = arguments[0].hash
25
    hash.key = hash.key.replace(/\{\{@index\}\}/, '[{{@index}}]')
26
    try{
27
      value = content ? content[hash['dictionnary']][arguments[0].data.index][key] : hash.key
28
    }
29
    catch(e){
30
      value = ''
31
    }
32
    if(typeof value === 'undefined' || typeof value === 'function' || value === null) {
33
      value = ''
34
    }
35
    if(typeof hash.type !== 'undefined' && hash.type !== null && hash.type === 'rich'){
36
      testXSS = xss(value.replace(/"/g, '"'), {
37
        'whiteList': config.htmlWhiteList,
38
        stripIgnoreTag: true
39
      })
40
      return new Handlebars.SafeString(testXSS)
41
    }
42
    return value.replace(/%27/, '\'')
43
  }
44
45
  key = arguments[0].hash['key'].replace('.', '-')
46
47
  hash = arguments[0].hash
48
  value = ((content) ? content[hash.key.replace('.', '-')] : hash.key)
49
  if(typeof value === 'undefined' || typeof value === 'function' || value === null) {
50
    value = ''
51
  }
52
  
53
  if(typeof hash.type !== 'undefined' && hash.type !== null && hash.type === 'rich'){
54
    testXSS = xss(value.replace(/"/g, '"'), {
55
      'whiteList': config.htmlWhiteList,
56
      stripIgnoreTag: true
57
    })
58
    return new Handlebars.SafeString(testXSS)
59
  }
60
  return value.replace(/%27/, '\'')
61
}
62