Skip to content

Commit

Permalink
add function compare
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Apr 20, 2016
1 parent 46a0c25 commit a8bef78
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/finding_data.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,25 @@ The search object can take a few forms depending on what you want to find:
{eyes: {is: [1, 2]}} // eyes is 1 OR 2
{friends: {includes: 'kevin'}} //friends array includes kevin

// Run a function to compare objects
{name: {func: function(field, objects){
return objects.filter(function(entry){
return entry[field] == "david"
})
}}}

results = db.where({name: 'david'}); // where name is david
results.length // 1
results[0].height // 105

results = db.where({name: {func: function(field, objects){
return objects.filter(function(entry){
return entry[field] == "david"
})
}}})
results.length // 1
results[0].height // 105

david = db.findOne({name: 'david'})
david.height // 105

Expand Down
3 changes: 3 additions & 0 deletions src/compares.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ module.exports =
matches: (field, value, objects) ->
objects.filter (entry) ->
(value.test entry[field])

func: (field, value, objects) ->
value(field, objects)
8 changes: 8 additions & 0 deletions tests/docs-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe('Code Used in the Docs', function(){
expect(results.length).to.equal(1);
expect(results[0].height).to.equal(105);

results = db.where({name: {func: function(field, objects){
return objects.filter(function(entry){
return entry[field] == "david"
})
}}})
expect(results.length).to.equal(1);
expect(results[0].height).to.equal(105);

david = db.findOne({name: 'david'})
expect(david.height).to.equal(105)

Expand Down
8 changes: 8 additions & 0 deletions tests/sodb-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ for caching in [true, false]
results = db.where({name: {matches: /kev/}})
expect(results.length).to.equal 1

it 'should support a function', ->
results = db.where({name: {func: (field, objects) ->
objects.filter (entry) ->
(entry[field] == entry[field])
}})

expect(results.length).to.equal db.count()

describe 'result manipulation', ->
it 'should order by', ->
ordered = db.order({name: {isnot: 'something new'}}, "age")
Expand Down

0 comments on commit a8bef78

Please sign in to comment.