Skip to content

Filter transform '!=' operation and 'preservegaps' option #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/transforms/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var axisIds = require('../plots/cartesian/axis_ids');
var autoType = require('../plots/cartesian/axis_autotype');
var setConvert = require('../plots/cartesian/set_convert');

var INEQUALITY_OPS = ['=', '!=', '<', '>=', '>', '<='];
var COMPARISON_OPS = ['=', '!='];
var INEQUALITY_OPS = ['<', '>=', '>', '<='];
var INTERVAL_OPS = ['[]', '()', '[)', '(]', '][', ')(', '](', ')['];
var SET_OPS = ['{}', '}{'];

Expand Down Expand Up @@ -51,7 +52,11 @@ exports.attributes = {
},
operation: {
valType: 'enumerated',
values: [].concat(INEQUALITY_OPS).concat(INTERVAL_OPS).concat(SET_OPS),
values: []
.concat(COMPARISON_OPS)
.concat(INEQUALITY_OPS)
.concat(INTERVAL_OPS)
.concat(SET_OPS),
dflt: '=',
description: [
'Sets the filter operation.',
Expand Down Expand Up @@ -88,8 +93,9 @@ exports.attributes = {
'Values are expected to be in the same type as the data linked',
'to *target*.',

'When `operation` is set to one of the inequality values',
'(' + INEQUALITY_OPS + ')',
'When `operation` is set to one of',
'the comparison or (' + COMPARISON_OPS + ')',
'inequality values (' + INEQUALITY_OPS + ')',
'*value* is expected to be a number or a string.',

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

var coercedValue;

if(isOperationIn(INEQUALITY_OPS)) {
if(isOperationIn(COMPARISON_OPS) || isOperationIn(INEQUALITY_OPS)) {
coercedValue = hasArrayValue ? d2cValue(value[0]) : d2cValue(value);
}
else if(isOperationIn(INTERVAL_OPS)) {
Expand Down
98 changes: 98 additions & 0 deletions test/jasmine/tests/transform_filter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,104 @@ describe('filter transforms calc:', function() {
expect(out[0].marker.color).toEqual([undefined, undefined, undefined, undefined, 0.2, 0.3, 0.4]);
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any interesting cases to test with two filters applied in sequence having different preservegaps values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet. I'll add one in a future commit 🚀

it('two filter transforms with `preservegaps: true` should commute', function() {
var transform0 = {
type: 'filter',
preservegaps: true,
operation: '>',
value: -1,
target: 'x'
};

var transform1 = {
type: 'filter',
preservegaps: true,
operation: '<',
value: 2,
target: 'x'
};

var out0 = _transform([Lib.extendDeep({}, base, {
transforms: [transform0, transform1]
})]);

var out1 = _transform([Lib.extendDeep({}, base, {
transforms: [transform1, transform0]
})]);

['x', 'y', 'ids', 'marker.color', 'marker.size'].forEach(function(k) {
var v0 = Lib.nestedProperty(out0[0], k).get();
var v1 = Lib.nestedProperty(out1[0], k).get();
expect(v0).toEqual(v1);
});
});

it('two filter transforms with `preservegaps: false` should commute', function() {
var transform0 = {
type: 'filter',
preservegaps: false,
operation: '>',
value: -1,
target: 'x'
};

var transform1 = {
type: 'filter',
preservegaps: false,
operation: '<',
value: 2,
target: 'x'
};

var out0 = _transform([Lib.extendDeep({}, base, {
transforms: [transform0, transform1]
})]);

var out1 = _transform([Lib.extendDeep({}, base, {
transforms: [transform1, transform0]
})]);

['x', 'y', 'ids', 'marker.color', 'marker.size'].forEach(function(k) {
var v0 = Lib.nestedProperty(out0[0], k).get();
var v1 = Lib.nestedProperty(out1[0], k).get();
expect(v0).toEqual(v1);
});
});

it('two filter transforms with different `preservegaps` values should not necessary commute', function() {
var transform0 = {
type: 'filter',
preservegaps: true,
operation: '>',
value: -1,
target: 'x'
};

var transform1 = {
type: 'filter',
preservegaps: false,
operation: '<',
value: 2,
target: 'x'
};

var out0 = _transform([Lib.extendDeep({}, base, {
transforms: [transform0, transform1]
})]);

expect(out0[0].x).toEqual([0, 1]);
expect(out0[0].y).toEqual([1, 2]);
expect(out0[0].marker.color).toEqual([0.1, 0.2]);

var out1 = _transform([Lib.extendDeep({}, base, {
transforms: [transform1, transform0]
})]);

expect(out1[0].x).toEqual([undefined, undefined, undefined, 0, 1]);
expect(out1[0].y).toEqual([undefined, undefined, undefined, 1, 2]);
expect(out1[0].marker.color).toEqual([undefined, undefined, undefined, 0.1, 0.2]);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for those tests.


describe('filters should handle numeric values', function() {
var _base = Lib.extendDeep({}, base);

Expand Down