Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-fuzzy

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-fuzzy

Fast and tiny fuzzy-search utility

  • 1.8.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

fast-fuzzy Build Status npm

Fast fuzzy-search utility

methodology

fast-fuzzy is a tiny, lightning-quick fuzzy-searching utility. The ranking algorithm is a modification of levenshtein distance proposed by Peter H. Sellers (paper). fast-fuzzy also uses the damerau-levenshtein distance by default, which, compared to normal levenshtein, punishes transpositions less.

Inputs are normalized before search. Normalization consists of standard utf8-normalization, followed by optionally taking the lowercase of a string, optionally removing non-word characters, and optionally flattening/trimming whitespace.

Inputs are scored from 0 to 1, where a higher score indicates a closer match. When searching, results are returned in descending order of score. Ties are broken by favoring the candidate whose length is closest to the length of the search term. This causes matches which are closer to exact full string matches to be effectively ranked higher in the case of a tie. Ties in length difference are broken by insertion order.

Lists of candidates are stored in a trie internally, which avoids doing redundant work on candidates with common prefixes. Additionally, when a subtree of the trie can be determined to have no string long enough to score > threshold, the entire subtree is skipped entirely. This can significantly improve search times compared with a bruteforce search.

A note about normalization

utf8 normalization is not optional, all strings will be normalized internally at minimum. Match positions, as a result, refer to positions within the normalized string. Match positions are, however, mapped back to the position in the string before whitespace and symbols are stripped out. Therefore, it is highly recommended that if one intends on using returned match data, you normalize the strings you intend to search by. This can be done by calling string.normalize().

exports

namedescriptionsignature
fuzzyfuzzy ranking algorithm; returns match strength(term, candidate, options?) => score
searchfor one-off searches; returns a sorted array of matches(term, candidates, options?) => matches
Searcherfor searching the same set of candidates multiple times; caches normalization and key selectionN/A

Searcher methods

namedescriptionsignature
constructorsupply the options and initial list of candidates(candidates?, options?) => searcher
addadd new candidates to the list(...candidates) => void
searchperform a search against the instance's candidates(term, options?) => matches*

* allows overriding the threshold, returnMatchData, and useDamerau options

options

Searcher and search both take an options object for configuring behavior.

optiontypedescriptiondefault
keySelectorFunctionselects the string(s)* to search when candidates are objectss => s
thresholdNumberthe minimum score that can be returned.6
ignoreCaseBoolnormalize case by calling toLower on input and patterntrue
ignoreSymbolsBoolstrip non-word symbols** from inputtrue
normalizeWhitespaceBoolnormalize and trim whitespacetrue
returnMatchDataBoolreturn match data***false
useDamerauBooluse damerau-levenshtein distancetrue

* if the keySelector returns an array, the candidate will take the score of the highest scoring key.

** `~!@#$%^&*()-=_+{}[]\|\;':",./<>?

*** in the form {item, original, key, score, match: {index, length}}

fuzzy accepts a subset of these options (excluding keySelector and threshold) with the same defaults.

examples

You can call fuzzy directly to get a match score for a single string

const {fuzzy} = require("fast-fuzzy");

fuzzy("hello", "hello world"); //returns 1
fuzzy("word", "hello world"); //returns .75

//pass in custom options
fuzzy("hello world", "hello  world"); //returns 1
fuzzy("hello world", "hello  world", {normalizeWhitespace: false}); //returns .90909090...

Use search to search a list of strings or objects

const {search} = require("fast-fuzzy");

search("abc", ["def", "bcd", "cde", "abc"]); //returns ["abc", "bcd"]

//pass in a keySelector to search for objects
search(
    "abc",
    [{name: "def"}, {name: "bcd"}, {name: "cde"}, {name: "abc"}],
    {keySelector: (obj) => obj.name},
);
//returns [{name: "abc"}, {name: "bcd"}]

//pass returnMatchData to receive the matchData for each result
search("abc", ["def", "bcd", "cde", "abc"], {returnMatchData: true});
/* returns [{
    item: 'abc', original: 'abc', key: 'abc', score: 1,
    match: {index: 0, length: 3},
}, { 
    item: 'bcd', original: 'bcd', key: 'bcd', score: 0.6666666666666667,
    match: {index: 0, length: 2},
}] */

Use Searcher in much the same way as search

const {Searcher} = require("fast-fuzzy");

const searcher = new Searcher(["def", "bcd", "cde", "abc"]);
searcher.search("abc"); //returns ["abc", "bcd"]

//options are passed in on construction
const anotherSearcher = new Searcher(
    [{name: "thing1"}, {name: "thing2"}],
    {keySelector: (obj) => obj.name},
);

//some options can be overridden per call
searcher.search("abc", {returnMatchData: true});
/* returns [{
    item: 'abc', original: 'abc', key: 'abc', score: 1,
    match: {index: 0, length: 3},
}, { 
    item: 'bcd', original: 'bcd', key: 'bcd', score: 0.6666666666666667,
    match: {index: 0, length: 2},
}] */

Keywords

FAQs

Package last updated on 16 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc