Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds nocase query to wrap value in case insensitive regex #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var methods = ['get', 'post', 'put', 'delete'], // All HTTP methods, PATCH not c
'lte': query('lte'),
'ne': query('ne'),
'regex': query('regex'),
'nocase': query('regex'),
'in': query('in')
});
defaults = function() {
Expand Down Expand Up @@ -448,6 +449,10 @@ function filterable(props, subfilters) {
data = data.split(',');
}

if (filter_func === 'nocase') {
return subfilters[filter_func](new RegExp(data, 'i'), quer.where(field[0]));
}

return subfilters[filter_func](data, quer.where(field[0]));
},
contains: function(key, quer) {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,10 @@ exports.movies = [
_id: new ObjectId(),
title: "Title14",
year: 2009
},
{
_id: new ObjectId(),
title: "Godzilla",
year: 2014
}
];
10 changes: 10 additions & 0 deletions test/model.filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ describe('Model', function() {
done();
});
});
it('should filter fields using nocase', function(done) {
request(app)
.get('/api/movies?title__nocase=godzilla')
.end(function(err, res) {
res.body.forEach(function(movie) {
movie.title.should.include('Godzilla');
});
done();
});
});
it('should populate an objectId', function(done) {
request(app)
.get('/api/movies/' + movie1._id + '?populate=creator')
Expand Down