Skip to content

Commit

Permalink
Improve search to OR by whitespace; fixes Meteor-Community-Packages#57
Browse files Browse the repository at this point in the history
  • Loading branch information
aldeed committed Sep 11, 2015
1 parent c8e1328 commit aaf34c4
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions client/pubSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,31 @@ getPubSelector = function getPubSelector(selector, searchString, searchFields, s
var searchTerms = _.isEmpty(searchColumns) ? searchFields : searchColumns;

_.each(searchTerms, function(field) {
var m1 = {}, m2 = {};
var searchValue = field.search.value || '';

var numSearchString = Number(field.search.value);
// Split and OR by whitespace, as per default DataTables search behavior
searchValue = searchValue.match(/\S+/g);

// String search
m1[field.data] = { $regex: field.search.value };
_.each(searchValue, function (searchTerm) {
var m1 = {}, m2 = {};

// DataTables searches are case insensitive by default
if (searchCaseInsensitive !== false) {
m1[field.data].$options = "i";
}
// String search
m1[field.data] = { $regex: searchTerm };

searches.push(m1);
// DataTables searches are case insensitive by default
if (searchCaseInsensitive !== false) {
m1[field.data].$options = "i";
}

searches.push(m1);

// Number search
if (!isNaN(numSearchString)) {
m2[field.data] = numSearchString;
searches.push(m2);
}
// Number search
var numSearchString = Number(searchTerm);
if (!isNaN(numSearchString)) {
m2[field.data] = numSearchString;
searches.push(m2);
}
});
});

var result;
Expand All @@ -62,5 +68,7 @@ getPubSelector = function getPubSelector(selector, searchString, searchFields, s
result = {$or: searches};
}

console.log(result);

return result;
};

0 comments on commit aaf34c4

Please sign in to comment.