npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fuzzysort-esm

v3.0.1

Published

Fast SublimeText-like fuzzy search for JavaScript. ESM fork of farzher/fuzzysort.

Downloads

426

Readme

Demo

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

Installation Node

npm i fuzzysort-esm
import fuzzysort from 'fuzzysort-esm'

Installation Browser

<script src="https://cdn.jsdelivr.net/npm/[email protected]/fuzzysort.min.ejs"></script>

Usage

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

const mystuff = [{file: 'Monitor.cpp'}, {file: 'MeshRenderer.cpp'}]
const results = fuzzysort.go('mr', mystuff, {key: 'file'})

// [{score: 0.74, obj: {file: 'MeshRenderer.cpp'}}, {score: 0.28, obj: {file: 'Monitor.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() // 'some string that contains my <b>query</b>.'

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

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

result.highlight(callback)

result.highlight((m, i) => <react key={i}>{m}</react>) // [<react key=0>t</react>, 'es', <react key=1>t</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

Changelog

v3.0.1

  • ESM fork of farzher/fuzzysort