Skip to content
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

Fix filter so logical operations are constructed correctly #123

Merged
merged 2 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 35 additions & 4 deletions Filter.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
define(['dojo/_base/declare'], function (declare) {
// a Filter builder
function filterCreator(type) {
// constructs a new filter based on type, used to create each method
// constructs a new filter based on type, used to create each comparison method
return function newFilter() {
var Filter = this.constructor;
var filter = new Filter();
filter.type = type;
filter.args = arguments;
// ensure args is array so we can concat, slice, unshift
filter.args = Array.prototype.slice.call(arguments);
if (this.type) {
// we are chaining, so combine with an and operator
return filterCreator('and').call(Filter.prototype, this, filter);
}
return filter;
};
}
function logicalOperatorCreator(type) {
// constructs a new logical operator 'filter', used to create each logical operation method
return function newLogicalOperator() {
var Filter = this.constructor;
var argsArray = [];
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
argsArray.push(arg instanceof Filter ? arg : new Filter(arg));
}
var filter = new Filter();
filter.type = type;
filter.args = argsArray;
if (this.type === type) {
// chaining, same type
// combine arguments
filter.args = this.args.concat(argsArray);
} else if (this.type) {
// chaining, different type
// add this filter to start of arguments
argsArray.unshift(this);
} else if (argsArray.length === 1) {
// not chaining and only one argument
// returned filter is the same as the single argument
filter.type = argsArray[0].type;
filter.args = argsArray[0].args.slice();
}
return filter;
};
}
var Filter = declare(null, {
constructor: function (filterArg) {
var argType = typeof filterArg;
Expand Down Expand Up @@ -43,8 +73,8 @@ define(['dojo/_base/declare'], function (declare) {
}
},
// define our operators
and: filterCreator('and'),
or: filterCreator('or'),
and: logicalOperatorCreator('and'),
or: logicalOperatorCreator('or'),
eq: filterCreator('eq'),
ne: filterCreator('ne'),
lt: filterCreator('lt'),
Expand All @@ -56,5 +86,6 @@ define(['dojo/_base/declare'], function (declare) {
match: filterCreator('match')
});
Filter.filterCreator = filterCreator;
Filter.logicalOperatorCreator = logicalOperatorCreator;
return Filter;
});
3 changes: 1 addition & 2 deletions tests/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ define([
var filter = new store.Filter();
var betweenTwoAndFour = filter.ne('id', 2).or(filter.eq('foo', true), filter.eq('foo'));
return runCollectionTest(store.filter(betweenTwoAndFour), { queryParams: {
id: 'ne=2',
'(foo': 'true|foo=undefined)'
id: 'ne=2|foo=true|foo=undefined'
}});
},

Expand Down