diff --git a/src/ol/expr/expression.js b/src/ol/expr/expression.js index bc732e825f..d90eca6b0d 100644 --- a/src/ol/expr/expression.js +++ b/src/ol/expr/expression.js @@ -154,16 +154,17 @@ ol.expr.lib[ol.expr.functions.FID] = function(var_args) { /** - * Determine if a feature attribute is like the provided value. - * @param {string} attribute The name of the attribute to test for. - * @param {string} value The value to test for. + * Determine if two strings are like one another, based on simple pattern + * matching. + * @param {string} value The string to test. + * @param {string} pattern The comparison pattern. * @param {string} wildCard The wildcard character to use. * @param {string} singleChar The single character to use. * @param {string} escapeChar The escape character to use. * @param {boolean} matchCase Should we match case or not? * @this {ol.Feature} */ -ol.expr.lib[ol.expr.functions.LIKE] = function(attribute, value, wildCard, +ol.expr.lib[ol.expr.functions.LIKE] = function(value, pattern, wildCard, singleChar, escapeChar, matchCase) { if (wildCard == '.') { throw new Error('"." is an unsupported wildCard character for ' + @@ -173,19 +174,18 @@ ol.expr.lib[ol.expr.functions.LIKE] = function(attribute, value, wildCard, wildCard = goog.isDef(wildCard) ? wildCard : '*'; singleChar = goog.isDef(singleChar) ? singleChar : '.'; escapeChar = goog.isDef(escapeChar) ? escapeChar : '!'; - var val; - val = value.replace( + pattern = pattern.replace( new RegExp('\\' + escapeChar + '(.|$)', 'g'), '\\$1'); - val = value.replace( + pattern = pattern.replace( new RegExp('\\' + singleChar, 'g'), '.'); - val = value.replace( + pattern = pattern.replace( new RegExp('\\' + wildCard, 'g'), '.*'); - val = value.replace( + pattern = pattern.replace( new RegExp('\\\\.\\*', 'g'), '\\' + wildCard); - val = value.replace( + pattern = pattern.replace( new RegExp('\\\\\\.', 'g'), '\\' + singleChar); var modifiers = (matchCase === false) ? 'gi' : 'g'; - return new RegExp(val, modifiers).test(this.get(attribute)); + return new RegExp(pattern, modifiers).test(value); }; diff --git a/test/spec/ol/expr/expression.test.js b/test/spec/ol/expr/expression.test.js index 30b52945d5..009f467a2e 100644 --- a/test/spec/ol/expr/expression.test.js +++ b/test/spec/ol/expr/expression.test.js @@ -758,6 +758,36 @@ describe('ol.expr.lib', function() { }); + describe('like()', function() { + + var one = new ol.Feature({foo: 'bar'}); + var two = new ol.Feature({foo: 'baz'}); + var three = new ol.Feature({foo: 'foo'}); + + var like = parse('like(foo, "ba")'); + + it('First and second feature match, third does not', function() { + expect(evaluate(like, one), true); + expect(evaluate(like, two), true); + expect(evaluate(like, three), false); + }); + + var uclike = parse('like(foo, "BA")'); + it('Matchcase is true by default', function() { + expect(evaluate(uclike, one), false); + expect(evaluate(uclike, two), false); + expect(evaluate(uclike, three), false); + }); + + var ilike = parse('like(foo, "BA", "*", ".", "!", false)'); + it('Using matchcase false, first two features match again', function() { + expect(evaluate(ilike, one), true); + expect(evaluate(ilike, two), true); + expect(evaluate(uclike, three), false); + }); + + }); + }); describe('ol.expr.register()', function() {