Skip to content

Commit

Permalink
add refineSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Apr 20, 2016
1 parent 0213b68 commit df27ed5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/sodb.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,38 @@ module.exports =
# Only called if the cache needs a new value.
#
findResults: (search) ->
results = @objects
results = @runSearch(@objects, search)

return results.map @unref

#
# runSearch(results, search)
#
# results - the array of objects to search
# search - array of search objects
#
runSearch: (results, search) ->
for condition in search
field = Object.keys(condition)[0]
compare = Object.keys(condition[field])[0]
results = @runCondition(field, compare, condition[field][compare], results)

return results.map @unref
return results

#
# refineSearch(results, search...)
#
# results - results from a previous sodb query
# search... - search objects
#
refineSearch: ->
args = Array.prototype.slice.call(arguments)
results = args.shift()
search = args.map(@expandQuery)

refined = @runSearch(results, @expandQuery(search))

return refined.map @unref

#
# findOne(search...)
Expand Down
9 changes: 9 additions & 0 deletions tests/sodb-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ for caching in [true, false]
it 'should have a dbrevision of not 0', ->
expect(db.dbRevision).not.to.equal 0

describe 'Results', ->
it 'should let you refine your results', ->
results = db.where({eyes: 2})

res = db.refineSearch(results, {age: 20})

expect(res.length).to.equal 1


describe 'JSON', ->
[db, json] = []

Expand Down

0 comments on commit df27ed5

Please sign in to comment.