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

fuzzbunny

v1.0.1

Published

Fast fuzzy string matching with scoring and matched ranges

Downloads

2,686

Readme

fuzzbunny

fuzzbunny is a small (1k), fast & memory efficient fuzzy string searching/matching/highlighting library. It works equally well in a browser environment or Node.js.

Travis npm npm npm

Why fuzzbunny?

  • Human friendly - fuzzbunny scoring and algorithm is more tuned to "human" searching patterns. It surfaces what you're looking for with minimal keystrokes.
  • Lightweight - ~3KB minified and has zero dependencies.
  • Ultra fast - ~million lines/second on a 2.4Ghz virtual core.

Other similar libraries are fuzzymatch, fuzzy, fuzzy-search, fuzzyjs.

fuzzbunny aims to be nimble and fast. It has a simple api that can easily be integrated with any frontend library to build great search UI. We use it at mixpanel.com to power our UI dropdowns and tables.

Installation

npm install --save fuzzbunny or yarn add fuzzbunny

Demo

Fuzzbunny Gutenberg Catalog Demo →

Fuzzbunny demo

Usage

const {fuzzyFilter, fuzzyMatch} = require(`fuzzbunny`);
// or import {fuzzyFilter, fuzzyMatch} from 'fuzzbunny';

const heroes = [
  {
    name: `Claire Bennet`,
    ability: `Rapid cellular regeneration`,
  },
  {
    name: `Micah Sanders`,
    ability: `Technopathy`,
  },
  {
    name: `Hiro Nakamura`,
    ability: `Space-time manipulation`,
  },
  {
    name: `Peter Petrelli`,
    ability: `Tactile power mimicry`,
  },
];

// Use fuzzyFilter to filter an array of items on specific fields and get filtered + score-sorted results with highlights.
const results = fuzzyFilter(heroes, `stm`, {fields: [`name`, `ability`]});
/*
results = [
  {
    item: {
      name: 'Peter Petrelli',
      ability: 'Tactile power mimicry',
    },
    score: 1786,
    highlights: {
      ability: ['', 'T', 'actile power ', 'm', 'imicry'],
    },
  },
  {
    item: {
      name: 'Hiro Nakamura',
      ability: 'Space-time manipulation',
    },
    score: 983,
    highlights: {
      ability: ['Space-', 't', 'ime ', 'm', 'anipulation'],
    },
  },
];
*/

// Use fuzzyMatch to match a single string to get score + highlights. Returns null if no match found.
const match = fuzzyMatch(heroes[0].name, `ben`);
/*
match = {
  score: 2893,
  highlights: ['Claire ', 'Ben', 'net'],
};
*/

Scoring and Sort order

fuzzbunny uses a scoring algorithm that prioritizes following signals. See _getMatchScore function.

Example 1:

  • Start of string - {Mayfl}ower ranks above The {Mayfl}ower
  • Closer to start - The {Mayfl}ower ranks above Story of the {Mayfl}ower
  • Contiguous length - The {Mayfl}ower ranks above {May} {fl}ower
  • Alphabetically - The {May} {fl}ower ranks above This {May} {fl}ower

image

Example 2:

const f = require(`fuzzbunny`);
f.fuzzyMatch(`Gobbling pupusas`, `usa`);
// {score: 2700, highlights: ['Gobbling pup', 'usa', 's']}
f.fuzzyMatch(`United Sheets of Antarctica`, `usa`);
// {score: 2276, highlights: ['', 'U', 'nited ', 'S', 'heets of ', 'A', 'ntarctica']}

Gobbling pup{usa}s wins because 3 letter contiguous sequence yields a higher score.

NOTE: fuzzbunny optmizes for meaningful results. It only does substring/prefix/acronym-matching, not greedy matching.

This is because humans brains are great at prefix recall. e.g words that start with "ca" are much easier to recall than words that contain the letters "c" and "a" somewhere. It's easy to remember that {usa} stands for {U}nited {S}tates of {A}merica, not F{u}ll Java{s}cript Fr{a}mework

Performance

fuzzbunny matches ~ million lines/second on modern hardware. Tested on 2018 MacBook Pro with 2.4Ghz CPU. See tests/performance.js

Types

fuzzbunny comes with autogenerated TypeScript types. See index.d.ts