Skip to content

Commit

Permalink
Merge pull request #10 from PlayNetwork/v0.2.8
Browse files Browse the repository at this point in the history
V0.2.8
  • Loading branch information
brozeph committed Aug 26, 2014
2 parents 4c104ba + e87573d commit e08edac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 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.8 / 2014-08-26

* Modifying how Mongoose `#where` method is used in filters to be compliant with Mongoose 3.x

# 0.2.7 / 2014-06-28

* Removing testing on Node v0.8 from Travis CI
Expand Down
23 changes: 13 additions & 10 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = function(mongoose) {
return str.replace(/\W\s/ig, '\\$&');
}


mongoose.Query.prototype.filter = function (options) {
if (!options || !options.filters) {
return this;
Expand All @@ -22,13 +21,9 @@ module.exports = function(mongoose) {
var applyWhere = function (spec, buildRegex) {
for (var key in spec) {
if (spec.hasOwnProperty(key)) {
var val = spec[key];
var val = buildRegex(spec[key]);

if (typeof val === 'string') {
self.where(key).regex(buildRegex(val));
} else {
self.where(key).equals(buildRegex(val));
}
self.where(key, val);
}
}
};
Expand All @@ -47,9 +42,11 @@ module.exports = function(mongoose) {
item[key] = buildRegex(val);
items.push(item);
});

return items;
} else {
item[key] = buildRegex(value);

return item;
}
};
Expand Down Expand Up @@ -97,15 +94,21 @@ module.exports = function(mongoose) {
};

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

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

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

// MANDATORY
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.7",
"version": "0.2.8",
"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
11 changes: 2 additions & 9 deletions test/lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,8 @@ describe('filter', function () {
}
};

mongoose.Query.prototype.where = function (key) {
return {
regex : function (value) {
whereClause[key] = value;
},
equals : function (value) {
whereClause[key] = value;
}
}
mongoose.Query.prototype.where = function (key, val) {
whereClause[key] = val;
};
});

Expand Down

0 comments on commit e08edac

Please sign in to comment.