Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
faster filter creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jan 29, 2016
1 parent 9ba624b commit 6dca54f
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function createFilter(filter) {
op === '!in' ? filter.slice(2) : filter.slice(1);

var opFn =
op === 'in' ? (key === '$type' ? inType : _in) :
op === 'in' ? (key === '$type' ? inType : inNormal) :
op === '!in' ? (key === '$type' ? notInType : notIn) :
op === '==' ? (key === '$type' ? equalsType : equals) :
op === '!=' ? (key === '$type' ? notEqualsType : notEquals) :
Expand All @@ -40,44 +40,52 @@ function createFilter(filter) {
op === '>=' ? greaterEqual :
val.map(createFilter);

if (op === 'any') {
return function (f) {
for (var i = 0; i < opFn.length; i++) {
if (opFn[i](f)) return true;
}
return false;
}
} else if (op === 'all') {
return function (f) {
for (var i = 0; i < opFn.length; i++) {
if (!opFn[i](f)) return false;
}
return true;
if (op === 'any') return createAny(opFn);
if (op === 'all') return createAll(opFn);
if (op === 'none') return createNone(opFn);
return createOp(opFn, key, val);
}

function createOp(opFn, key, val) {
return function (f) {
return opFn(f.properties || f.tags || {}, f.type, key, val);
}
}
function createAny(opFn) {
return function (f) {
for (var i = 0; i < opFn.length; i++) {
if (opFn[i](f)) return true;
}
} else if (op === 'none') {
return function (f) {
for (var i = 0; i < opFn.length; i++) {
if (opFn[i](f)) return false;
}
return true;
return false;
}
}
function createAll(opFn) {
return function (f) {
for (var i = 0; i < opFn.length; i++) {
if (!opFn[i](f)) return false;
}
return true;
}

}
function createNone(opFn) {
return function (f) {
return opFn(f.properties || f.tags || {}, f.type);
};

function _in(p, t) { return val.indexOf(p[key]) !== -1; }
function inType(p, t) { return val.indexOf(types[t]) !== -1; }
function notIn(p, t) { return val.indexOf(p[key]) === -1; }
function notInType(p, t) { return val.indexOf(types[t]) === -1; }
function equals(p, t) { return p[key] === val; }
function equalsType(p, t) { return t === typeLookup[val]; }
function notEquals(p, t) { return p[key] !== val; }
function notEqualsType(p, t) { return t !== typeLookup[val]; }
function lesser(p, t) { return typeof p[key] === typeof val && p[key] < val; }
function greater(p, t) { return typeof p[key] === typeof val && p[key] > val; }
function lesserEqual(p, t) { return typeof p[key] === typeof val && p[key] <= val; }
function greaterEqual(p, t) { return typeof p[key] === typeof val && p[key] >= val; }
function truth() { return true; }
for (var i = 0; i < opFn.length; i++) {
if (opFn[i](f)) return false;
}
return true;
}
}

function inNormal(p, t, key, val) { return val.indexOf(p[key]) !== -1; }
function inType(p, t, key, val) { return val.indexOf(types[t]) !== -1; }
function notIn(p, t, key, val) { return val.indexOf(p[key]) === -1; }
function notInType(p, t, key, val) { return val.indexOf(types[t]) === -1; }
function equals(p, t, key, val) { return p[key] === val; }
function equalsType(p, t, key, val) { return t === typeLookup[val]; }
function notEquals(p, t, key, val) { return p[key] !== val; }
function notEqualsType(p, t, key, val) { return t !== typeLookup[val]; }
function lesser(p, t, key, val) { return typeof p[key] === typeof val && p[key] < val; }
function greater(p, t, key, val) { return typeof p[key] === typeof val && p[key] > val; }
function lesserEqual(p, t, key, val) { return typeof p[key] === typeof val && p[key] <= val; }
function greaterEqual(p, t, key, val) { return typeof p[key] === typeof val && p[key] >= val; }
function truth() { return true; }

0 comments on commit 6dca54f

Please sign in to comment.