Skip to content

Commit

Permalink
Merge pull request datopian#389 from kielni/terms-filter
Browse files Browse the repository at this point in the history
add support for terms filter
  • Loading branch information
rufuspollock committed Feb 5, 2014
2 parents 5a23913 + c275684 commit 2ccc162
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/backend.memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
// register filters
var filterFunctions = {
term : term,
terms : terms,
range : range,
geo_distance : geo_distance
};
Expand Down Expand Up @@ -140,6 +141,14 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
return (value === term);
}

function terms(record, filter) {
var parse = getDataParser(filter);
var value = parse(record[filter.field]);
var terms = parse(filter.terms).split(",");

return (_.indexOf(terms, value) >= 0);
}

function range(record, filter) {
var fromnull = (_.isUndefined(filter.from) || filter.from === null || filter.from === '');
var tonull = (_.isUndefined(filter.to) || filter.to === null || filter.to === '');
Expand Down
14 changes: 14 additions & 0 deletions test/backend.memory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ test('filters', function () {
deepEqual(_.pluck(out.hits, 'country'), ['UK','UK','UK']);
});

query = new recline.Model.Query();
query.addFilter({type: 'terms', field: 'country', terms: ['UK','DE']});
data.query(query.toJSON()).then(function(out) {
equal(out.total, 5);
deepEqual(_.pluck(out.hits, 'country'), ['DE','UK','UK','UK','DE']);
});

query = new recline.Model.Query();
query.addFilter({type: 'range', field: 'date', from: '2011-01-01', to: '2011-05-01'});
data.query(query.toJSON()).then(function(out) {
Expand Down Expand Up @@ -307,6 +314,13 @@ test('filters', function () {
deepEqual(dataset.records.pluck('country'), ['UK', 'UK', 'UK']);
});

dataset = makeBackendDataset();
dataset.queryState.addFilter({type: 'terms', field: 'country', terms: ['UK','DE']});
dataset.query().then(function() {
equal(dataset.records.length, 5);
deepEqual(dataset.records.pluck('country'), ['DE','UK', 'UK', 'UK','DE']);
});

dataset = makeBackendDataset();
dataset.queryState.addFilter({type: 'range', field: 'date', from: '2011-01-01', to: '2011-05-01'});
dataset.query().then(function() {
Expand Down

0 comments on commit 2ccc162

Please sign in to comment.