-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
51 lines (48 loc) · 1.34 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
var search = require('./search')
var generateGrams = require('./generateGrams')
module.exports = function fuzzyfind (input, collection, options) {
if (typeof input !== 'string' || input === '') {
return collection
}
options = options || {}
var accessor = options.accessor || function(item) { return item }
var precision = options.precision === undefined ? 1 : options.precision
var suggestions = []
var grams = generateGrams(input, precision)
collection.forEach(function (item) {
var searchableItem = accessor(item)
grams.find(function (gram) {
var match = search(searchableItem).for(gram)
if (match.found) {
suggestions.push({
gram: gram,
length: match.end - match.start,
start: match.start,
searchableItem: searchableItem,
item: item
})
}
return match.found
})
})
return suggestions.sort(function (a, b) {
if (a.gram.length !== b.gram.length) {
return b.gram.length - a.gram.length
}
if (a.length !== b.length) {
return a.length - b.length
}
if (a.start !== b.start) {
return a.start - b.start
}
if (a.searchableItem > b.searchableItem) {
return 1
}
if (a.searchableItem < b.searchableItem) {
return -1
}
return 0
}).map(function (item) {
return item.item
})
}