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

Match.check   F

Complexity

Conditions 14

Size

Total Lines 41
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 41
rs 3.6
c 0
b 0
f 0
cc 14

How to fix   Complexity   

Complexity

Complex classes like Match.check 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
class Match {
5
    constructor(find, operator) {
6
        this.find = find;
7
        this.operator = operator || null;
8
    }
9
10
    check(value) {
11
        let returnValue = false;
12
13
        if (Array.isArray(this.find)) {
14
            if (
15
                this.find.indexOf(value) < 0 &&
16
                (this.operator == true ||
0 ignored issues
show
Best Practice introduced by
Comparing this.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...
17
                    this.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...
18
                    this.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...
19
            ) {
20
                returnValue = true;
21
            } else if (this.find.indexOf(value) >= 0 && !this.operator) {
22
                returnValue = true;
23
            } else if (value > max(this.find) && this.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...
24
                returnValue = true;
25
            } else if (value >= max(this.find) && this.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...
26
                returnValue = true;
27
            } else if (value < min(this.find) && this.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...
28
                returnValue = true;
29
            } else if (value <= min(this.find) && this.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...
30
                returnValue = true;
31
            }
32
        } else if (
33
            value != this.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...
34
            (this.operator == true || this.operator == '!=' || this.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...
introduced by
Replace ·this.operator·==·'!='·|| with ...
Loading history...
35
        ) {
36
            returnValue = true;
37
        } else if (value == this.find && !this.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...
38
            returnValue = true;
39
        } else if (value > this.find && this.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...
40
            returnValue = true;
41
        } else if (value >= this.find && this.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...
42
            returnValue = true;
43
        } else if (value < this.find && this.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...
44
            returnValue = true;
45
        } else if (value <= this.find && this.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...
46
            returnValue = true;
47
        }
48
49
        return returnValue;
50
    }
51
52
    static create(original, key, find, operator) {
53
        const matcher = new Match(find, operator);
54
55
        return original.filter((item) => {
56
            let values = item[key];
57
    
0 ignored issues
show
introduced by
Delete ····
Loading history...
58
            if (!values) {
59
                return false;
60
            }
61
    
0 ignored issues
show
introduced by
Delete ····
Loading history...
62
            values = values.toString().split(',');
63
    
0 ignored issues
show
introduced by
Delete ····
Loading history...
64
            return values.some(matcher.check.bind(matcher));
65
        });
66
    }
67
}
68
69
module.exports = function multifilter(original, key, find, operator) {
70
    return Match.create(original, key, find, operator);
71
};
72