Skip to content

Commit

Permalink
Merge 3791248 into 1b21c55
Browse files Browse the repository at this point in the history
  • Loading branch information
brozeph committed Jun 13, 2013
2 parents 1b21c55 + 3791248 commit 4fd77ea
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 309 deletions.
33 changes: 16 additions & 17 deletions lib/field.js
@@ -1,25 +1,24 @@
var Query = require('mongoose').Query;


Query.prototype.field = function (options) {
module.exports = function(mongoose) {
'use strict';

var self = this;
mongoose.Query.prototype.field = function (options) {
var self = this;

if (options && options.filters && options.filters.field) {
if (options.filters.field instanceof Array && options.filters.field.length) {
options.filters.field.forEach(function (field) {
if (options && options.filters && options.filters.field) {
if (options.filters.field instanceof Array && options.filters.field.length) {
options.filters.field.forEach(function (field) {

if (self.model.schema.path(field)) {
self.select(field);
if (self.model.schema.path(field)) {
self.select(field);
}
});
} else {
if (self.model.schema.path(options.filters.field)) {
self.select(options.filters.field);
}
});
} else {
if (self.model.schema.path(options.filters.field)) {
self.select(options.filters.field);
}
}
}

return this;
};
return this;
};
};
183 changes: 90 additions & 93 deletions lib/filter.js
@@ -1,119 +1,116 @@
var Query = require('mongoose').Query;


function sanitize(str) {
module.exports = function(mongoose) {
'use strict';

// santizes regex escapes
return str.replace(/\W\s/ig, '\\$&');
}
function sanitize(str) {
// santizes regex escapes
return str.replace(/\W\s/ig, '\\$&');
}


Query.prototype.filter = function (options) {
'use strict';
mongoose.Query.prototype.filter = function (options) {
if (!options || !options.filters) {
return this;
}

if (!options || !options.filters) {
return this;
}
var
key = null,
mandatory = options.filters.mandatory || {},
optional = options.filters.optional || {},
self = this,
value = null;

var
key = null,
mandatory = options.filters.mandatory || {},
optional = options.filters.optional || {},
self = this,
value = null;

var applyWhere = function (spec, buildRegex) {
for (var key in spec) {
if (spec.hasOwnProperty(key)) {
var val = spec[key];
self.where(key).regex(buildRegex(val));
var applyWhere = function (spec, buildRegex) {
for (var key in spec) {
if (spec.hasOwnProperty(key)) {
var val = spec[key];
self.where(key).regex(buildRegex(val));
}
}
}
};

var applyOptionals = function(optional) {
var items = [];
};

var populate = function (key, value, buildRegex) {
var
items = [],
item = {};
var applyOptionals = function(optional) {
var items = [];

if (value instanceof Array && value.length) {
value.forEach(function (val) {
var populate = function (key, value, buildRegex) {
var
items = [],
item = {};
item[key] = buildRegex(val);
items.push(item);
});
return items;
} else {
item[key] = buildRegex(value);
return item;
}
};

var parse = function(spec, buildRegex) {
var
parsedItems = null,
result = null;
if (value instanceof Array && value.length) {
value.forEach(function (val) {
item = {};
item[key] = buildRegex(val);
items.push(item);
});
return items;
} else {
item[key] = buildRegex(value);
return item;
}
};

if (!spec) {
return null;
}
var parse = function(spec, buildRegex) {
var
parsedItems = null,
result = null;

for (var key in spec) {
if (spec.hasOwnProperty(key)) {
result = populate(key, spec[key], buildRegex);
if (!spec) {
return null;
}

if (result instanceof Array && result.length) {
parsedItems = result;
} else {
if (!parsedItems) {
parsedItems = [];
for (var key in spec) {
if (spec.hasOwnProperty(key)) {
result = populate(key, spec[key], buildRegex);

if (result instanceof Array && result.length) {
parsedItems = result;
} else {
if (!parsedItems) {
parsedItems = [];
}
parsedItems.push(result);
}
parsedItems.push(result);
}
}
}
return parsedItems;
};
return parsedItems;
};

items = parse(optional.contains, regexContains);
if (items && items instanceof Array && items.length) {
self.or(items);
}
items = parse(optional.contains, regexContains);
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);
}
items = parse(optional.startsWith, regexStartsWith);
if (items && items instanceof Array && items.length) {
self.or(items);
}

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

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

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

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

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

// OPTIONAL
applyOptionals(optional);
// OPTIONAL
applyOptionals(optional);

return self;
};
return self;
};
};
23 changes: 15 additions & 8 deletions lib/index.js
@@ -1,12 +1,19 @@
var
field = require('./field'),
filter = require('./filter'),
keyword = require('./keyword'),
order = require('./order'),
page = require('./page');
var mongooseLib = require('mongoose');

module.exports.initialize = function (options, mongoose) {
if (!mongoose) {
mongoose = options;
options = null;
}

var
field = require('./field')(mongoose),
filter = require('./filter')(mongoose),
keyword = require('./keyword')(mongoose),
order = require('./order')(mongoose),
page = require('./page')(mongoose);

module.exports.initialize = function (options) {
if (options && options.maxDocs) {
page.initialize(options);
}
};
};

0 comments on commit 4fd77ea

Please sign in to comment.