11
11
var Lib = require ( '../lib' ) ;
12
12
var axisIds = require ( '../plots/cartesian/axis_ids' ) ;
13
13
14
+ var INEQUALITY_OPS = [ '=' , '<' , '>=' , '>' , '<=' ] ;
15
+ var INTERVAL_OPS = [ '[]' , '()' , '[)' , '(]' , '][' , ')(' , '](' , ')[' ] ;
16
+ var SET_OPS = [ '{}' , '}{' ] ;
17
+
14
18
exports . moduleType = 'transform' ;
15
19
16
20
exports . name = 'filter' ;
@@ -33,10 +37,31 @@ exports.attributes = {
33
37
} ,
34
38
operation : {
35
39
valType : 'enumerated' ,
36
- values : [ '=' , '<' , '>' , 'within' , 'notwithin' , 'in' , 'notin' ] ,
40
+ values : [ ] . concat ( INEQUALITY_OPS ) . concat ( INTERVAL_OPS ) . concat ( SET_OPS ) ,
37
41
dflt : '=' ,
38
42
description : [
39
- 'Sets the filter operation.'
43
+ 'Sets the filter operation.' ,
44
+
45
+ '*=* filters items equal to `value`' ,
46
+
47
+ '*<* filters items less than `value`' ,
48
+ '*<=* filters items less than or equal to `value`' ,
49
+
50
+ '*>* filters items greater than `value`' ,
51
+ '*>=* filters items greater than or equal to `value`' ,
52
+
53
+ '*[]* filters items inside `value[0]` to value[1]` including both bounds`' ,
54
+ '*()* filters items inside `value[0]` to value[1]` excluding both bounds`' ,
55
+ '*[)* filters items inside `value[0]` to value[1]` including `value[0]` but excluding `value[1]' ,
56
+ '*(]* filters items inside `value[0]` to value[1]` including both bounds`' ,
57
+
58
+ '*][* filters items outside `value[0]` to value[1]` and not equal to both bounds`' ,
59
+ '*)(* filters items outside `value[0]` to value[1]`' ,
60
+ '*](* filters items outside `value[0]` to value[1]` and not equal to `value[0]`' ,
61
+ '*)[* filters items outside `value[0]` to value[1]` and not equal to `value[1]`' ,
62
+
63
+ '*{}* filters items present in a set of values' ,
64
+ '*}{* filters items not present in a set of values'
40
65
] . join ( ' ' )
41
66
} ,
42
67
value : {
@@ -51,19 +76,6 @@ exports.attributes = {
51
76
'is the lower bound and the second item is the upper bound.' ,
52
77
'When `operation`, is set to *in*, *notin* '
53
78
] . join ( ' ' )
54
- } ,
55
- strictinterval : {
56
- valType : 'boolean' ,
57
- dflt : true ,
58
- arrayOk : true ,
59
- description : [
60
- 'Determines whether or not the filter operation includes data item value,' ,
61
- 'equal to *value*.' ,
62
- 'Has only an effect for `operation` *>*, *<*, *within* and *notwithin*' ,
63
- 'When `operation` is set to *within* and *notwithin*,' ,
64
- '`strictinterval` is expected to be a 2-item array where the first (second)' ,
65
- 'item determines strictness for the lower (second) bound.'
66
- ] . join ( ' ' )
67
79
}
68
80
} ;
69
81
@@ -77,14 +89,9 @@ exports.supplyDefaults = function(transformIn) {
77
89
var enabled = coerce ( 'enabled' ) ;
78
90
79
91
if ( enabled ) {
80
- var operation = coerce ( 'operation' ) ;
81
-
92
+ coerce ( 'operation' ) ;
82
93
coerce ( 'value' ) ;
83
94
coerce ( 'filtersrc' ) ;
84
-
85
- if ( [ '=' , 'in' , 'notin' ] . indexOf ( operation ) === - 1 ) {
86
- coerce ( 'strictinterval' ) ;
87
- }
88
95
}
89
96
90
97
return transformOut ;
@@ -154,33 +161,23 @@ function getDataToCoordFunc(gd, filtersrc) {
154
161
function getFilterFunc ( opts , d2c ) {
155
162
var operation = opts . operation ,
156
163
value = opts . value ,
157
- hasArrayValue = Array . isArray ( value ) ,
158
- strict = opts . strictinterval ,
159
- hasArrayStrict = Array . isArray ( strict ) ;
164
+ hasArrayValue = Array . isArray ( value ) ;
160
165
161
166
function isOperationIn ( array ) {
162
167
return array . indexOf ( operation ) !== - 1 ;
163
168
}
164
169
165
- var coercedValue , coercedStrict ;
170
+ var coercedValue ;
166
171
167
- if ( isOperationIn ( [ '=' , '<' , '>' ] ) ) {
172
+ if ( isOperationIn ( INEQUALITY_OPS ) ) {
168
173
coercedValue = hasArrayValue ? d2c ( value [ 0 ] ) : d2c ( value ) ;
169
-
170
- if ( isOperationIn ( [ '<' , '>' ] ) ) {
171
- coercedStrict = hasArrayStrict ? strict [ 0 ] : strict ;
172
- }
173
174
}
174
- else if ( isOperationIn ( [ 'within' , 'notwithin' ] ) ) {
175
+ else if ( isOperationIn ( INTERVAL_OPS ) ) {
175
176
coercedValue = hasArrayValue ?
176
177
[ d2c ( value [ 0 ] ) , d2c ( value [ 1 ] ) ] :
177
178
[ d2c ( value ) , d2c ( value ) ] ;
178
-
179
- coercedStrict = hasArrayStrict ?
180
- [ strict [ 0 ] , strict [ 1 ] ] :
181
- [ strict , strict ] ;
182
179
}
183
- else if ( isOperationIn ( [ 'in' , 'notin' ] ) ) {
180
+ else if ( isOperationIn ( SET_OPS ) ) {
184
181
coercedValue = hasArrayValue ? value . map ( d2c ) : [ d2c ( value ) ] ;
185
182
}
186
183
@@ -190,85 +187,71 @@ function getFilterFunc(opts, d2c) {
190
187
return function ( v ) { return d2c ( v ) === coercedValue ; } ;
191
188
192
189
case '<' :
193
- if ( coercedStrict ) {
194
- return function ( v ) { return d2c ( v ) < coercedValue ; } ;
195
- }
196
- else {
197
- return function ( v ) { return d2c ( v ) <= coercedValue ; } ;
198
- }
190
+ return function ( v ) { return d2c ( v ) < coercedValue ; } ;
191
+
192
+ case '<=' :
193
+ return function ( v ) { return d2c ( v ) <= coercedValue ; } ;
199
194
200
195
case '>' :
201
- if ( coercedStrict ) {
202
- return function ( v ) { return d2c ( v ) > coercedValue ; } ;
203
- }
204
- else {
205
- return function ( v ) { return d2c ( v ) >= coercedValue ; } ;
206
- }
207
-
208
- case 'within' :
209
-
210
- if ( coercedStrict [ 0 ] && coercedStrict [ 1 ] ) {
211
- return function ( v ) {
212
- var cv = d2c ( v ) ;
213
- return cv > coercedValue [ 0 ] && cv < coercedValue [ 1 ] ;
214
- } ;
215
- }
216
- else if ( coercedStrict [ 0 ] && ! coercedStrict [ 1 ] ) {
217
- return function ( v ) {
218
- var cv = d2c ( v ) ;
219
- return cv > coercedValue [ 0 ] && cv <= coercedValue [ 1 ] ;
220
- } ;
221
- }
222
- else if ( ! coercedStrict [ 0 ] && coercedStrict [ 1 ] ) {
223
- return function ( v ) {
224
- var cv = d2c ( v ) ;
225
- return cv >= coercedValue [ 0 ] && cv < coercedValue [ 1 ] ;
226
- } ;
227
- }
228
- else if ( ! coercedStrict [ 0 ] && ! coercedStrict [ 1 ] ) {
229
- return function ( v ) {
230
- var cv = d2c ( v ) ;
231
- return cv >= coercedValue [ 0 ] && cv <= coercedValue [ 1 ] ;
232
- } ;
233
- }
234
-
235
- break ;
236
-
237
- case 'notwithin' :
238
-
239
- if ( coercedStrict [ 0 ] && coercedStrict [ 1 ] ) {
240
- return function ( v ) {
241
- var cv = d2c ( v ) ;
242
- return cv < coercedValue [ 0 ] || cv > coercedValue [ 1 ] ;
243
- } ;
244
- }
245
- else if ( coercedStrict [ 0 ] && ! coercedStrict [ 1 ] ) {
246
- return function ( v ) {
247
- var cv = d2c ( v ) ;
248
- return cv < coercedValue [ 0 ] || cv >= coercedValue [ 1 ] ;
249
- } ;
250
- }
251
- else if ( ! coercedStrict [ 0 ] && coercedStrict [ 1 ] ) {
252
- return function ( v ) {
253
- var cv = d2c ( v ) ;
254
- return cv <= coercedValue [ 0 ] || cv > coercedValue [ 1 ] ;
255
- } ;
256
- }
257
- else if ( ! coercedStrict [ 0 ] && ! coercedStrict [ 1 ] ) {
258
- return function ( v ) {
259
- var cv = d2c ( v ) ;
260
- return cv <= coercedValue [ 0 ] || cv >= coercedValue [ 1 ] ;
261
- } ;
262
- }
263
-
264
- break ;
265
-
266
- case 'in' :
196
+ return function ( v ) { return d2c ( v ) > coercedValue ; } ;
197
+
198
+ case '>=' :
199
+ return function ( v ) { return d2c ( v ) >= coercedValue ; } ;
200
+
201
+ case '[]' :
202
+ return function ( v ) {
203
+ var cv = d2c ( v ) ;
204
+ return cv >= coercedValue [ 0 ] && cv <= coercedValue [ 1 ] ;
205
+ } ;
206
+
207
+ case '()' :
208
+ return function ( v ) {
209
+ var cv = d2c ( v ) ;
210
+ return cv > coercedValue [ 0 ] && cv < coercedValue [ 1 ] ;
211
+ } ;
212
+
213
+ case '[)' :
214
+ return function ( v ) {
215
+ var cv = d2c ( v ) ;
216
+ return cv >= coercedValue [ 0 ] && cv < coercedValue [ 1 ] ;
217
+ } ;
218
+
219
+ case '(]' :
220
+ return function ( v ) {
221
+ var cv = d2c ( v ) ;
222
+ return cv > coercedValue [ 0 ] && cv <= coercedValue [ 1 ] ;
223
+ } ;
224
+
225
+ case '][' :
226
+ return function ( v ) {
227
+ var cv = d2c ( v ) ;
228
+ return cv < coercedValue [ 0 ] || cv > coercedValue [ 1 ] ;
229
+ } ;
230
+
231
+ case ')(' :
232
+ return function ( v ) {
233
+ var cv = d2c ( v ) ;
234
+ return cv <= coercedValue [ 0 ] || cv >= coercedValue [ 1 ] ;
235
+ } ;
236
+
237
+ case '](' :
238
+ return function ( v ) {
239
+ var cv = d2c ( v ) ;
240
+ return cv < coercedValue [ 0 ] || cv >= coercedValue [ 1 ] ;
241
+ } ;
242
+
243
+ case ')[' :
244
+ return function ( v ) {
245
+ var cv = d2c ( v ) ;
246
+ return cv <= coercedValue [ 0 ] || cv > coercedValue [ 1 ] ;
247
+ } ;
248
+
249
+ case '{}' :
267
250
return function ( v ) {
268
251
return coercedValue . indexOf ( d2c ( v ) ) !== - 1 ;
269
252
} ;
270
253
271
- case 'notin ' :
254
+ case '}{ ' :
272
255
return function ( v ) {
273
256
return coercedValue . indexOf ( d2c ( v ) ) === - 1 ;
274
257
} ;
0 commit comments