Passed
Pull Request — master (#30)
by Pieter Epeüs
01:35
created

multifilter.js ➔ multifilter   F

Complexity

Conditions 17

Size

Total Lines 50
Code Lines 40

Duplication

Lines 18
Ratio 36 %

Importance

Changes 0
Metric Value
eloc 40
dl 18
loc 50
rs 1.8
c 0
b 0
f 0
cc 17

How to fix   Complexity   

Complexity

Complex classes like multifilter.js ➔ multifilter 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
const min = require('./min.js');
2
const max = require('./max.js');
3
4
module.exports = function multifilter(original, key, find, operator) {
5
    return original.filter((item) => {
6
        let values = item[key];
7
        let returnValue = false;
8
9
        if (!values) {
10
            return returnValue;
11
        }
12
13
        values = values.toString().split(',');
14
15
        values.forEach((value) => {
16
            if (Array.isArray(find)) {
17
                if (
18
                    find.indexOf(value) < 0 &&
19
                    (operator == true || operator == '!=' || operator == '<>')
0 ignored issues
show
Best Practice introduced by
Comparing operator to true using the == operator is not safe. Consider using === instead.
Loading history...
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
20
                ) {
21
                    returnValue = true;
22
                } else if (find.indexOf(value) >= 0 && !operator) {
23
                    returnValue = true;
24 View Code Duplication
                } else if (value > max(find) && operator == '>') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
25
                    returnValue = true;
26
                } else if (value >= max(find) && operator == '>=') {
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
27
                    returnValue = true;
28
                } else if (value < min(find) && operator == '<') {
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
29
                    returnValue = true;
30
                } else if (value <= min(find) && operator == '<=') {
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
31
                    returnValue = true;
32
                }
33
            } else if (
34
                value != find &&
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison !== instead of !=.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
35
                (operator == true || operator == '!=' || operator == '<>')
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
36
            ) {
37
                returnValue = true;
38
            } else if (value == find && !operator) {
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
39
                returnValue = true;
40 View Code Duplication
            } else if (value > find && operator == '>') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
41
                returnValue = true;
42
            } else if (value >= find && operator == '>=') {
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
43
                returnValue = true;
44
            } else if (value < find && operator == '<') {
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
45
                returnValue = true;
46
            } else if (value <= find && operator == '<=') {
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
47
                returnValue = true;
48
            }
49
        });
50
51
        return returnValue;
52
    });
53
};
54