3.1.0 • Published 8 months ago

fuzzysort v3.1.0

Weekly downloads
34,227
License
MIT
Repository
github
Last release
8 months ago

Demo

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

npm.io

npm.io

npm.io

npm.io

Installation Node / Bun / Deno

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

Installation Browser

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

Usage

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

const mystuff = [{file: 'Apple.cpp'}, {file: 'Banana.cpp'}]
const results = fuzzysort.go('a', mystuff, {key: 'file'})
// [{score: 0.81, obj: {file: 'Apple.cpp'}}, {score: 0.59, obj: {file: 'Banana.cpp'}}]

Options

fuzzysort.go(search, targets, {
  threshold: 0,    // Don't return matches worse than this
  limit: 0,        // Don't return more results than this
  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)
})

What's a result

const result = fuzzysort.single('query', 'some string that contains my query.')
result.score       // .80 (1 is a perfect match. 0.5 is a good match. 0 is no match.)
result.target      // 'some string that contains my query.'
result.obj         // reference to your original obj when using options.key
result.indexes     // [29, 30, 31, 32, 33]

result.highlight('<b>', '</b>')
// 'some string that contains my <b>query</b>.'

result.highlight((m, i) => <react key={i}>{m}</react>)
// ['some string that contains my ', <react key=0>query</react>, '.']

Advanced Usage

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

let objects = [{
  title: 'Liechi Berry',
  meta: {desc: 'Raises Attack when HP is low.'},
  tags: ['berries', 'items'],
  bookmarked: true,
}, {
  title: 'Petaya Berry',
  meta: {desc: 'Raises Special Attack when HP is low.'},
}]

let results = fuzzysort.go('attack berry', objects, {
  keys: ['title', 'meta.desc', obj => obj.tags?.join()],
  scoreFn: r => r.score * r.obj.bookmarked ? 2 : 1, // if the item is bookmarked, boost its score
})

var keysResult = results[0]
// When using multiple `keys`, results are different. They're indexable to get each normal result
keysResult[0].highlight() // 'Liechi <b>Berry</b>'
keysResult[1].highlight() // 'Raises <b>Attack</b> when HP is low.'
keysResult.score          // .84
keysResult.obj.title      // 'Liechi Berry'

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: .5, // don't return bad results
}
fuzzysort.go('gotta', targets, options)
fuzzysort.go('go',    targets, options)
fuzzysort.go('fast',  targets, options)

Gotcha

result.score is implemented as a getter/setter and stored different internally r.score = .3; // r.score == 0.30000000000000004

Star History

Star History Chart

Changelog

v3.1.0

  • Automatically handle diacritics / accents / ligatures

v3.0.0

  • Added new behavior when using keys and your search contains spaces!
  • Added options.key can now be a function {key: obj => obj.tags.join()}
  • Removed fuzzysort.indexes & Added result.indexes (as a getter/setter for GC perf)
  • Removed fuzzysort.highlight() & Added result.highlight()
  • Changed scoring: score is now a number from 0 to 1 instead of from -Infinity to 0
  • Changed scoring: substring matches are even more relevant
  • Changed scoring: straw berry now matches great against strawberry
  • Changed scoring: tweaked the scoring quite a bit
  • result.score is behind a getter/setter for performance reasons
  • Fixed minor issues

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@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/ttyfuzzyparsesvelte-quicksearch-barstoriesxxxixxxstrapi-plugin-fuzzy-searchtestfactory-send-requestvalen@dmatora/trpc-panel@compai/css-gui@commandbar/foobar@cortezaproject/corteza-vue@rosmarinus/search@rpcbase/clientkaribou-mlxinsomnia-send-requestktbl-apis@ticketlos/nlpmesaidesvelo-shamzicpro-webuc-alfred-orclapextrpc-paneltoggl-tracker@automagicl/home-cli@altdot/ui@carrotsearch/gatsby-plugin-content-search@britecore/bc-design-system@clburlison/trpc-panel@aarhus-university/au-lib-react-maps@davidfrp/saga@deconet/react-command-palette@devsen/trpc-panel@digital-alchemy/terminalzoomjunpackage@dtinth/tailwind-css-class-search@folk-org/react-command-palette@hp4k1h5/agora@genshin-db/tcg@gerark/just-svelte-lib@leaflink/stash@iteria-app/wysiwyg@macrovision/core@manuscripts/manuscript-article-editor@manuscripts/style-guide@manuscripts/symbol-picker@mapsindoors/components@maory/npmr@navikt/bedriftsmeny@extendslcc/trpc-panel@eyecuelab/react-common@metamorph/trpc-panel@monopolo11/trpc-panel@integry/sdk@integry/actions-sdk-beta@joeattardi/emoji-button@incidentiq/emoji-button@prvidrgo/manage-vue@radio4000/components@saphe/react-table@openctx/provider-devdocs@opengovsg/design-system-react@pathfinder-ide/react@phartenfeller/alfred-vscode-workspaces@pedroslopez/trpc-panel@silverbulletmd/web@rogueg/zen@robertcooper/alfred-vscode@rorhug/trpc-panel@rikkainc/trpc-panelmethonenuxt-dbrumrunreact-command-paletterekit-studio-sdkprotolab-corta-vuereact-fuzzysortnext-trpc-panel-2policyengine-clientreq-clisimple-emoji-buttonreact18-react-command-palettesift-clireact-turbo-search-boxgit-interactive-checkoutghost-searchidl-for-vscode
3.1.0

8 months ago

3.0.2

12 months ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.4

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.9.0

3 years ago

1.2.1

3 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago

0.8.2

8 years ago

0.8.1

8 years ago

0.8.0

8 years ago

0.7.7

8 years ago

0.7.6

8 years ago

0.7.5

8 years ago

0.7.4

8 years ago

0.7.3

8 years ago

0.7.2

8 years ago

0.7.1

8 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago