From b550b2801b597d929aaf761ca5e72d4a5d295abf Mon Sep 17 00:00:00 2001 From: kielni Date: Fri, 25 Oct 2013 10:17:41 -0700 Subject: [PATCH] make backend memory store work with from/to filters --- dist/recline.js | 10 +++++---- src/backend.memory.js | 10 +++++---- test/backend.memory.test.js | 45 +++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/dist/recline.js b/dist/recline.js index da4b6b53..d95cbb53 100644 --- a/dist/recline.js +++ b/dist/recline.js @@ -524,12 +524,14 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {}; } function range(record, filter) { - var startnull = (filter.start === null || filter.start === ''); - var stopnull = (filter.stop === null || filter.stop === ''); + var filterStart = filter.start || filter.from; + var filterStop = filter.stop || filter.to; + var startnull = (_.isUndefined(filterStart) || filterStart === null || filterStart === ''); + var stopnull = (_.isUndefined(filterStop) || filterStop === null || filterStop === ''); var parse = getDataParser(filter); var value = parse(record[filter.field]); - var start = parse(filter.start); - var stop = parse(filter.stop); + var start = parse(startnull ? '' : filterStart); + var stop = parse(stopnull ? '' : filterStop); // if at least one end of range is set do not allow '' to get through // note that for strings '' <= {any-character} e.g. '' <= 'a' diff --git a/src/backend.memory.js b/src/backend.memory.js index 0e6094cc..00f1db4c 100644 --- a/src/backend.memory.js +++ b/src/backend.memory.js @@ -141,12 +141,14 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {}; } function range(record, filter) { - var startnull = (filter.start === null || filter.start === ''); - var stopnull = (filter.stop === null || filter.stop === ''); + var filterStart = filter.start || filter.from; + var filterStop = filter.stop || filter.to; + var startnull = (_.isUndefined(filterStart) || filterStart === null || filterStart === ''); + var stopnull = (_.isUndefined(filterStop) || filterStop === null || filterStop === ''); var parse = getDataParser(filter); var value = parse(record[filter.field]); - var start = parse(filter.start); - var stop = parse(filter.stop); + var start = parse(startnull ? '' : filterStart); + var stop = parse(stopnull ? '' : filterStop); // if at least one end of range is set do not allow '' to get through // note that for strings '' <= {any-character} e.g. '' <= 'a' diff --git a/test/backend.memory.test.js b/test/backend.memory.test.js index e4636884..9b88bec2 100644 --- a/test/backend.memory.test.js +++ b/test/backend.memory.test.js @@ -112,6 +112,20 @@ test('filters', function () { equal(out.total, 3); deepEqual(_.pluck(out.hits, 'z'), [3,6,9]); }); + + 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) { + equal(out.total, 3); + deepEqual(_.pluck(out.hits, 'date'), ['2011-01-01','2011-02-03','2011-04-05']); + }); + + query = new recline.Model.Query(); + query.addFilter({type: 'range', field: 'z', from: '0', to: '10'}); + data.query(query.toJSON()).then(function(out) { + equal(out.total, 3); + deepEqual(_.pluck(out.hits, 'z'), [3,6,9]); + }); }); @@ -124,18 +138,36 @@ test('filters with nulls', function () { equal(out.total, 6); }); + query = new recline.Model.Query(); + query.addFilter({type: 'range', field: 'z', from: '', to: null}); + data.query(query.toJSON()).then(function(out) { + equal(out.total, 6); + }); + query = new recline.Model.Query(); query.addFilter({type: 'range', field: 'x', start: '', stop: '3'}); data.query(query.toJSON()).then(function(out) { equal(out.total, 3); }); + query = new recline.Model.Query(); + query.addFilter({type: 'range', field: 'x', from: '', to: '3'}); + data.query(query.toJSON()).then(function(out) { + equal(out.total, 3); + }); + query = new recline.Model.Query(); query.addFilter({type: 'range', field: 'x', start: '3', stop: ''}); data.query(query.toJSON()).then(function(out) { equal(out.total, 4); }); + query = new recline.Model.Query(); + query.addFilter({type: 'range', field: 'x', from: '3', to: ''}); + data.query(query.toJSON()).then(function(out) { + equal(out.total, 4); + }); + data.records[5].country = ''; query = new recline.Model.Query(); query.addFilter({type: 'range', field: 'country', start: '', stop: 'Z'}); @@ -143,11 +175,24 @@ test('filters with nulls', function () { equal(out.total, 5); }); + query = new recline.Model.Query(); + query.addFilter({type: 'range', field: 'country', from: '', to: 'Z'}); + data.query(query.toJSON()).then(function(out) { + equal(out.total, 5); + }); + query = new recline.Model.Query(); query.addFilter({type: 'range', field: 'x', start: '', stop: ''}); data.query(query.toJSON()).then(function(out) { equal(out.total, 6); }); + + query = new recline.Model.Query(); + query.addFilter({type: 'range', field: 'x', from: '', to: ''}); + data.query(query.toJSON()).then(function(out) { + equal(out.total, 6); + }); + }); test('facet', function () {