Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e0fe2f

Browse files
committed
AJ-proof filter preservegaps
- add COMPARISON_OPS list - fixup `preservegaps` description - add `preservegaps` commute tests
1 parent bb34df2 commit 1e0fe2f

File tree

2 files changed

+111
-7
lines changed

2 files changed

+111
-7
lines changed

src/transforms/filter.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var axisIds = require('../plots/cartesian/axis_ids');
1515
var autoType = require('../plots/cartesian/axis_autotype');
1616
var setConvert = require('../plots/cartesian/set_convert');
1717

18-
var INEQUALITY_OPS = ['=', '!=', '<', '>=', '>', '<='];
18+
var COMPARISON_OPS = ['=', '!='];
19+
var INEQUALITY_OPS = ['<', '>=', '>', '<='];
1920
var INTERVAL_OPS = ['[]', '()', '[)', '(]', '][', ')(', '](', ')['];
2021
var SET_OPS = ['{}', '}{'];
2122

@@ -51,7 +52,11 @@ exports.attributes = {
5152
},
5253
operation: {
5354
valType: 'enumerated',
54-
values: [].concat(INEQUALITY_OPS).concat(INTERVAL_OPS).concat(SET_OPS),
55+
values: []
56+
.concat(COMPARISON_OPS)
57+
.concat(INEQUALITY_OPS)
58+
.concat(INTERVAL_OPS)
59+
.concat(SET_OPS),
5560
dflt: '=',
5661
description: [
5762
'Sets the filter operation.',
@@ -88,8 +93,9 @@ exports.attributes = {
8893
'Values are expected to be in the same type as the data linked',
8994
'to *target*.',
9095

91-
'When `operation` is set to one of the inequality values',
92-
'(' + INEQUALITY_OPS + ')',
96+
'When `operation` is set to one of',
97+
'the comparison or (' + COMPARISON_OPS + ')',
98+
'inequality values (' + INEQUALITY_OPS + ')',
9399
'*value* is expected to be a number or a string.',
94100

95101
'When `operation` is set to one of the interval value',
@@ -108,9 +114,9 @@ exports.attributes = {
108114
dflt: false,
109115
description: [
110116
'Determines whether or not gaps in data arrays produced by the filter operation',
111-
'are preserved or not.',
117+
'are preserved.',
112118
'Setting this to *true* might be useful when plotting a line chart',
113-
'with `connectgaps` set to *true*.'
119+
'with `connectgaps` set to *false*.'
114120
].join(' ')
115121
},
116122
};
@@ -268,7 +274,7 @@ function getFilterFunc(opts, d2c, targetCalendar) {
268274

269275
var coercedValue;
270276

271-
if(isOperationIn(INEQUALITY_OPS)) {
277+
if(isOperationIn(COMPARISON_OPS) || isOperationIn(INEQUALITY_OPS)) {
272278
coercedValue = hasArrayValue ? d2cValue(value[0]) : d2cValue(value);
273279
}
274280
else if(isOperationIn(INTERVAL_OPS)) {

test/jasmine/tests/transform_filter_test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,104 @@ describe('filter transforms calc:', function() {
337337
expect(out[0].marker.color).toEqual([undefined, undefined, undefined, undefined, 0.2, 0.3, 0.4]);
338338
});
339339

340+
it('two filter transforms with `preservegaps: true` should commute', function() {
341+
var transform0 = {
342+
type: 'filter',
343+
preservegaps: true,
344+
operation: '>',
345+
value: -1,
346+
target: 'x'
347+
};
348+
349+
var transform1 = {
350+
type: 'filter',
351+
preservegaps: true,
352+
operation: '<',
353+
value: 2,
354+
target: 'x'
355+
};
356+
357+
var out0 = _transform([Lib.extendDeep({}, base, {
358+
transforms: [transform0, transform1]
359+
})]);
360+
361+
var out1 = _transform([Lib.extendDeep({}, base, {
362+
transforms: [transform1, transform0]
363+
})]);
364+
365+
['x', 'y', 'ids', 'marker.color', 'marker.size'].forEach(function(k) {
366+
var v0 = Lib.nestedProperty(out0[0], k).get();
367+
var v1 = Lib.nestedProperty(out1[0], k).get();
368+
expect(v0).toEqual(v1);
369+
});
370+
});
371+
372+
it('two filter transforms with `preservegaps: false` should commute', function() {
373+
var transform0 = {
374+
type: 'filter',
375+
preservegaps: false,
376+
operation: '>',
377+
value: -1,
378+
target: 'x'
379+
};
380+
381+
var transform1 = {
382+
type: 'filter',
383+
preservegaps: false,
384+
operation: '<',
385+
value: 2,
386+
target: 'x'
387+
};
388+
389+
var out0 = _transform([Lib.extendDeep({}, base, {
390+
transforms: [transform0, transform1]
391+
})]);
392+
393+
var out1 = _transform([Lib.extendDeep({}, base, {
394+
transforms: [transform1, transform0]
395+
})]);
396+
397+
['x', 'y', 'ids', 'marker.color', 'marker.size'].forEach(function(k) {
398+
var v0 = Lib.nestedProperty(out0[0], k).get();
399+
var v1 = Lib.nestedProperty(out1[0], k).get();
400+
expect(v0).toEqual(v1);
401+
});
402+
});
403+
404+
it('two filter transforms with different `preservegaps` values should not necessary commute', function() {
405+
var transform0 = {
406+
type: 'filter',
407+
preservegaps: true,
408+
operation: '>',
409+
value: -1,
410+
target: 'x'
411+
};
412+
413+
var transform1 = {
414+
type: 'filter',
415+
preservegaps: false,
416+
operation: '<',
417+
value: 2,
418+
target: 'x'
419+
};
420+
421+
var out0 = _transform([Lib.extendDeep({}, base, {
422+
transforms: [transform0, transform1]
423+
})]);
424+
425+
expect(out0[0].x).toEqual([0, 1]);
426+
expect(out0[0].y).toEqual([1, 2]);
427+
expect(out0[0].marker.color).toEqual([0.1, 0.2]);
428+
429+
var out1 = _transform([Lib.extendDeep({}, base, {
430+
transforms: [transform1, transform0]
431+
})]);
432+
433+
expect(out1[0].x).toEqual([undefined, undefined, undefined, 0, 1]);
434+
expect(out1[0].y).toEqual([undefined, undefined, undefined, 1, 2]);
435+
expect(out1[0].marker.color).toEqual([undefined, undefined, undefined, 0.1, 0.2]);
436+
});
437+
340438
describe('filters should handle numeric values', function() {
341439
var _base = Lib.extendDeep({}, base);
342440

0 commit comments

Comments
 (0)