From ef043af1d432c63605727fef52f3d8790cf55176 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Sun, 4 Dec 2011 22:32:22 +0100 Subject: [PATCH] SubstringFilter can match against an array. --- lib/filters/filter.js | 16 ++++++++++++++++ lib/filters/substr_filter.js | 2 +- tst/filters/substr.test.js | 10 ++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/filters/filter.js b/lib/filters/filter.js index 75c146b..e25b42d 100644 --- a/lib/filters/filter.js +++ b/lib/filters/filter.js @@ -56,3 +56,19 @@ 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; + value.forEach(function(v) { + if(rule(v)) { response = true; } + }); + return response; + } else { + return rule(value); + } +} diff --git a/lib/filters/substr_filter.js b/lib/filters/substr_filter.js index 74a186d..10a107e 100644 --- a/lib/filters/substr_filter.js +++ b/lib/filters/substr_filter.js @@ -81,7 +81,7 @@ 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; diff --git a/tst/filters/substr.test.js b/tst/filters/substr.test.js index cf10de4..a6201f8 100644 --- a/tst/filters/substr.test.js +++ b/tst/filters/substr.test.js @@ -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');