Merge pull request #40 from athoune/master

Approx and substring filters can match against a list of values
This commit is contained in:
Mark Cavage 2011-12-05 08:03:46 -08:00
commit 6783d9f642
5 changed files with 51 additions and 3 deletions

View File

@ -48,8 +48,14 @@ ApproximateFilter.prototype.matches = function(target) {
throw new TypeError('target (object) required');
var matches = false;
if (target.hasOwnProperty(this.attribute))
matches = (this.value === target[this.attribute]);
if (target.hasOwnProperty(this.attribute)) {
var tv = target[this.attribute];
if (Array.isArray(tv)) {
matches = (tv.indexOf(this.value) != -1);
} else {
matches = (this.value === target[this.attribute]);
}
}
return matches;
};

View File

@ -56,3 +56,22 @@ Filter.prototype.toBer = function(ber) {
ber.endSequence();
return ber;
};
/*
* Test a rule against one or more values.
*/
Filter.multi_test = function(rule, value) {
if (Array.isArray(value)) {
var response = false;
for (var i = 0; i < value.length; i++) {
if (rule(value[i])) {
response = true;
break;
}
}
return response;
} else {
return rule(value);
}
};

View File

@ -81,7 +81,9 @@ SubstringFilter.prototype.matches = function(target) {
re += this['final'] + '$';
var matcher = new RegExp(re);
return matcher.test(target[this.attribute]);
return Filter.multi_test(
function(v) { return matcher.test(v); },
target[this.attribute]);
}
return false;

View File

@ -57,6 +57,17 @@ test('match true', function(t) {
});
test('match multiple', function(t) {
var f = new ApproximateFilter({
attribute: 'foo',
value: 'bar'
});
t.ok(f);
t.ok(f.matches({ foo: ['steak', 'bar']}));
t.ok(!f.matches({ foo: ['nihhh', 'rabbit']}));
t.end();
});
test('match false', function(t) {
var f = new ApproximateFilter({
attribute: 'foo',

View File

@ -78,6 +78,16 @@ test('match false', function(t) {
});
test('match any', function(t) {
var f = new SubstringFilter({
attribute: 'foo',
initial: 'bar'
});
t.ok(f);
t.ok(f.matches({ foo: ['beuha', 'barista']}));
t.end();
});
test('parse ok', function(t) {
var writer = new BerWriter();
writer.writeString('foo');