Conditions | 17 |
Total Lines | 50 |
Code Lines | 40 |
Lines | 18 |
Ratio | 36 % |
Changes | 0 |
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'); |
||
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 == '<>') |
||
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 == '>') { |
|
25 | returnValue = true; |
||
26 | } else if (value >= max(find) && operator == '>=') { |
||
27 | returnValue = true; |
||
28 | } else if (value < min(find) && operator == '<') { |
||
29 | returnValue = true; |
||
30 | } else if (value <= min(find) && operator == '<=') { |
||
31 | returnValue = true; |
||
32 | } |
||
33 | } else if ( |
||
34 | value != find && |
||
35 | (operator == true || operator == '!=' || operator == '<>') |
||
36 | ) { |
||
37 | returnValue = true; |
||
38 | } else if (value == find && !operator) { |
||
39 | returnValue = true; |
||
40 | View Code Duplication | } else if (value > find && operator == '>') { |
|
41 | returnValue = true; |
||
42 | } else if (value >= find && operator == '>=') { |
||
43 | returnValue = true; |
||
44 | } else if (value < find && operator == '<') { |
||
45 | returnValue = true; |
||
46 | } else if (value <= find && operator == '<=') { |
||
47 | returnValue = true; |
||
48 | } |
||
49 | }); |
||
50 | |||
51 | return returnValue; |
||
52 | }); |
||
53 | }; |
||
54 |