2.0.4 • Published 1 year ago

fuzzysort v2.0.4

Weekly downloads
34,227
License
MIT
Repository
github
Last release
1 year ago

fuzzysort

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.

Demo

  • 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

npm.io

npm.io

npm.io

Installation Node

npm install fuzzysort
const fuzzysort = require('fuzzysort')
import fuzzysort from 'fuzzysort'

Installation Browser

<script src="https://cdn.jsdelivr.net/npm/fuzzysort@2.0.4/fuzzysort.min.js"></script>

Most Common Usage

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

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

Usage

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

const results = fuzzysort.go('mr', ['Monitor.cpp', 'MeshRenderer.cpp'])
// [{score: -18, target: "MeshRenderer.cpp"}, {score: -6009, target: "Monitor.cpp"}]
Options
fuzzysort.go(search, targets, {
  threshold: -Infinity, // Don't return matches worse than this (higher is faster)
  limit: Infinity, // Don't return more results than this (lower is faster)
  all: false, // If true, returns all results for an empty search

  key: null, // For when targets are objects (see its example usage)
  keys: null, // For when targets are objects (see its example usage)
  scoreFn: null, // For use with `keys` (see its example usage)
})

fuzzysort.highlight(result, open='<b>', close='</b>')

fuzzysort.highlight(fuzzysort.single('tt', 'test'), '*', '*') // *t*es*t*

fuzzysort.highlight(result, callback)

fuzzysort.highlight(result, (m, i) => <react key={i}>{m}</react>) // [<react key=0>t</react>, 'es', <react key=1>t</react>]

What is a result

const result = fuzzysort.single('query', 'some string that contains my query.')
// exact match returns a score of 0. lower is worse
result.score // -59
result.target // some string that contains my query.
result.obj // reference to your original obj when using options.key
fuzzysort.highlight(result, '<b>', '</b>') // 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.filter(t => t.file.length < 1000)

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

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

const options = {
  limit: 100, // don't return more results than you need!
  threshold: -10000, // don't return bad results
}
fuzzysort.go('gotta', targets, options)
fuzzysort.go('go', targets, options)
fuzzysort.go('fast', targets, options)

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.go('chr', objects, {
  keys: ['title', 'desc'],
  // Create a custom combined score to sort by. -100 to the desc score makes it a worse match
  scoreFn: a => Math.max(a[0]?a[0].score:-1000, a[1]?a[1].score-100:-1000)
})

var bestResult = results[0]
// When using multiple `keys`, results are different. They're indexable to get each normal result
fuzzysort.highlight(bestResult[0]) // 'Google <b>Chr</b>ome'
fuzzysort.highlight(bestResult[1]) // 'Launch <b>Chr</b>ome'
bestResult.obj.title // 'Google Chrome'

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
@flyteorg/console@flyteoss/console@leaflink/stash@hp4k1h5/iexcli@ccontour/home-cli@steggy/google@steggy/home-cli@keypj/genshin-db@infinitebrahmanuniverse/nolb-fuz@textea/yodassssiiixxx@everything-registry/sub-chunk-1713@digital-alchemy/config-builder@digital-alchemy/hass-cli@digital-alchemy/sampler-app@digital-alchemy/ttyfuzzyparsereq-clisift-clisimple-emoji-buttonrekit-studio-sdkstoriesxxxixxxpolicyengine-clientreact-command-palettereact-fuzzysortreact18-react-command-palettereact-turbo-search-boxstrapi-plugin-fuzzy-searchsvelte-quicksearch-bartestfactory-send-request@ticketlos/nlpesya-trpc-panelemoji-button-parroentreeposfuzzysort-collectionfuzz-runez-noteghost-searchgenshin-dbgit-interactive-checkoutmdbsveltemesaidesvelo-shamzicmethoneink-power-auto-completeinsomnia-send-requestkubectl-extraktbl-apishabanero-reacthabanero-vueinsomnia-cliinsomnia-componentskaribou-mlxnext-trpc-panel-2nuxt-dbtrpc-panelcreate-dosbox@hp4k1h5/agora@deconet/react-command-palette@dmatora/trpc-panel@fourlights/mapper-plugin-anonymize@devsen/trpc-panel@digital-alchemy/terminal@britecore/bc-design-system@incidentiq/emoji-button@integry/actions-sdk-beta@integry/sdk@carrotsearch/gatsby-plugin-content-searchcardinal-bot@joeattardi/emoji-button@davidfrp/saga@genshin-db/tcg@gerark/just-svelte-lib@aarhus-university/au-lib-react-mapsaws-keyhubbhaiyaaiktech-libraryaiktech-reactalfred-vscodealfred-vscode2alfred-piholetoggl-tracker@automagicl/home-cli@compai/css-gui@commandbar/foobar@cortezaproject/corteza-vueblueprint-vuezoomjcmdkit@radio4000/components@prvidrgo/manage-vue@opengovsg/design-system-react@iteria-app/wysiwyg@manuscripts/manuscript-article-editor@manuscripts/style-guide@manuscripts/symbol-picker@maory/npmr@mapsindoors/components@monopolo11/trpc-panel@pedroslopez/trpc-panel@phartenfeller/alfred-vscode-workspaces
2.0.3

1 year ago

2.0.2

1 year ago

2.0.4

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.9.0

2 years ago

1.2.1

2 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.7

7 years ago

0.7.6

7 years ago

0.7.5

7 years ago

0.7.4

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

0.0.0

7 years ago