Fast SublimeText-like fuzzy search for JavaScript. ESM fork of farzher/fuzzysort.
npm install fuzzysort-esm
Fast, Tiny, & Good fuzzy search for JavaScript.
Ported to ESM by hlysine.
Fast: <1ms to search 13,000 files.
Tiny: 1 file, 0 dependencies, 5kb.
Good: clean api + sorts results well.
https://rawgit.com/farzher/fuzzysort/master/test/test.html




``sh`
npm i fuzzysort-esm`js`
import fuzzysort from 'fuzzysort-esm'
`html`
`js`
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'}}]
`js
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)`
})
`js
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('', '')
// 'some string that contains my query.'
result.highlight((m, i) =>
// ['some string that contains my ',
`
Search a list of objects, by multiple complex keys, with custom weights.
`js
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 Berry'
keysResult[1].highlight() // 'Raises Attack when HP is low.'
keysResult.score // .84
keysResult.obj.title // 'Liechi Berry'
`js
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)
`
is implemented as a getter/setter and stored different internally
r.score = .3; // r.score == 0.30000000000000004`#### v3.1.1
- Automatically handle diacritics / accents / ligatures
#### v3.0.1
- ESM fork of farzher/fuzzysort