Merge pfmooney/filter-case-attrs

Fix mcavage/node-ldapjs#155
This commit is contained in:
Patrick Mooney 2014-01-17 15:58:32 -06:00
commit 6414682eee
9 changed files with 105 additions and 14 deletions

View File

@ -50,12 +50,13 @@ ApproximateFilter.prototype.matches = function (target) {
throw new TypeError('target (object) required');
var matches = false;
if (target.hasOwnProperty(this.attribute)) {
var tv = target[this.attribute];
var tv = Filter.get_attr_caseless(target, this.attribute);
if (tv !== null) {
if (Array.isArray(tv)) {
matches = (tv.indexOf(this.value) != -1);
} else {
matches = (this.value === target[this.attribute]);
matches = (this.value === tv);
}
}

View File

@ -50,8 +50,9 @@ EqualityFilter.prototype.matches = function (target) {
throw new TypeError('target (object) required');
var self = this;
var tv = Filter.get_attr_caseless(target, this.attribute);
if (target.hasOwnProperty(this.attribute)) {
if (tv !== null) {
var value = this.value;
return Filter.multi_test(
function (v) {
@ -59,7 +60,7 @@ EqualityFilter.prototype.matches = function (target) {
v = v.toLowerCase();
return value === v;
},
target[this.attribute]);
tv);
}
return false;

View File

@ -94,7 +94,9 @@ ExtensibleFilter.prototype.matches = function (target) {
return false;
var self = this;
if (this.matchType && target.hasOwnProperty(this.matchType)) {
var tv = Filter.get_attr_caseless(target, this.matchType);
if (this.matchType && tv !== null) {
if (self.rule === '2.5.13.4' || self.rule === 'caseIgnoreSubstringsMatch') {
var f = {
@ -120,7 +122,7 @@ ExtensibleFilter.prototype.matches = function (target) {
}
return self.value === v;
}, target[this.matchType]);
}, tv);
}
return false;

View File

@ -73,3 +73,22 @@ Filter.multi_test = function (rule, value) {
return rule(value);
}
};
// Search object for attribute, insensitive to case
Filter.get_attr_caseless = function (target, attr) {
// Check for exact case match first
if (target.hasOwnProperty(attr)) {
return target[attr];
}
// Perform case-insensitive enumeration after that
var lower = attr.toLowerCase();
var result = null;
Object.getOwnPropertyNames(target).some(function (item) {
if (item.toLowerCase() == lower) {
result = target[item];
return true;
}
return false;
});
return result;
};

View File

@ -50,11 +50,13 @@ GreaterThanEqualsFilter.prototype.matches = function (target) {
if (typeof (target) !== 'object')
throw new TypeError('target (object) required');
if (target.hasOwnProperty(this.attribute)) {
var tv = Filter.get_attr_caseless(target, this.attribute);
if (tv !== null) {
var value = this.value;
return Filter.multi_test(
function (v) { return value <= v; },
target[this.attribute]);
tv);
}
return false;

View File

@ -50,11 +50,13 @@ LessThanEqualsFilter.prototype.matches = function (target) {
if (typeof (target) !== 'object')
throw new TypeError('target (object) required');
if (target.hasOwnProperty(this.attribute)) {
var tv = Filter.get_attr_caseless(target, this.attribute);
if (tv !== null) {
var value = this.value;
return Filter.multi_test(
function (v) { return value >= v; },
target[this.attribute]);
tv);
}
return false;

View File

@ -44,7 +44,7 @@ PresenceFilter.prototype.matches = function (target) {
if (typeof (target) !== 'object')
throw new TypeError('target (object) required');
return target.hasOwnProperty(this.attribute);
return (Filter.get_attr_caseless(target, this.attribute) !== null);
};

View File

@ -75,7 +75,9 @@ SubstringFilter.prototype.matches = function (target) {
if (typeof (target) !== 'object')
throw new TypeError('target (object) required');
if (target.hasOwnProperty(this.attribute)) {
var tv = Filter.get_attr_caseless(target, this.attribute);
if (tv !== null) {
var re = '';
if (this.initial)
re += '^' + escapeRegExp(this.initial) + '.*';
@ -95,7 +97,7 @@ SubstringFilter.prototype.matches = function (target) {
v = v.toLowerCase();
return matcher.test(v);
},
target[this.attribute]);
tv);
}
return false;

View File

@ -0,0 +1,62 @@
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var test = require('tap').test;
///--- Globals
var Filter;
///--- Tests
test('load library', function (t) {
var filters = require('../../lib/index').filters;
t.ok(filters);
Filter = filters.Filter;
t.ok(Filter);
t.end();
});
test('multi_test array', function (t) {
var rule = function (item) {
return (item == 3);
};
t.ok(Filter.multi_test(rule, [1, 2, 3]));
t.ok(!Filter.multi_test(rule, [1, 2]));
t.end();
});
test('multi_test value', function (t) {
var rule = function (item) {
return (item == 3);
};
t.ok(Filter.multi_test(rule, 3));
t.ok(!Filter.multi_test(rule, 1));
t.end();
});
test('get_attr_caseless exact match', function (t) {
var f = Filter.get_attr_caseless;
t.equal(f({attr: 'testval'}, 'attr'), 'testval');
t.equal(f({attr: 'testval'}, 'missing'), null);
t.end();
});
test('get_attr_caseless insensitive match', function (t) {
var f = Filter.get_attr_caseless;
var data = {
lower: 'lower',
UPPER: 'upper',
MiXeD: 'mixed'
};
t.equal(f(data, 'lower'), 'lower');
t.equal(f(data, 'upper'), 'upper');
t.equal(f(data, 'mixed'), 'mixed');
t.equal(f(data, 'missing'), null);
t.end();
});