Skip to content

Commit

Permalink
Merge pull request #23 from PlayNetwork/v0.2.15
Browse files Browse the repository at this point in the history
V0.2.15
  • Loading branch information
Stanford Chiang committed Mar 4, 2016
2 parents 5d9afb9 + 8224698 commit 76f2849
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 76 deletions.
5 changes: 5 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.2.15 / 2016-03-04

* Fixed issue where there was an incompatibility with mquery module in mongoose
* Updated dependencies

# v0.2.14 / 2016-02-22

* Increasing code coverage of unit tests with minor refactors
Expand Down
147 changes: 87 additions & 60 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,101 @@ module.exports = function(mongoose) {
optional = options.filters.optional || {},
self = this;

var applyGreaterThan = function (spec, clause) {
var analyzeWhereSpec = function (val) {
if (typeof val === 'string') {
switch (val.toLowerCase()) {
case 'null' : return null;
case 'true' : return true;
case 'false' : return false;
default :
if (!isNaN(val)) {
// val is a number
if (val.indexOf('.') > -1) {
// val is a float
return parseFloat(val);
} else {
return parseInt(val);
}
}
}
}

return val;
};

var applyGreaterThan = function (spec, isOptional) {
if (typeof isOptional === 'undefined') {
isOptional = false;
}

for (var key in spec) {
if (spec.hasOwnProperty(key)) {
clause(key).gt(spec[key]);
(isOptional ? self.or(key) : self.where(key)).gt(spec[key]);
}
}
};

var applyGreaterThanEqual = function (spec, clause) {
var applyGreaterThanEqual = function (spec, isOptional) {
if (typeof isOptional === 'undefined') {
isOptional = false;
}

for (var key in spec) {
if (spec.hasOwnProperty(key)) {
clause(key).gte(spec[key]);
(isOptional ? self.or(key) : self.where(key)).gte(spec[key]);
}
}
};

var applyLesserThan = function (spec, clause) {
var applyLesserThan = function (spec, isOptional) {
if (typeof isOptional === 'undefined') {
isOptional = false;
}

for (var key in spec) {
if (spec.hasOwnProperty(key)) {
clause(key).lt(spec[key]);
(isOptional ? self.or(key) : self.where(key)).lt(spec[key]);
}
}
};

var applyLesserThanEqual = function (spec, clause) {
var applyLesserThanEqual = function (spec, isOptional) {
if (typeof isOptional === 'undefined') {
isOptional = false;
}

for (var key in spec) {
if (spec.hasOwnProperty(key)) {
clause(key).lte(spec[key]);
(isOptional ? self.or(key) : self.where(key)).lte(spec[key]);
}
}
};

var applyNotEqual = function (spec, clause) {
var applyNotEqual = function (spec, isOptional) {
if (typeof isOptional === 'undefined') {
isOptional = false;
}

for (var key in spec) {
if (spec.hasOwnProperty(key)) {
clause(key).ne(parseWhereSpec(spec[key]));
(isOptional ? self.or(key) : self.where(key))
.ne(analyzeWhereSpec(spec[key]));
}
}
};

var applyRegex = function (spec, buildRegex, clause) {
var applyRegex = function (spec, buildRegex, isOptional) {
if (typeof isOptional === 'undefined') {
isOptional = false;
}

var bulkApply = function (key, val) {
console.log('bulk applying key: ', key);
val.forEach(function (term) {
clause(key, term);
if (isOptional) {
self.or(key, term);
} else {
self.where(key, term);
}
});
};

Expand All @@ -71,32 +121,14 @@ module.exports = function(mongoose) {
if (Array.isArray(val)) {
bulkApply(key, val);
} else {
clause(key, val);
}
}
}
};

var parseWhereSpec = function (val) {
if (typeof val === 'string') {
switch (val.toLowerCase()) {
case 'null' : return null;
case 'true' : return true;
case 'false' : return false;
default :
if (!isNaN(val)) {
// val is a number
if (val.indexOf('.') > -1) {
// val is a float
return parseFloat(val);
} else {
return parseInt(val);
}
if (isOptional) {
self.or(key, val);
} else {
self.where(key, val);
}
}
}
}

return val;
};

var regexContains = function (val) {
Expand Down Expand Up @@ -130,7 +162,7 @@ module.exports = function(mongoose) {
});
}

val = parseWhereSpec(val);
val = analyzeWhereSpec(val);

if (typeof val === 'string') {
return new RegExp('^' + sanitize(val) + '$', 'i');
Expand All @@ -150,48 +182,43 @@ module.exports = function(mongoose) {
};

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

applyGreaterThan(
mandatory.greaterThan || mandatory.gt || {},
self.where);
mandatory.greaterThan || mandatory.gt || {});
applyGreaterThanEqual(
mandatory.greaterThanEqual || mandatory.gte || {},
self.where);
mandatory.greaterThanEqual || mandatory.gte || {});
applyLesserThan(
mandatory.lessThan || mandatory.lt || {},
self.where);
mandatory.lessThan || mandatory.lt || {});
applyLesserThanEqual(
mandatory.lessThanEqual || mandatory.lte || {},
self.where);
mandatory.lessThanEqual || mandatory.lte || {});
applyNotEqual(
mandatory.notEqual || mandatory.notEqualTo || mandatory.ne || {},
self.where);
mandatory.notEqual || mandatory.notEqualTo || mandatory.ne || {});

// OPTIONAL
applyRegex(optional.contains, regexContains, self.or);
applyRegex(optional.endsWith, regexEndsWith, self.or);
applyRegex(optional.startsWith, regexStartsWith, self.or);
applyRegex(optional.exact, regexExact, self.or);
applyRegex(optional.contains, regexContains, true);
applyRegex(optional.endsWith, regexEndsWith, true);
applyRegex(optional.startsWith, regexStartsWith, true);
applyRegex(optional.exact, regexExact, true);

applyGreaterThan(
optional.greaterThan || optional.gt || {},
self.or);
true);
applyGreaterThanEqual(
optional.greaterThanEqual || optional.gte || {},
self.or);
true);
applyLesserThan(
optional.lessThan || optional.lt || {},
self.or);
true);
applyLesserThanEqual(
optional.lessThanEqual || optional.lte || {},
self.or);
true);
applyNotEqual(
optional.notEqual || optional.notEqualTo || optional.ne || {},
self.or);
true);

