From b0044f9bf0c1fe27b2e688e529b2884643f09286 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Sun, 4 Dec 2011 23:23:39 +0100 Subject: [PATCH] ApproximateFilter can match multiple values. --- lib/filters/approx_filter.js | 10 ++++++++-- tst/filters/approx.test.js | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/filters/approx_filter.js b/lib/filters/approx_filter.js index 9647a42..21a432d 100644 --- a/lib/filters/approx_filter.js +++ b/lib/filters/approx_filter.js @@ -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; }; diff --git a/tst/filters/approx.test.js b/tst/filters/approx.test.js index 1f27df2..78cd5f1 100644 --- a/tst/filters/approx.test.js +++ b/tst/filters/approx.test.js @@ -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',