Skip to content

Commit

Permalink
Merge pull request #13 from PlayNetwork/develop
Browse files Browse the repository at this point in the history
Preparing release v0.2.9
  • Loading branch information
brozeph committed Aug 29, 2014
2 parents 5c52c67 + 479b9bd commit 13c86fd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.2.9 / 2014-08-28

* Adding support for `endsWith` optional and mandatory filters

# 0.2.8 / 2014-08-26

* Modifying how Mongoose `#where` method is used in filters to be compliant with Mongoose 3.x
Expand Down
10 changes: 10 additions & 0 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ module.exports = function(mongoose) {
self.or(items);
}

items = parse(optional.endsWith, regexEndsWith);
if (items && items instanceof Array && items.length) {
self.or(items);
}

items = parse(optional.startsWith, regexStartsWith);
if (items && items instanceof Array && items.length) {
self.or(items);
Expand All @@ -99,6 +104,10 @@ module.exports = function(mongoose) {
val);
};

var regexEndsWith = function (val) {
return new RegExp(sanitize(val) + '$', 'i');
};

var regexStartsWith = function (val) {
return (typeof val === 'string' ?
new RegExp('^' + sanitize(val), 'i') :
Expand All @@ -113,6 +122,7 @@ module.exports = function(mongoose) {

// MANDATORY
applyWhere(mandatory.contains, regexContains);
applyWhere(mandatory.endsWith, regexEndsWith);
applyWhere(mandatory.startsWith, regexStartsWith);
applyWhere(mandatory.exact, regexExact);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongoose-middleware",
"description": "Middleware for mongoose that makes filtering, sorting, pagination and projection chainable and simple to apply",
"version": "0.2.8",
"version": "0.2.9",
"scripts": {
"pretest": "rm -rf lib-cov ; jshint lib/*.js ; jscoverage lib lib-cov",
"test": "mocha --check-leaks -R spec -r ./test/common.js -u bdd ./test/lib/*.js",
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Filters can be used in three ways: mandatory, optional and keyword searches. Add
* `exact` - Matches the string letter for letter, but is not case sensitive
* `contains` - Matches documents where the string exists as a substring of the field (similar to a where field like '%term%' query in a relational datastore)
* `startsWith` - Matches documents where field begins with the string supplied (similar to a where field like 'term%' query in a relational datastore)
* `endsWith` - Matches documents where field ends with the string supplied (similar to a where field like '%term' query in a relational datastore)

#### Mandatory

Expand Down Expand Up @@ -318,7 +319,7 @@ var options = {

### Pagination

Pagination is performed by swapping the `exec()` or `execFind()` function of Mongoose with `page()`. Pagination may be specified as follows:
Pagination is performed by swapping the `exec()` function of Mongoose with `page()`. Pagination may be specified as follows:

```Javascript
var options = {
Expand Down
44 changes: 44 additions & 0 deletions test/lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ describe('filter', function () {
whereClause.name.test('dog').should.equals(false);
});

it ('should look for occurrences of a term at the start of a string using endsWith', function () {
var options = {
filters : {
mandatory : {
endsWith : {
name : 'cat'
}
}
}
};

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

should.exist(query);
should.exist(whereClause.name);
whereClause.name.test('cat').should.equals(true);
whereClause.name.test('cool cat').should.equals(true);
whereClause.name.test('this cat is sick').should.equals(false);
});

it ('should look for occurrences of a term at the start of a string using startsWith', function () {
var options = {
filters : {
Expand Down Expand Up @@ -167,6 +189,28 @@ describe('filter', function () {
orClauseItems[0][0].name.test('dog').should.equals(false);
});

it ('should look for occurrences of a term at the start of a string using endsWith', function () {
var options = {
filters : {
optional : {
endsWith : {
name : 'cat'
}
}
}
};

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

should.exist(query);
orClauseItems.should.have.length(1);
orClauseItems[0][0].name.test('cat').should.equals(true);
orClauseItems[0][0].name.test('cool cat').should.equals(true);
orClauseItems[0][0].name.test('this cat is sick').should.equals(false);
});

it ('should look for occurrences of a term at the start of a string using startsWith', function () {
var options = {
filters : {
Expand Down

0 comments on commit 13c86fd

Please sign in to comment.