return self;
};
Expand Down
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
{
"name": "mongoose-middleware",
"description": "Middleware for mongoose that makes filtering, sorting, pagination and projection chainable and simple to apply",
"version": "0.2.14",
"version": "0.2.15",
"scripts": {
"test": "gulp test-all"
},
"devDependencies": {
"chai": "^1.10.0",
"del": "^1.1.1",
"gulp": "^3.8.10",
"gulp-coveralls": "^0.1.3",
"gulp-istanbul": "^0.5.0",
"gulp-jshint": "^1.9.0",
"gulp-mocha": "^2.0.0",
"jshint-stylish": "^1.0.0",
"mongoose": "~3.8.21",
"run-sequence": "^1.0.2"
"chai": "^3.5.0",
"del": "^2.2.0",
"gulp": "^3.9.1",
"gulp-coveralls": "^0.1.4",
"gulp-istanbul": "^0.10.3",
"gulp-jshint": "^2.0.0",
"gulp-mocha": "^2.2.0",
"jshint": "^2.9.1",
"jshint-stylish": "^2.1.0",
"mongoose": "^4.4.6",
"run-sequence": "^1.1.5"
},
"keywords": [
"mongo",
"mongoose",
"mongoose middlware",
"mongoose-middleware"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git@github.com:PlayNetwork/mongoose-middleware.git"
Expand Down
2 changes: 1 addition & 1 deletion test/lib/keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('keyword', function () {

should.exist(query);
orClauseItems[0].should.have.length(2);
orClauseItems[0][1].peePatches.$in.should.exist();
should.exist(orClauseItems[0][1].peePatches.$in);
});

it ('should search for keyword occurrences with multiple words', function () {
Expand Down
9 changes: 5 additions & 4 deletions test/lib/page.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* jshint -W030 */ // allows use of .empty
var
chai = require('chai'),
should = chai.should();
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('page', function () {
.find()
.page(null, function (err, data) {
should.not.exist(err);
data.should.not.be.empty();
data.should.not.be.empty;
skip.should.equals(0);

return done();
Expand All @@ -78,7 +79,7 @@ describe('page', function () {
.find()
.page(null, function (err, data) {
should.not.exist(err);
data.should.not.be.empty();
data.should.not.be.empty;
limit.should.equals(25);
skip.should.equals(0);

Expand All @@ -98,7 +99,7 @@ describe('page', function () {
.find()
.page(options, function (err, data) {
should.not.exist(err);
data.should.not.be.empty();
data.should.not.be.empty;
limit.should.equals(25);
skip.should.equals(0);

Expand Down Expand Up @@ -148,7 +149,7 @@ describe('page', function () {

data.options.start.should.equals(0);
data.options.count.should.equals(50);
data.results.should.be.empty();
data.results.should.be.empty;
data.total.should.equals(total);

return done();
Expand Down

0 comments on commit 76f2849

Please sign in to comment.