This library implements an algorithm that is suitable for autocompletion.
kindalike function takes a search string and an array of subject strings to search in:
var query = ;var subjects = ;// matches is an Array where each element is an object// with properties subject, indices and gapsvar matches = ;
match is an object like so:
subject: 'horse' indices: 1 4 gaps: 2
subject property is the matched subject.
indices property is an array of int, each representing the index in the subject
where a query character is found. In the example above the query string was
each of those letters were found at indices
gaps property is a number indicating how many gaps there are between letters from the
query string. In this case there was a gap of
2 - between index
The matches are sorted by the
gaps property, ranking matches with small gaps higher. Subjects that don't
match all the characters in the query are excluded.
match can be turned into string with
<span> elements around each matched character:
var html = kindalike
By styling the
<span> element this can be used to highlight the letters from the query.
index.html in a browser.