mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-27 09:35:39 -05:00
Added '$or' and '$and' to Local Mongo
This commit is contained in:
parent
b3bddbd785
commit
e177912a5a
2 changed files with 24 additions and 14 deletions
|
@ -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
|
||||
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
|
|
@ -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()
|
Loading…
Reference in a new issue