Convenient factories for logical filters
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
goog.provide('ol.filter.Logical');
|
||||
goog.provide('ol.filter.LogicalOperator');
|
||||
goog.provide('ol.filter.and');
|
||||
goog.provide('ol.filter.not');
|
||||
goog.provide('ol.filter.or');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.filter.Filter');
|
||||
@@ -79,3 +82,37 @@ ol.filter.LogicalOperator = {
|
||||
OR: '||',
|
||||
NOT: '!'
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a filter that evaluates to true if any of the provided filters
|
||||
* evaluate to true.
|
||||
* @param {...ol.filter.Filter} var_filters Filters.
|
||||
* @return {ol.filter.Logical} A logical filter.
|
||||
*/
|
||||
ol.filter.and = function(var_filters) {
|
||||
var filters = Array.prototype.slice.call(arguments);
|
||||
return new ol.filter.Logical(filters, ol.filter.LogicalOperator.AND);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a new filter that is the logical compliment of another.
|
||||
* @param {ol.filter.Filter} filter The filter to negate.
|
||||
* @return {ol.filter.Logical} A logical filter.
|
||||
*/
|
||||
ol.filter.not = function(filter) {
|
||||
return new ol.filter.Logical([filter], ol.filter.LogicalOperator.NOT);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a filter that evaluates to true if all of the provided filters
|
||||
* evaluate to true.
|
||||
* @param {...ol.filter.Filter} var_filters Filters.
|
||||
* @return {ol.filter.Logical} A logical filter.
|
||||
*/
|
||||
ol.filter.or = function(var_filters) {
|
||||
var filters = Array.prototype.slice.call(arguments);
|
||||
return new ol.filter.Logical(filters, ol.filter.LogicalOperator.OR);
|
||||
};
|
||||
|
||||
@@ -91,7 +91,54 @@ describe('ol.filter.Logical', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('ol.filter.and', function() {
|
||||
it('creates the a logical AND filter', function() {
|
||||
var a = new ol.filter.Filter();
|
||||
var b = new ol.filter.Filter();
|
||||
var c = new ol.filter.Filter();
|
||||
var and = ol.filter.and(a, b, c);
|
||||
expect(and).to.be.a(ol.filter.Logical);
|
||||
expect(and.operator).to.be(ol.filter.LogicalOperator.AND);
|
||||
var filters = and.getFilters();
|
||||
expect(filters[0]).to.be(a);
|
||||
expect(filters[1]).to.be(b);
|
||||
expect(filters[2]).to.be(c);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ol.filter.not', function() {
|
||||
it('creates the logical compliment of another filter', function() {
|
||||
var include = new ol.filter.Filter(function() {return true;});
|
||||
var notInclude = ol.filter.not(include);
|
||||
expect(notInclude).to.be.a(ol.filter.Logical);
|
||||
expect(notInclude.applies()).to.be(false);
|
||||
|
||||
var exclude = new ol.filter.Filter(function() {return false;});
|
||||
var notExclude = ol.filter.not(exclude);
|
||||
expect(notExclude).to.be.a(ol.filter.Logical);
|
||||
expect(notExclude.applies()).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ol.filter.or', function() {
|
||||
it('creates the a logical OR filter', function() {
|
||||
var a = new ol.filter.Filter();
|
||||
var b = new ol.filter.Filter();
|
||||
var c = new ol.filter.Filter();
|
||||
var and = ol.filter.or(a, b, c);
|
||||
expect(and).to.be.a(ol.filter.Logical);
|
||||
expect(and.operator).to.be(ol.filter.LogicalOperator.OR);
|
||||
var filters = and.getFilters();
|
||||
expect(filters[0]).to.be(a);
|
||||
expect(filters[1]).to.be(b);
|
||||
expect(filters[2]).to.be(c);
|
||||
});
|
||||
});
|
||||
|
||||
goog.require('ol.Feature');
|
||||
goog.require('ol.filter.Filter');
|
||||
goog.require('ol.filter.Logical');
|
||||
goog.require('ol.filter.LogicalOperator');
|
||||
goog.require('ol.filter.and');
|
||||
goog.require('ol.filter.not');
|
||||
goog.require('ol.filter.or');
|
||||
|
||||
Reference in New Issue
Block a user