Skip to content

Commit

Permalink
Multiple word searching with List.search - closes #680
Browse files Browse the repository at this point in the history
  • Loading branch information
sheffieldnick committed Apr 2, 2020
1 parent 805e1bb commit 3109ede
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/search.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,41 @@ module.exports = function(list) {
};
var search = {
list: function() {
var words = [];
// Extract quoted phrases "word1 word2" from original searchString
// searchString is converted to lowercase by List.js
var ss = searchString;
while ((var phrase = ss.match(/"([^"]+)"/)) !== null) {
words.push(phrase[1]);
ss = ss.substring(0,phrase.index) + ss.substring(phrase.index+phrase[0].length);
};
// Get remaining space-separated words (if any)
ss = ss.trim();
if (ss.length) words = words.concat(ss.split(/\s+/));
for (var k = 0, kl = list.items.length; k < kl; k++) {
search.item(list.items[k]);
}
},
item: function(item) {
item.found = false;
for (var j = 0, jl = columns.length; j < jl; j++) {
if (search.values(item.values(), columns[j])) {
item.found = true;
return;
}
}
},
values: function(values, column) {
if (values.hasOwnProperty(column)) {
text = list.utils.toString(values[column]).toLowerCase();
if ((searchString !== "") && (text.search(searchString) > -1)) {
return true;
var item = list.items[k];
item.found = false;
if (!words.length) continue;
for (var i = 0, il = words.length; i < il; i++) {
var word_found = false;
for (var j = 0, jl = columns.length; j < jl; j++) {
var values = item.values(), column = columns[j];
if (values.hasOwnProperty(column) && values[column] !== undefined && values[column] !== null) {
var text = (typeof values[column] !== 'string') ? values[column].toString() : values[column];
if (text.toLowerCase().indexOf(words[i]) !== -1) {
// word found, so no need to check it against any other columns
word_found = true;
break;
}
}
}
// this word not found? no need to check any other words, the item cannot match
if (!word_found) break;
}
item.found = word_found;
}
return false;
},
// Removed search.item() and search.values()
reset: function() {
list.reset.search();
list.searched = false;
Expand Down

0 comments on commit 3109ede

Please sign in to comment.