handlebars.registerHelper(ꞌifCondꞌ)   F
last analyzed

Complexity

Conditions 16
Paths 12

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 16

Importance

Changes 0
Metric Value
cc 16
eloc 25
nc 12
dl 0
loc 29
ccs 17
cts 17
cp 1
crap 16
rs 2.4
c 0
b 0
f 0
nop 1

How to fix   Complexity   

Complexity

Complex classes like handlebars.registerHelper(ꞌifCondꞌ) 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 1
var handlebars = require('handlebars');
2
/**
3
 * Aufruf in templates per
4
 * 
5
 * {{#ifCond value="1" comp="2" mode='lt'}}
6
 *     "1" ist kleiner "2"
7
 * {{else}}
8
 *     "1" ist nicht kleiner "2"
9
 * {{/ifCond}}
10
 * 
11
 * mögliche "mode"s:
12
 * - eq   ==
13
 * - lt   <
14
 * - gt   >
15
 * - lte  <=
16
 * - gte  >=
17
 * - neq  !=
18
 * 
19
 */
20 1
handlebars.registerHelper('ifCond', function(options) {
21
	var 
22 36
	    modes = ['eq', 'lt', 'gt', 'lte', 'gte', 'neq'],
23 36
	    mode = ( (typeof options.hash.mode != 'undefined') && (modes.indexOf(options.hash.mode) != -1) ) ? options.hash.mode : 'eq'
24
	;
25
	
26 36
	switch (mode) {
27
		case 'lt' :
28 6
		    if (options.hash.value < options.hash.comp) return options.fn(this);
0 ignored issues
show
Coding Style Best Practice introduced by Björn Bartels
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...
29 4
		break;
30
		case 'gt' :
31 6
		    if (options.hash.value > options.hash.comp) return options.fn(this);
0 ignored issues
show
Coding Style Best Practice introduced by Björn Bartels
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...
32 4
		break;
33
		case 'lte' :
34 6
		    if (options.hash.value <= options.hash.comp) return options.fn(this);
0 ignored issues
show
Coding Style Best Practice introduced by Björn Bartels
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...
35 2
		break;
36
		case 'gte' :
37 6
		    if (options.hash.value >= options.hash.comp) return options.fn(this);
0 ignored issues
show
Coding Style Best Practice introduced by Björn Bartels
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...
38 2
		break;
39
		case 'neq' :
40 6
		    if (options.hash.value != options.hash.comp) return options.fn(this);
0 ignored issues
show
Coding Style Best Practice introduced by Björn Bartels
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...
41 2
		break;
42
		case 'eq' :
43
		default :
44 6
		    if (options.hash.value == options.hash.comp) return options.fn(this);
0 ignored issues
show
Coding Style Best Practice introduced by Björn Bartels
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...
45 4
		break;
46
	}
47 18
    return options.inverse(this);
48
});
49