fuzzysort

Fast SublimeText-like fuzzy search for JavaScript.

README


Fast, Tiny, & Good 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.


- Fast - 1ms to search 13,000 files.
- Tiny - 1 file, 5kb. 0 dependencies.
- Good - clean api + sorts results well.

https://rawgit.com/farzher/fuzzysort/master/test/test.html

undefined

undefined

undefined

Installation Node


  1. ```sh
  2. npm install fuzzysort
  3. ```
  1. ``` js
  2. const fuzzysort = require('fuzzysort')
  3. ```
  1. ``` js
  2. import fuzzysort from 'fuzzysort'
  3. ```

Installation Browser


  1. ``` html
  2. <script src="https://cdn.jsdelivr.net/npm/fuzzysort@2.0.4/fuzzysort.min.js"></script>
  3. ```

Most Common Usage


fuzzysort.go(search, targets, options=null)


  1. ``` js
  2. const mystuff = [{file:'Monitor.cpp'}, {file:'MeshRenderer.cpp'}]
  3. const results = fuzzysort.go('mr', mystuff, {key:'file'})
  4. // [{score:-18, obj:{file:'MeshRenderer.cpp'}}, {score:-6009, obj:{file:'Monitor.cpp'}}]
  5. ```

Usage


fuzzysort.go(search, targets, options=null)


  1. ``` js
  2. const results = fuzzysort.go('mr', ['Monitor.cpp', 'MeshRenderer.cpp'])
  3. // [{score: -18, target: "MeshRenderer.cpp"}, {score: -6009, target: "Monitor.cpp"}]
  4. ```

Options

  1. ``` js
  2. fuzzysort.go(search, targets, {
  3.   threshold: -Infinity, // Don't return matches worse than this (higher is faster)
  4.   limit: Infinity, // Don't return more results than this (lower is faster)
  5.   all: false, // If true, returns all results for an empty search

  6.   key: null, // For when targets are objects (see its example usage)
  7.   keys: null, // For when targets are objects (see its example usage)
  8.   scoreFn: null, // For use with `keys` (see its example usage)
  9. })
  10. ```

#### `fuzzysort.highlight(result, open='', close='')`

  1. ``` js
  2. fuzzysort.highlight(fuzzysort.single('tt', 'test'), '*', '*') // *t*es*t*
  3. ```

fuzzysort.highlight(result, callback)

  1. ``` js
  2. fuzzysort.highlight(result, (m, i) => <react key={i}>{m}</react>) // [treact>, 'es', <react key=1>t</react>]
  3. ```

What is a result


  1. ``` js
  2. const result = fuzzysort.single('query', 'some string that contains my query.')
  3. // exact match returns a score of 0. lower is worse
  4. result.score // -59
  5. result.target // some string that contains my query.
  6. result.obj // reference to your original obj when using options.key
  7. fuzzysort.highlight(result, '<b>', '</b>') // some string that contains my query.
  8. ```

How To Go Fast · Performance Tips


  1. ``` js
  2. let targets = [{file:'Monitor.cpp'}, {file:'MeshRenderer.cpp'}]

  3. // filter out targets that you don't need to search! especially long ones!
  4. targets = targets.filter(t => t.file.length < 1000)

  5. // if your targets don't change often, provide prepared targets instead of raw strings!
  6. targets.forEach(t => t.filePrepared = fuzzysort.prepare(t.file))

  7. // don't use options.key if you don't need a reference to your original obj
  8. targets = targets.map(t => t.filePrepared)

  9. const options = {
  10.   limit: 100, // don't return more results than you need!
  11.   threshold: -10000, // don't return bad results
  12. }
  13. fuzzysort.go('gotta', targets, options)
  14. fuzzysort.go('go', targets, options)
  15. fuzzysort.go('fast', targets, options)
  16. ```

Advanced Usage


Search a list of objects, by multiple fields, with custom weights.

  1. ``` js
  2. let objects = [{title:'Favorite Color', desc:'Chrome'}, {title:'Google Chrome', desc:'Launch Chrome'}]
  3. let results = fuzzysort.go('chr', objects, {
  4.   keys: ['title', 'desc'],
  5.   // Create a custom combined score to sort by. -100 to the desc score makes it a worse match
  6.   scoreFn: a => Math.max(a[0]?a[0].score:-1000, a[1]?a[1].score-100:-1000)
  7. })

  8. var bestResult = results[0]
  9. // When using multiple `keys`, results are different. They're indexable to get each normal result
  10. fuzzysort.highlight(bestResult[0]) // 'Google Chrome'
  11. fuzzysort.highlight(bestResult[1]) // 'Launch Chrome'
  12. bestResult.obj.title // 'Google Chrome'
  13. ```

Changelog


v2.0.0

- Added new behavior when your search contains spaces!
- Added fuzzysort.min.js
- Now depends on ES6 features
- Removed result.indexes & Added fuzzysort.indexes (improved GC performance)
- Completely Removed options.allowTypo
- Completely Removed fuzzysort.goAsync
- Completely Removed fuzzysort.new
- Rewrote the demo

v1.9.0

- Even faster
- Added options.all
- Deprecated/Removed options.allowTypo
- Deprecated/Removed fuzzysort.goAsync
- Changed scoring: boosted substring matches
- Changed scoring: targets with too many beginning indexes lose points for being a bad target
- Changed scoring: penality for not starting near the beginning
- Changed scoring: penality for more groups
- Fixed "Exponential backtracking hangs browser"

v1.2.0

- Added fuzzysort.highlight(result, callback)

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 into fuzzysort.go optional params

v0.x.x


- init