codecombat/app/lib/LocalMongo.coffee

29 lines
1.1 KiB
CoffeeScript
Raw Normal View History

2014-05-13 15:09:54 -04:00
LocalMongo = module.exports
LocalMongo.doQuerySelector = (value, operatorObj) ->
for operator, body of operatorObj
switch operator
when '$gt' then return false unless value > body
when '$gte' then return false unless value >= body
when '$in' then return false unless value in body
when '$lt' then return false unless value < body
when '$lte' then return false unless value <= body
when '$ne' then return false unless value != body
when '$nin' then return false if value in body
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 x
when '$and' then return false unless _.reduce body (res, query) -> res and matchesQuery target query
#when '$not' then return false if
LocalMongo.matchesQuery = (target, query) ->
for key, value of query
return false unless key in target
if typeof value != 'object'
return false unless target[key] == value
else return false unless doQuerySelector value query[key]