From 4e5ef05e5e060f120a31b971baf3da2982ac821a Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 25 Jun 2013 11:55:01 -0600 Subject: [PATCH] Expression for evaluating feature ids --- src/ol/expr/expression.js | 20 ++++++++++++ test/spec/ol/expr/expression.test.js | 48 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/ol/expr/expression.js b/src/ol/expr/expression.js index 3739ffc54b..81a511bc26 100644 --- a/src/ol/expr/expression.js +++ b/src/ol/expr/expression.js @@ -96,6 +96,7 @@ ol.expr.lib = {}; */ ol.expr.functions = { EXTENT: 'extent', + FID: 'fid', GEOMETRY_TYPE: 'geometryType' }; @@ -119,8 +120,27 @@ ol.expr.lib[ol.expr.functions.EXTENT] = function(minX, maxX, minY, maxY) { return intersects; }; + +/** + * Determine if the feature identifier matches any of the provided values. + * @param {...string} var_args Feature identifiers. + * @return {boolean} The feature's identifier matches one of the given values. + * @this {ol.Feature} + */ +ol.expr.lib[ol.expr.functions.FID] = function(var_args) { + var matches = false; + var id = this.getFeatureId(); + if (goog.isDef(id)) { + for (var i = 0, ii = arguments.length; i < ii; ++i) { + if (arguments[i] === id) { + matches = true; + break; + } } } + return matches; +}; + /** * Determine if a feature's default geometry is of the given type. diff --git a/test/spec/ol/expr/expression.test.js b/test/spec/ol/expr/expression.test.js index ca017825d8..30b52945d5 100644 --- a/test/spec/ol/expr/expression.test.js +++ b/test/spec/ol/expr/expression.test.js @@ -663,6 +663,54 @@ describe('ol.expr.lib', function() { }); + describe('fid()', function() { + + var one = new ol.Feature(); + one.setFeatureId('one'); + + var two = new ol.Feature(); + two.setFeatureId('two'); + + var three = new ol.Feature(); + three.setFeatureId('three'); + + var four = new ol.Feature(); + four.setFeatureId('four'); + + var odd = parse('fid("one", "three")'); + var even = parse('fid("two", "four")'); + var first = parse('fid("one")'); + var last = parse('fid("four")'); + var none = parse('fid("foo")'); + + it('evaluates to true if feature id matches', function() { + expect(evaluate(odd, one), true); + expect(evaluate(odd, three), true); + expect(evaluate(even, two), true); + expect(evaluate(even, four), true); + expect(evaluate(first, one), true); + expect(evaluate(last, four), true); + }); + + it('evaluates to false if feature id doesn\'t match', function() { + expect(evaluate(odd, two), false); + expect(evaluate(odd, four), false); + expect(evaluate(even, one), false); + expect(evaluate(even, three), false); + expect(evaluate(first, two), false); + expect(evaluate(first, three), false); + expect(evaluate(first, four), false); + expect(evaluate(last, one), false); + expect(evaluate(last, two), false); + expect(evaluate(last, three), false); + expect(evaluate(none, one), false); + expect(evaluate(none, two), false); + expect(evaluate(none, three), false); + expect(evaluate(none, four), false); + }); + + }); + describe('geometryType()', function() { var point = new ol.Feature({