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 || |
|
|
|
|
17
|
|
|
this.operator == '!=' || |
|
|
|
|
18
|
|
|
this.operator == '<>') |
|
|
|
|
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 == '>') { |
|
|
|
|
24
|
|
|
returnValue = true; |
25
|
|
|
} else if (value >= max(this.find) && this.operator == '>=') { |
|
|
|
|
26
|
|
|
returnValue = true; |
27
|
|
|
} else if (value < min(this.find) && this.operator == '<') { |
|
|
|
|
28
|
|
|
returnValue = true; |
29
|
|
|
} else if (value <= min(this.find) && this.operator == '<=') { |
|
|
|
|
30
|
|
|
returnValue = true; |
31
|
|
|
} |
32
|
|
|
} else if ( |
33
|
|
|
value != this.find && |
|
|
|
|
34
|
|
|
(this.operator == true || this.operator == '!=' || this.operator == '<>') |
|
|
|
|
35
|
|
|
) { |
36
|
|
|
returnValue = true; |
37
|
|
|
} else if (value == this.find && !this.operator) { |
|
|
|
|
38
|
|
|
returnValue = true; |
39
|
|
|
} else if (value > this.find && this.operator == '>') { |
|
|
|
|
40
|
|
|
returnValue = true; |
41
|
|
|
} else if (value >= this.find && this.operator == '>=') { |
|
|
|
|
42
|
|
|
returnValue = true; |
43
|
|
|
} else if (value < this.find && this.operator == '<') { |
|
|
|
|
44
|
|
|
returnValue = true; |
45
|
|
|
} else if (value <= this.find && this.operator == '<=') { |
|
|
|
|
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
|
|
|
|
|
|
|
|
58
|
|
|
if (!values) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
values = values.toString().split(','); |
63
|
|
|
|
|
|
|
|
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
|
|
|
|