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 🙏

© 2026 – Pkg Stats / Ryan Hefner

fuzzybear

v2.0.1

Published

Fuzzybear is a library for fuzzy string search with a special focus on short strings.

Readme

Fuzzybear - short string search

NPM Version Tests CodeQL

Fuzzybear is a TypeScript library for fuzzy string search with a special focus on short strings. It is designed to use multiple string distance functions (including custom) but by default it uses a combination of Jaro-Winkler and Jaccard string distances. The former favours matches from the beginning of a string, while the latter splits the string into tokens and analyses those.

Combining the Jaro-Winkler and Jaccard distances provides very good level of fuzzines with comparable performance.

Fuzzy bear

Installation

npm install fuzzybear

The package ships type declarations and is published as both ESM and CommonJS:

import { search, score } from 'fuzzybear'        // ESM
const { search, score } = require( 'fuzzybear' ) // CommonJS

Usage

Subset Search

search is the primary method used for searching. It accepts either a string array or an object array where each element contains a key label.

let matches = [ 'Identical', 'Identifier', 'dentical', 'Dental', 'dentist', 'different' ]
// OR
let matches = [
    { label: 'Identical', id: 's0' },
    { label: 'Identifier', id: 's1' },
    { label: 'dentical', id: 's2' },
    { label: 'Dental', id: 's3' },
    { label: 'dentist', id: 's4' },
    { label: 'Different', id: 's5' },
]
search( 'Identical', matches )

Each result is the original element with a _score added, sorted by descending score, where 1 is an exact match.

You can also restrict the number of results returned:

search( 'Identical', matches, { results: 3 })

Manual scoring

score( 'prism', 'contact' )    // => 0
score( 'prism', 'prism' )      // => 1
score( 'prism', 'unpristine' ) // => 0.56

Advanced usage

Search method parameters

You can pass custom methods and/or use one of the implemented methods in fuzzybear. You can also specify certain method parameters to override the method's behaviour. For example, you can use a minimum of 3 letter substring matches in the Jaccard search method to ignore matches with less than 3 letters.

search( 'Identical', matches, {
    methods: [
        {
            name: 'jaccard',
            params: { n: 3 } // Minimum ngram length
        }
    ]
})

Custom search function

You can also pass a custom scoring function to the search method. The function takes 3 parameters: the search term, the target string and the method parameters. The function should return a number between 0 and 1, where 0 is a perfect match (meaning the string distance is 0).

search( 'asd', [ 'a', 'b', 'c', 'd' ], {
    methods: [
        {
            name: 'match-all',
            function: function( _a, _b, _params ){
                return 0.36
            }
        }
    ]
})

Methods may also be given in shorthand — as a bare name or a bare function, each of which defaults to a weight of 1:

search( 'term', matches, { methods: [ 'jaro_winkler', 'jaccard' ] })
search( 'term', matches, { methods: [ ( a, b ) => 0.8 ] })

Preprocessors

Preprocessors normalise strings before they are scored. Each one is applied, in order, to both the search term and every candidate, so both sides are always transformed the same way. The built-in transliterate preprocessor folds text to ASCII — accents are stripped onto their base letter and Latin ligatures, special letters and common punctuation are mapped to ASCII equivalents — so that accented and unaccented spellings match.

search( 'Munchen', [ 'München', 'Malmö' ], { preprocessors: [ 'transliterate' ] })
// => München matches as an exact result

score( 'senor', 'señor', { preprocessors: [ 'transliterate' ] }) // => 1

A preprocessor may be a built-in name or a custom function. Case folding (see caseSensitive) is applied after the preprocessors run.

// Strip punctuation before scoring
search( 'ab-cd', [ 'abcd' ], { preprocessors: [ s => s.replace( /[^a-z0-9]/gi, '' ) ] })

The transliterate function is also exported for direct use:

import { transliterate } from 'fuzzybear'
transliterate( '¿señor Æther straße?' ) // => '?senor AEther strasse?'

API

search( term, elements, options? ): SearchResult[] // Fuzzy string search across a list of elements.
score( term, testString, options? ): number        // Fuzzy string comparison of two strings.

Configuration options

interface Options {
    results?: number       // Number of results to return. Defaults to 0 - all elements distanced
    labelField?: string    // Field to search against. Defaults to "label"
    caseSensitive?: boolean // Whether to perform a case sensitive match. Defaults to false
    minScore?: number      // Minimum score of matches to be included in the results
    methods?: MethodSpec[] // Which methods to use when scoring matches
    preprocessors?: PreprocessorSpec[] // Normalisers applied to both sides before scoring. Defaults to none
}

// A method may be a built-in name, a bare distance function, or a definition object
type MethodSpec = string | DistanceFunction | MethodDefinition

interface MethodDefinition {
    name?: string               // Name of a built-in search algorithm, or a label for a custom one
    function?: DistanceFunction // A custom search algorithm. Takes precedence over `name`
    weight?: number             // Search algorithm weight in scoring. Defaults to 1
    params?: unknown            // Search algorithm parameters
}

// Returns a distance normalized between 0 and 1, where 0 is an exact match —
// the inverse of the score reported by search() and score()
type DistanceFunction = ( a: string, b: string, params?: any ) => number

// A preprocessor may be a built-in name ('transliterate') or a custom function
type PreprocessorSpec = string | Preprocessor
type Preprocessor = ( input: string ) => string

Passing an unsupported method name throws Unsupported search method: <name>, and an unsupported preprocessor name throws Unsupported preprocessor: <name>.

Upgrading from 1.x

Fuzzybear is now written in TypeScript and ships its own type declarations. search and score keep the same signatures, so most callers need no change.

Two things did change:

  • Scores have shifted. The Jaccard method previously credited a repeated ngram in the test string multiple times against a single occurrence in the search term, which could push a distance outside its documented 0..1 range and let a string with a repeated token outrank an exact match. Occurrences are now consumed correctly. Absolute scores differ slightly and result ordering can change for longer text — if you persist scores or assert on exact orderings, re-check them.
  • Unsupported method names now throw. Previously they failed later with a TypeError.

PR's accepted for:

  • Search methods that support longer text and using a tokenised approach (and maybe even re-using the standard string distance methods).
  • Additional preprocessors, e.g. a Metaphone phonetic preprocessor (see the preprocessors option for where these plug in).

License

All code and documentation are licensed under the MIT license, although permission is not granted for using this code as a sample data for training machine learning networks.