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

Match   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 26
dl 0
loc 44
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A constructor 0 4 1
A check 0 7 2
A checkOperators 0 13 1
A create 0 15 3
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
        if (Array.isArray(this.find)) {
12
            return this.checkOperators(value, this.find.indexOf(value) < 0);
13
        }
14
15
        return this.checkOperators(value, 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...
16
    }
17
18
    checkOperators(value, find) {
19
        const maxValue = max(this.find);
20
        const minValue = min(this.find);
21
22
        const notOperator = find && (this.operator === '!=' || this.operator === '<>');
0 ignored issues
show
introduced by
Insert ⏎···········
Loading history...
23
        const noInput = !find && !this.operator;
24
        const gtOperator = ((value > maxValue && maxValue !== null) || value > this.find) && this.operator === '>';
0 ignored issues
show
introduced by
Replace ·((value·>·maxValue·...alue·>·this.find)·&& with ...
Loading history...
25
        const gltOperator = ((value >= maxValue && maxValue !== null) || value >= this.find) && this.operator === '>=';
0 ignored issues
show
introduced by
Replace ...lue·>=·this.find)·&& with ...
Loading history...
26
        const ltOperator = ((value < minValue && minValue !== null) || value < this.find) && this.operator === '<';
0 ignored issues
show
introduced by
Replace ·((value·<·minValue·...alue·<·this.find)·&& with ...
Loading history...
27
        const lltOperator = ((value <= minValue && minValue !== null) || value <= this.find) && this.operator === '<=';
0 ignored issues
show
introduced by
Replace ...lue·<=·this.find)·&& with ...
Loading history...
28
29
        return notOperator || noInput || gtOperator || gltOperator || ltOperator || lltOperator;
0 ignored issues
show
introduced by
Replace notOperator·||·noInput...erator·||·lltOperator with (⏎··········...tor⏎········)
Loading history...
30
    }
31
32
    static create(original, key, find, operator) {
33
        const matcher = new Match(find, operator);
34
35
        return original.filter((item) => {
36
            let values = item[key];
37
38
            if (!values) {
39
                return false;
40
            }
41
42
            values = values.toString().split(',');
43
44
            return values.some(matcher.check.bind(matcher));
45
        });
46
    }
47
}
48
49
module.exports = function multifilter(original, key, find, operator) {
50
    return Match.create(original, key, find, operator);
51
};
52