Enhance comparators for memory connector

This commit is contained in:
Raymond Feng 2014-06-06 08:19:41 -07:00
parent 04e1256b8b
commit a0a9fae9c6
1 changed files with 46 additions and 9 deletions

View File

@ -377,24 +377,61 @@ function applyFilter(filter) {
}
if (example.inq) {
if (!value) return false;
for (var i = 0; i < example.inq.length; i += 1) {
if (example.inq[i] == value) return true;
// if (!value) return false;
for (var i = 0; i < example.inq.length; i++) {
if (example.inq[i] == value) {
return true;
}
}
return false;
}
if (isNum(example.gt) && example.gt < value) return true;
if (isNum(example.gte) && example.gte <= value) return true;
if (isNum(example.lt) && example.lt > value) return true;
if (isNum(example.lte) && example.lte >= value) return true;
if (testInEquality(example, value)) {
return true;
}
}
// not strict equality
return (example !== null ? example.toString() : example) == (value !== null ? value.toString() : value);
}
function isNum(n) {
return typeof n === 'number';
/**
* Compare two values
* @param {*} val1 The 1st value
* @param {*} val2 The 2nd value
* @returns {number} 0: =, postive: >, negative <
* @private
*/
function compare(val1, val2) {
if (typeof val1 === 'number') {
return val1 - val2;
}
if (typeof val1 === 'string') {
return (val1 > val2) ? 1 : ((val1 < val2) ? -1 : 0);
}
if (typeof val1 === 'boolean') {
return val1 - val2;
}
if (val1 instanceof Date) {
return val1.getTime() - ((val2 && val2.getTime()) || 0);
}
// Return NaN if we don't know how to compare
return (val1 === val2) ? 0 : NaN;
}
function testInEquality(example, val) {
if ('gt' in example) {
return compare(example.gt, val) > 0;
}
if ('gte' in example) {
return compare(example.gte, val) >= 0;
}
if ('lt' in example) {
return compare(example.lt, val) < 0;
}
if ('lte' in example) {
return compare(example.lte, val) <= 0;
}
return false;
}
}