forked from javve/list.fuzzysearch.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (64 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var classes = require('classes'),
events = require('events'),
extend = require('extend'),
toString = require('to-string'),
getByClass = require('get-by-class');
module.exports = function(options) {
options = options || {};
extend(options, {
location: 0,
distance: 100,
threshold: 0.4,
multiSearch: true,
searchClass: 'fuzzy-search'
});
var fuzzy = require('./src/fuzzy'),
list;
var fuzzySearch = {
search: function(searchString, columns) {
// Substract arguments from the searchString or put searchString as only argument
var searchArguments = options.multiSearch ? searchString.replace(/ +$/, '').split(/ +/) : [searchString];
for (var k = 0, kl = list.items.length; k < kl; k++) {
fuzzySearch.item(list.items[k], columns, searchArguments);
}
},
item: function(item, columns, searchArguments) {
var found = true;
for(var i = 0; i < searchArguments.length; i++) {
var foundArgument = false;
for (var j = 0, jl = columns.length; j < jl; j++) {
if (fuzzySearch.values(item.values(), columns[j], searchArguments[i])) {
foundArgument = true;
}
}
if(!foundArgument) {
found = false;
}
}
item.found = found;
},
values: function(values, value, searchArgument) {
if (values.hasOwnProperty(value)) {
var text = toString(values[value]).toLowerCase();
if (fuzzy(text, searchArgument, options)) {
return true;
}
}
return false;
}
};
return {
init: function(parentList) {
list = parentList;
events.bind(getByClass(list.listContainer, options.searchClass), 'keyup', function(e) {
var target = e.target || e.srcElement; // IE have srcElement
list.search(target.value, fuzzySearch.search);
});
return;
},
search: function(str, columns) {
list.search(str, columns, fuzzySearch.search);
},
name: options.name || "fuzzySearch"
};
};