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
+11 -11
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. * Determine if two strings are like one another, based on simple pattern
* @param {string} attribute The name of the attribute to test for. * matching.
* @param {string} value The value to test for. * @param {string} value The string to test.
* @param {string} pattern The comparison pattern.
* @param {string} wildCard The wildcard character to use. * @param {string} wildCard The wildcard character to use.
* @param {string} singleChar The single character to use. * @param {string} singleChar The single character to use.
* @param {string} escapeChar The escape character to use. * @param {string} escapeChar The escape character to use.
* @param {boolean} matchCase Should we match case or not? * @param {boolean} matchCase Should we match case or not?
* @this {ol.Feature} * @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) { singleChar, escapeChar, matchCase) {
if (wildCard == '.') { if (wildCard == '.') {
throw new Error('"." is an unsupported wildCard character for ' + 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 : '*'; wildCard = goog.isDef(wildCard) ? wildCard : '*';
singleChar = goog.isDef(singleChar) ? singleChar : '.'; singleChar = goog.isDef(singleChar) ? singleChar : '.';
escapeChar = goog.isDef(escapeChar) ? escapeChar : '!'; escapeChar = goog.isDef(escapeChar) ? escapeChar : '!';
var val; pattern = pattern.replace(
val = value.replace(
new RegExp('\\' + escapeChar + '(.|$)', 'g'), '\\$1'); new RegExp('\\' + escapeChar + '(.|$)', 'g'), '\\$1');
val = value.replace( pattern = pattern.replace(
new RegExp('\\' + singleChar, 'g'), '.'); new RegExp('\\' + singleChar, 'g'), '.');
val = value.replace( pattern = pattern.replace(
new RegExp('\\' + wildCard, 'g'), '.*'); new RegExp('\\' + wildCard, 'g'), '.*');
val = value.replace( pattern = pattern.replace(
new RegExp('\\\\.\\*', 'g'), '\\' + wildCard); new RegExp('\\\\.\\*', 'g'), '\\' + wildCard);
val = value.replace( pattern = pattern.replace(
new RegExp('\\\\\\.', 'g'), '\\' + singleChar); new RegExp('\\\\\\.', 'g'), '\\' + singleChar);
var modifiers = (matchCase === false) ? 'gi' : 'g'; var modifiers = (matchCase === false) ? 'gi' : 'g';
return new RegExp(val, modifiers).test(this.get(attribute)); return new RegExp(pattern, modifiers).test(value);
}; };
+30
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() { describe('ol.expr.register()', function() {