fuzzysort
Fast SublimeText-like fuzzy search for JavaScript.
Sublime's fuzzy search is... sublime. I wish everything used it. So here's an open source js version.
Demo
https://rawgit.com/farzher/fuzzysort/master/test.html
Installation Node
npm install fuzzysort
const fuzzysort =
Installation Browser
Most Common Usage
fuzzysort.go(search, targets, options=null)
const mystuff = file:'Monitor.cpp' file:'MeshRenderer.cpp'const results = fuzzysort// [{score:-18, obj:{file:'MeshRenderer.cpp'}}, {score:-6009, obj:{file:'Monitor.cpp'}}]
Usage
fuzzysort.go(search, targets, options=null)
const results = fuzzysort// [{score: -18, target: "MeshRenderer.cpp"}, {score: -6009, target: "Monitor.cpp"}]
fuzzysort.goAsync(search, targets, options=null)
let promise = fuzzysortpromiseifinvalidated promise
Options
fuzzysort
fuzzysort.highlight(result, open='<b>', close='</b>')
fuzzysort // *t*es*t*
result
What is a const result = fuzzysort// exact match returns a score of 0. lower is worseresultscore // -59resultindexes // [29, 30, 31, 32, 33]resulttarget // some string that contains my query.resultobj // reference to your original obj when using options.keyfuzzysort // some string that contains my <b>query</b>.
How To Go Fast · Performance Tips
let targets = file:'Monitor.cpp' file:'MeshRenderer.cpp' // filter out targets that you don't need to search! especially long ones!targets = targets // if your targets don't change often, provide prepared targets instead of raw strings!targets // don't use options.key if you don't need a reference to your original objtargets = targets const options = limit: 100 // don't return more results than you need! allowTypo: false // if you don't care about allowing typos threshold: -10000 // don't return bad resultsfuzzysortfuzzysortfuzzysort
Advanced Usage
Search a list of objects, by multiple fields, with custom weights.
let objects = title:'Favorite Color' desc:'Chrome' title:'Google Chrome' desc:'Launch Chrome'let results = fuzzysort var bestResult = results0// When using multiple `keys`, results are different. They're indexable to get each normal resultfuzzysort // 'Google <b>Chr</b>ome'fuzzysort // 'Launch <b>Chr</b>ome'bestResultobjtitle // 'Google Chrome'
Multiple instances, each with different default options.
const strictsort = fuzzysort
Changelog
v1.1.0
- Added
allowTypo
as an option
v1.0.0
- Inverted scores; they're now negative instead of positive, so that higher scores are better
- Added ability to search objects by
key
/keys
with custom weights - Removed the option to automatically highlight and exposed
fuzzysort.highlight
- Removed all options from
fuzzysort
and moved them intofuzzysort.go
optional params
v0.x.x
- init