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

Boolean Property field filters #6

Merged
merged 2 commits into from
Apr 27, 2014
Merged
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
8 changes: 4 additions & 4 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function(mongoose) {
for (var key in spec) {
if (spec.hasOwnProperty(key)) {
var val = spec[key];
self.where(key).regex(buildRegex(val));
self.where(key).regex(buildRegex(val));
}
}
};
Expand Down Expand Up @@ -92,15 +92,15 @@ module.exports = function(mongoose) {
};

var regexContains = function (val) {
return new RegExp(sanitize(val), 'i');
return (typeof val)==='string' ? new RegExp(sanitize(val), 'i'):val;
};

var regexStartsWith = function (val) {
return new RegExp('^' + sanitize(val), 'i');
return (typeof val)==='string' ? new RegExp('^' + sanitize(val), 'i'): val;
};

var regexExact = function (val) {
return new RegExp('^' + sanitize(val) + '$', 'i');
return (typeof val)==='string' ?new RegExp('^' + sanitize(val) + '$', 'i'): val;
};

// MANDATORY
Expand Down
1 change: 1 addition & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ global.Kitteh = mongoose.model(
color : String,
isFurreh : Boolean
},
isDead: Boolean,
home : String,
name : String,
peePatches : [String]
Expand Down
43 changes: 43 additions & 0 deletions test/lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ describe('filter', function () {
whereClause.name.test('cat litter').should.equals(false);
whereClause.name.test('the cat').should.equals(false);
});

it ('should look for occurrences of an exact match of the object when using exact', function () {
var options = {
filters : {
mandatory : {
exact : {
isDead : false
}
}
}
};

var query = Kitteh
.find()
.filter(options);


should.exist(query);
should.exist(whereClause.isDead);
whereClause.isDead.should.equals(false);
});
});

describe('optional filters', function () {
Expand Down Expand Up @@ -195,6 +216,28 @@ describe('filter', function () {
orClauseItems[0][0].name.test('the cat').should.equals(false);
});

it ('should look for occurrences of an exact match of the object when using exact', function () {
var options = {
filters : {
optional : {
exact : {
isDead : true
}
}
}
};

var query = Kitteh
.find()
.filter(options);

should.exist(query);
orClauseItems.should.have.length(1);
orClauseItems[0][0].isDead.should.equals(true);
//orClauseItems[0][0].name.test('cat litter').should.equals(false);
//orClauseItems[0][0].name.test('the cat').should.equals(false);
});

it ('should look for multiple occurrences of a match when supplying an array', function () {
var options = {
filters : {
Expand Down