Skip to content
Browse files

Did some rebuilding to the new field operators. It's should now be mo…

…re stable
  • Loading branch information...
1 parent 9ccec72 commit 7c9414d345421d85b54b2aad7fa2ddb30359a393 @jfd committed Jan 19, 2010
Showing with 43 additions and 45 deletions.
  1. +43 −45 match.js
View
88 match.js
@@ -20,66 +20,42 @@ var Match = (function() {
}
function get_op_resolver(op, value_a) {
- var op_resolver = function() { throw NO_MATCH };
+ var op_resolver = function() { return true };
switch(op) {
case '=':
op_resolver = function(value_b, parent, state) {
- if(value_a == value_b) {
- var result = state.matched ? [] : [parent];
- state.matched = true;
- return result;
- }
- throw NO_MATCH;
+ if(value_a == value_b) return true;
+ return false;
}
break;
case '>':
op_resolver = function(value_b, parent, state) {
- if(value_a > value_b) {
- var result = state.matched ? [] : [parent];
- state.matched = true;
- return result;
- }
- throw NO_MATCH;
+ if(value_a > value_b) return true;
+ return false;
}
break;
case '<':
op_resolver = function(value_b, parent, state) {
- if(value_a < value_b) {
- var result = state.matched ? [] : [parent];
- state.matched = true;
- return result;
- }
- throw NO_MATCH;
+ if(value_a < value_b) return true;
+ return false;
}
break;
case '>=':
op_resolver = function(value_b, parent, state) {
- if(value_a >= value_b) {
- var result = state.matched ? [] : [parent];
- state.matched = true;
- return result;
- }
- throw NO_MATCH;
+ if(value_a >= value_b) return true;
+ return false;
}
break;
case '<=':
op_resolver = function(value_b, parent, state) {
- if(value_a <= value_b) {
- var result = state.matched ? [] : [parent];
- state.matched = true;
- return result;
- }
- throw NO_MATCH;
+ if(value_a <= value_b) return true;
+ return false;
}
break;
case '!':
op_resolver = function(value_b, parent, state) {
- if(value_a != value_b) {
- var result = state.matched ? [] : [parent];
- state.matched = true;
- return result;
- }
- throw NO_MATCH;
+ if(value_a != value_b) return true;
+ return false;
}
break;
}
@@ -206,25 +182,47 @@ var Match = (function() {
},
Object: function(obj) {
- var resolvers = {};
+ var resolvers = {},
+ has_ops = false;
for(var key in obj) {
var resolver, akey = key, op, m;
if((m = IS_FIELD_OP(key)) && (op = m[1])) {
+ has_ops = true;
akey = key.substr(0, key.length - op.length - 1);
resolver = get_op_resolver(op, obj[key]);
} else {
resolver = get_resolver(obj[key]);
}
resolvers[akey] = resolver;
}
- return function(value) {
- if(value === null || value === undefined) throw NO_MATCH;
- var result = [], state = {};
- for(var key in resolvers) {
- var resolver = resolvers[key];
- result = result.concat(resolver(value[key], value, state));
+ if(has_ops) {
+ return function(obj) {
+ if(obj === null || obj === undefined) throw NO_MATCH;
+ var result_a = [], result_b = [], match = null;
+ for(var key in resolvers) {
+ var resolver = resolvers[key];
+ if(resolver.is_op) {
+ match = resolver(obj[key])
+ } else if(match === null) {
+ result_a = result_a.concat(resolver(obj[key]));
+ } else {
+ result_b = result_b.concat(resolver(obj[key]));
+ }
+ }
+ if (match == false) throw NO_MATCH;
+ return match ? result_a.concat([obj]).concat(result_b) :
+ result_a.concat(result_b);
+ }
+ } else {
+ return function(obj) {
+ if(obj === null || obj === undefined) throw NO_MATCH;
+ var result = [];
+ for(var key in resolvers) {
+ var resolver = resolvers[key];
+ result = result.concat(resolver(obj[key]));
+ }
+ return result;
}
- return result;
}
}
}

0 comments on commit 7c9414d

Please sign in to comment.
Something went wrong with that request. Please try again.