diff --git a/docs/finding_data.markdown b/docs/finding_data.markdown index f49f36a..02af2c1 100644 --- a/docs/finding_data.markdown +++ b/docs/finding_data.markdown @@ -10,6 +10,7 @@ The search object can take a few forms depending on what you want to find: {name: 'kevin'} // Will be expanded to is (see next line) {name: {is: 'kevin'}} // name is kevin {name: {isnot: 'kevin'}} // name is not kevin +{name: {matches: /kev/}} // name matches the pattern /kev/ {height: {gt: 100}} // height is greater than 100 {height: {lt: 90}} // height is less than 90 {eyes: {is: [1, 2]}} // eyes is 1 OR 2 diff --git a/src/compares.coffee b/src/compares.coffee index bb9bdde..8460ae2 100644 --- a/src/compares.coffee +++ b/src/compares.coffee @@ -35,3 +35,7 @@ module.exports = includes: (field, value, objects) -> objects.filter (entry) -> (entry[field].indexOf(value) != -1) + + matches: (field, value, objects) -> + objects.filter (entry) -> + (value.test entry[field]) diff --git a/tests/sodb-tests.coffee b/tests/sodb-tests.coffee index 1275052..bfa46d4 100644 --- a/tests/sodb-tests.coffee +++ b/tests/sodb-tests.coffee @@ -99,6 +99,10 @@ for caching in [true, false] results = db.where({likes: {includes: 'bananas'}}) expect(results.length).to.equal 4 + it 'should support matches', -> + results = db.where({name: {matches: /kev/}}) + expect(results.length).to.equal 1 + describe 'result manipulation', -> it 'should order by', -> ordered = db.order({name: {isnot: 'something new'}}, "age")