1
1
import R from 'ramda' ;
2
+ import isNumeric from 'fast-isnumeric' ;
3
+
4
+ const FILTER_TERMS = {
5
+ '<' : ( a , b ) => a < b ,
6
+ '<=' : ( a , b ) => a <= b ,
7
+ '>' : ( a , b ) => a > b ,
8
+ '>=' : ( a , b ) => a >= b
9
+ }
2
10
3
11
export default function filterRows ( filters , rows ) {
4
12
return rows . filter ( r => {
@@ -8,17 +16,31 @@ export default function filterRows(filters, rows) {
8
16
const colFilter = filters [ columnKey ] ;
9
17
const rowValue = r [ columnKey ] ;
10
18
11
- /*
12
- * TODO:
13
- * - Options for lowercase
14
- * - Options for strict equality
15
- * - Numeric options: <5
16
- * - and / or: (<5, ==3) && NYC
17
- */
18
- include = include && R . contains (
19
- R . toLower ( colFilter . filterTerm + '' ) ,
20
- R . toLower ( rowValue + '' )
21
- ) ;
19
+ let matched = false ;
20
+ const strFilterTerm = colFilter . filterTerm + ''
21
+ R . keys ( FILTER_TERMS ) . forEach ( k => {
22
+ const replacedFilterTerm = R . replace ( k , '' , strFilterTerm ) ;
23
+ if ( ! matched &&
24
+ R . contains ( k , strFilterTerm ) &&
25
+ isNumeric ( replacedFilterTerm ) &&
26
+ isNumeric ( rowValue ) &&
27
+ strFilterTerm !== k
28
+ ) {
29
+ include = include && FILTER_TERMS [ k ] (
30
+ parseFloat ( rowValue , 10 ) ,
31
+ parseFloat ( replacedFilterTerm , 10 )
32
+ ) ;
33
+ matched = true ;
34
+ }
35
+ } ) ;
36
+ if ( ! matched &&
37
+ ! R . contains ( strFilterTerm , R . keys ( FILTER_TERMS ) )
38
+ ) {
39
+ include = include && R . contains (
40
+ R . toLower ( strFilterTerm ) ,
41
+ R . toLower ( rowValue + '' )
42
+ ) ;
43
+ }
22
44
} ) ;
23
45
24
46
return include ;
0 commit comments