diff --git a/app/lib/LocalMongo.coffee b/app/lib/LocalMongo.coffee index e1c7faca2..9aaec133e 100644 --- a/app/lib/LocalMongo.coffee +++ b/app/lib/LocalMongo.coffee @@ -18,20 +18,21 @@ doQuerySelector = (value, operatorObj) -> when '$ne' then return false if mapred value, body, (l, r) -> l == r when '$in' then return false unless _.reduce value, ((result, val) -> result or val in body), false when '$nin' then return false if _.reduce value, ((result, val) -> result or val in body), false + else return false true - -LocalMongo.doLogicalOperator = (target, operatorObj) -> - for operator, body of operatorObj - switch operator - when '$or' then return false unless _.reduce body (res, query) -> res or matchesQuery target query, false - when '$and' then return false unless _.reduce body (res, query) -> res and matchesQuery target query, true - - -LocalMongo.matchesQuery = (target, queryObj) => +matchesQuery = (target, queryObj) -> for prop, query of queryObj - return false unless prop of target - if typeof query != 'object' or _.isArray query - return false unless target[prop] == query or (query in target[prop] if _.isArray target[prop]) - else return false unless doQuerySelector(target[prop], query) - true \ No newline at end of file + if prop[0] == '$' + switch prop + when '$or' then return false unless _.reduce query, ((res, obj) -> res or matchesQuery target, obj), false + when '$and' then return false unless _.reduce query, ((res, obj) -> res and matchesQuery target, obj), true + else return false + else + return false unless prop of target + if typeof query != 'object' or _.isArray query + return false unless target[prop] == query or (query in target[prop] if _.isArray target[prop]) + else return false unless doQuerySelector target[prop], query + true + +LocalMongo.matchesQuery = matchesQuery \ No newline at end of file diff --git a/test/app/lib/local_mongo.spec.coffee b/test/app/lib/local_mongo.spec.coffee index e87db27b8..9d74d4795 100644 --- a/test/app/lib/local_mongo.spec.coffee +++ b/test/app/lib/local_mongo.spec.coffee @@ -31,6 +31,7 @@ describe 'Local Mongo queries', -> expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gte': 9001)).toBeFalsy() expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gte': 9000)).toBeTruthy() expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gte': [9000, 10000])).toBeTruthy() + expect(LocalMongo.matchesQuery(this.fixture1, 'levels': '$gte': [21, 30])).toBeTruthy() it '$lt selector', -> expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lt': 9001)).toBeTruthy() @@ -60,3 +61,11 @@ describe 'Local Mongo queries', -> expect(LocalMongo.matchesQuery(this.fixture1, 'type': '$nin': ['cats', 'dogs'])).toBeTruthy() expect(LocalMongo.matchesQuery(this.fixture1, 'likes': '$nin': ['popcorn', 'chicken'])).toBeFalsy() + it '$or operator', -> + expect(LocalMongo.matchesQuery(this.fixture1, $or: [{value:9000}, {type:'zebra'}])).toBeTruthy() + expect(LocalMongo.matchesQuery(this.fixture1, $or: [{value:9001}, {worth:$lt:10}])).toBeTruthy() + + it '$and operator', -> + expect(LocalMongo.matchesQuery(this.fixture1, $and: [{value:9000}, {type:'zebra'}])).toBeFalsy() + expect(LocalMongo.matchesQuery(this.fixture1, $and: [{value:9000}, {type:'unicorn'}])).toBeTruthy() + expect(LocalMongo.matchesQuery(this.fixture1, $and: [{value:$gte:9000}, {worth:$lt:10}])).toBeTruthy() \ No newline at end of file