Skip to content

Commit

Permalink
add support for terms filter; related to 32f5977ac39df3df832b6b6b5615…
Browse files Browse the repository at this point in the history
…2261a6ad7cd9
kielni committed Feb 4, 2014

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8c1a135 commit c275684
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
@@ -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
};
@@ -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 === '');
14 changes: 14 additions & 0 deletions test/backend.memory.test.js
Original file line number Diff line number Diff line change
@@ -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) {
@@ -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() {

0 comments on commit c275684

Please sign in to comment.