Skip to content

Commit

Permalink
merge #308
Browse files Browse the repository at this point in the history
  • Loading branch information
vbud authored and particlebanana committed Jan 25, 2016
1 parent c45b55d commit 82e0391
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,31 +213,31 @@ Query.prototype.parseExpression = function parseExpression(field, expression) {
// Handle `contains` by building up a case insensitive regex
if(modifier === 'contains') {
val = utils.caseInsensitive(val);
val = '.*' + val + '.*';
val = '[\\s\\S]*' + val + '[\\s\\S]*';
obj['$regex'] = new RegExp('^' + val + '$', 'i');
return obj;
}

// Handle `like`
if(modifier === 'like') {
val = utils.caseInsensitive(val);
val = val.replace(/%/g, '.*');
val = val.replace(/%/g, '[\\s\\S]*');
obj['$regex'] = new RegExp('^' + val + '$', 'i');
return obj;
}

// Handle `startsWith` by setting a case-insensitive regex
if(modifier === 'startsWith') {
val = utils.caseInsensitive(val);
val = val + '.*';
val = val + '[\\s\\S]*';
obj['$regex'] = new RegExp('^' + val + '$', 'i');
return obj;
}

// Handle `endsWith` by setting a case-insensitive regex
if(modifier === 'endsWith') {
val = utils.caseInsensitive(val);
val = '.*' + val;
val = '[\\s\\S]*' + val;
obj['$regex'] = new RegExp('^' + val + '$', 'i');
return obj;
}
Expand Down Expand Up @@ -366,12 +366,15 @@ Query.prototype.parseValue = function parseValue(field, modifier, val) {
return val;
}

if(!validator.isMongoId(val)){//only if it's not mongodbID, for most of case usage would like: user.find('56173df732776c64852f8c91')
// Only if it's not mongodbID, for most of case usage would like:
// user.find('56173df732776c64852f8c91')
if(!validator.isMongoId(val)){
// Replace Percent Signs, work in a case insensitive fashion by default
val = utils.caseInsensitive(val);
val = val.replace(/%/g, '.*');
val = val.replace(/%/g, '[\\s\\S]*');
val = new RegExp('^' + val + '$', 'i');
}

return val;
}

Expand Down

0 comments on commit 82e0391

Please sign in to comment.