Skip to content

Commit

Permalink
Merge pull request #6 from lafama/master
Browse files Browse the repository at this point in the history
Boolean Property field filters
  • Loading branch information
brozeph committed Apr 27, 2014
2 parents 4fddc9f + ef9907c commit 20cddb2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
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

0 comments on commit 20cddb2

Please sign in to comment.