1.12.0 โ€ข Published 1 year ago

fast-fuzzy v1.12.0

Weekly downloads
1,527
License
ISC
Repository
github
Last release
1 year ago

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, optionally taking the lowercase of a string, optionally removing non-word characters, and optionally flattening/trimming whitespace. Graphemes, such as conjoined emoji ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€, are treated as single characters.

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 in score are broken by earliness of match (when using sellers substring match only). Further 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. 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 which could possibly score >= the threshold, the entire subtree is skipped. This significantly improves search times compared to a bruteforce search.

While the default options are to use Damerau and Sellers (transposition-friendly substring search), either of these options can be opted out of if the need arises.

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 the constructed trie1N/A

1 it is recommended that you use a Searcher when searching the same set multiple times. search will create a new trie every time, and while this is relatively cheap, it can have an impact on responsiveness if you intend to update search results in real time, i.e. while typing.

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?) => matches2

2 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)3 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 symbols4 from inputtrue
normalizeWhitespaceBoolnormalize and trim whitespacetrue
returnMatchDataBoolreturn match data5false
useDamerauBooluse damerau-levenshtein distancetrue
useSellersBooluse the Sellers method for substring matchingtrue
useSeparatedUnicodeBooluse separated unicodefalse
sortBysortKinddefines which order results are returned in6bestMatch

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

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

5 in the form {item, original, key, score, match: {index, length}}. Match index and length are in terms of the original, non-normalized string. Also note that match will be undefined if useSellers is false.

6 the supported sortKinds are insertOrder and bestMatch

fuzzy accepts a subset of these options (excluding keySelector, threshold, and sortBy) 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},
}] */
1.12.0

1 year ago

1.11.2

2 years ago

1.11.1

2 years ago

1.11.0

2 years ago

1.10.9

3 years ago

1.10.10

3 years ago

1.10.8

4 years ago

1.10.7

4 years ago

1.10.6

4 years ago

1.10.5

4 years ago

1.10.4

4 years ago

1.10.3

4 years ago

1.10.2

4 years ago

1.10.1

4 years ago

1.10.0

4 years ago

1.9.2

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.8

5 years ago

1.8.7

5 years ago

1.8.6

5 years ago

1.8.5

5 years ago

1.8.4

5 years ago

1.8.3

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.8

6 years ago

1.3.7

6 years ago

1.3.6

7 years ago

1.3.5

7 years ago

1.3.4

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago