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.
https://rawgit.com/farzher/fuzzysort/master/test.html
npm i fuzzysort
node
> require('fuzzysort').single('t', 'test')
{ score: 3, highlighted: '<b>t</b>est' }
<script src="https://rawgit.com/farzher/fuzzysort/master/fuzzysort.js"></script>
<script>
console.log(fuzzysort.single('t', 'test'))
</script>
fuzzysort.single('query', 'some string that contains my query.')
// {score: 59, highlighted: "some string that contains my <b>query</b>."}
fuzzysort.single('query', 'irrelevant string') // null
// exact match returns a score of 0. lower score is better
fuzzysort.single('query', 'query') // {score: 0, highlighted: "<b>query</b>"}
fuzzysort.go('mr', ['Monitor.cpp', 'MeshRenderer.cpp'])
// [{score: 18, highlighted: "<b>M</b>esh<b>R</b>enderer.cpp"}
// ,{score: 6009, highlighted: "<b>M</b>onito<b>r</b>.cpp"}]
let promise = fuzzysort.goAsync('mr', ['Monitor.cpp', 'MeshRenderer.cpp'])
promise.then(results => console.log(results))
if(invalidated) promise.cancel()
fuzzysort.noMatchLimit = 100
If there's no match for a span this long, give up (improves performance of long search targets)fuzzysort.highlightMatches = true
Turn this off if you don't care abouthighlighted
(improves performance)fuzzysort.highlightOpen = '<b>'
fuzzysort.highlightClose = '</b>'
fuzzysort.threshold = null
Don't return matches worse than this (improves performance) (irrelevant forsingle
)fuzzysort.limit = null
Don't return more results than this (improves performance ifhighlightMatches
is on) (irrelevant forsingle
)