implement @tschaub's suggestions for the LIKE function

This commit is contained in:
Bart van den Eijnden
2013-07-22 13:23:08 +02:00
parent 31fd41ab14
commit 875edc540e
2 changed files with 41 additions and 11 deletions

View File

@@ -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);
};

View File

@@ -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() {