Merge pull request #41 from athoune/master

filtering against multiple values unified
This commit is contained in:
Mark Cavage 2011-12-07 13:44:50 -08:00
commit 959fa2587b
6 changed files with 47 additions and 35 deletions

View File

@ -47,20 +47,14 @@ EqualityFilter.prototype.matches = function(target) {
if (typeof(target) !== 'object') if (typeof(target) !== 'object')
throw new TypeError('target (object) required'); throw new TypeError('target (object) required');
var matches = false;
if (target.hasOwnProperty(this.attribute)) { if (target.hasOwnProperty(this.attribute)) {
if (Array.isArray(target[this.attribute])) { var value = this.value;
for (var i = 0; i < target[this.attribute].length; i++) { return Filter.multi_test(
matches = (this.value === target[this.attribute][i]); function(v) { return value === v; },
if (matches) target[this.attribute]);
break;
}
} else {
matches = (this.value === target[this.attribute]);
}
} }
return matches; return false;
}; };

View File

@ -48,21 +48,14 @@ GreaterThanEqualsFilter.prototype.matches = function(target) {
if (typeof(target) !== 'object') if (typeof(target) !== 'object')
throw new TypeError('target (object) required'); throw new TypeError('target (object) required');
var matches = false;
if (target.hasOwnProperty(this.attribute)) { if (target.hasOwnProperty(this.attribute)) {
if (Array.isArray(target[this.attribute])) { var value = this.value;
for (var i = 0; i < target[this.attribute].length; i++) { return Filter.multi_test(
matches = (this.value <= target[this.attribute][i]); function(v) { return value <= v; },
if (matches) target[this.attribute]);
break;
}
} else {
matches = (this.value <= target[this.attribute]);
}
} }
return matches; return false;
}; };

View File

@ -48,21 +48,14 @@ LessThanEqualsFilter.prototype.matches = function(target) {
if (typeof(target) !== 'object') if (typeof(target) !== 'object')
throw new TypeError('target (object) required'); throw new TypeError('target (object) required');
var matches = false;
if (target.hasOwnProperty(this.attribute)) { if (target.hasOwnProperty(this.attribute)) {
if (Array.isArray(target[this.attribute])) { var value = this.value;
for (var i = 0; i < target[this.attribute].length; i++) { return Filter.multi_test(
matches = (this.value >= target[this.attribute][i]); function(v) { return value >= v; },
if (matches) target[this.attribute]);
break;
}
} else {
matches = (this.value >= target[this.attribute]);
}
} }
return false;
return matches;
}; };

View File

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

View File

@ -57,6 +57,16 @@ test('match true', function(t) {
}); });
test('match multiple', function(t) {
var f = new GreaterThanEqualsFilter({
attribute: 'foo',
value: 'bar'
});
t.ok(f);
t.ok(f.matches({ foo: ['beuha', 'baz'] }));
t.end();
});
test('match false', function(t) { test('match false', function(t) {
var f = new GreaterThanEqualsFilter({ var f = new GreaterThanEqualsFilter({
attribute: 'foo', attribute: 'foo',

View File

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