Total Complexity | 8 |
Complexity/F | 4 |
Lines of Code | 33 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | /* eslint-disable censor/no-swear */ |
||
2 | export class InclusiveFilter { |
||
3 | constructor({ |
||
4 | include, |
||
5 | exclude, |
||
6 | pass = true, // filter is empty |
||
7 | conflict = true, // a value is both included and excluded |
||
8 | neither = false// a value is neither included nor excluded |
||
9 | }) { |
||
10 | this._include = include; |
||
11 | this._exclude = exclude; |
||
12 | |||
13 | this._conflict = conflict; |
||
14 | this._neither = neither; |
||
15 | this._pass = pass; |
||
16 | } |
||
17 | |||
18 | run(value) { |
||
19 | if (!this._include && !this._exclude) return this._pass; |
||
|
|||
20 | |||
21 | if (!this._include) return !this._exclude.includes(value); |
||
22 | if (!this._exclude) return this._include.includes(value); |
||
23 | |||
24 | const isIncluded = this._include.includes(value); |
||
25 | const isExcluded = this._exclude.includes(value); |
||
26 | |||
27 | if (isIncluded && !isExcluded) return true; |
||
28 | if (isExcluded && !isIncluded) return false; |
||
29 | |||
30 | if (isIncluded && isExcluded) return this._conflict; |
||
31 | |||
32 | return this._neither; // !isIncluded && !isExcluded |
||
33 | } |
||
34 | } |
||
35 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.