Gathering detailed insights and metrics for fast-fuzzy
Gathering detailed insights and metrics for fast-fuzzy
Gathering detailed insights and metrics for fast-fuzzy
Gathering detailed insights and metrics for fast-fuzzy
npm install fast-fuzzy
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
376 Stars
69 Commits
8 Forks
4 Watching
4 Branches
3 Contributors
Updated on 19 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-11.6%
19,731
Compared to previous day
Last week
2.5%
109,623
Compared to previous week
Last month
23.1%
445,220
Compared to previous month
Last year
80.2%
3,841,836
Compared to previous year
Fast fuzzy-search utility
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.
name | description | signature |
---|---|---|
fuzzy | fuzzy ranking algorithm; returns match strength | (term, candidate, options?) => score |
search | for one-off searches; returns a sorted array of matches | (term, candidates, options?) => matches |
Searcher | for searching the same set of candidates multiple times; caches the constructed trie1 | N/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
methodsname | description | signature |
---|---|---|
constructor | supply the options and initial list of candidates | (candidates?, options?) => searcher |
add | add new candidates to the list | (...candidates) => void |
search | perform a search against the instance's candidates | (term, options?) => matches 2 |
2 allows overriding the threshold
, returnMatchData
, and useDamerau
options
Searcher
and search
both take an options object for configuring behavior.
option | type | description | default |
---|---|---|---|
keySelector | Function | selects the string(s)3 to search when candidates are objects | s => s |
threshold | Number | the minimum score that can be returned | .6 |
ignoreCase | Bool | normalize case by calling toLower on input and pattern | true |
ignoreSymbols | Bool | strip non-word symbols4 from input | true |
normalizeWhitespace | Bool | normalize and trim whitespace | true |
returnMatchData | Bool | return match data5 | false |
useDamerau | Bool | use damerau-levenshtein distance | true |
useSellers | Bool | use the Sellers method for substring matching | true |
useSeparatedUnicode | Bool | use separated unicode | false |
sortBy | sortKind | defines which order results are returned in6 | bestMatch |
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.
You can call fuzzy
directly to get a match score for a single string
1const {fuzzy} = require("fast-fuzzy"); 2 3fuzzy("hello", "hello world"); //returns 1 4fuzzy("word", "hello world"); //returns .75 5 6//pass in custom options 7fuzzy("hello world", "hello world"); //returns 1 8fuzzy("hello world", "hello world", {normalizeWhitespace: false}); //returns .90909090...
Use search
to search a list of strings or objects
1const {search} = require("fast-fuzzy"); 2 3search("abc", ["def", "bcd", "cde", "abc"]); //returns ["abc", "bcd"] 4 5//pass in a keySelector to search for objects 6search( 7 "abc", 8 [{name: "def"}, {name: "bcd"}, {name: "cde"}, {name: "abc"}], 9 {keySelector: (obj) => obj.name}, 10); 11//returns [{name: "abc"}, {name: "bcd"}] 12 13//pass returnMatchData to receive the matchData for each result 14search("abc", ["def", "bcd", "cde", "abc"], {returnMatchData: true}); 15/* returns [{ 16 item: 'abc', original: 'abc', key: 'abc', score: 1, 17 match: {index: 0, length: 3}, 18}, { 19 item: 'bcd', original: 'bcd', key: 'bcd', score: 0.6666666666666667, 20 match: {index: 0, length: 2}, 21}] */
Use Searcher
in much the same way as search
1const {Searcher} = require("fast-fuzzy"); 2 3const searcher = new Searcher(["def", "bcd", "cde", "abc"]); 4searcher.search("abc"); //returns ["abc", "bcd"] 5 6//options are passed in on construction 7const anotherSearcher = new Searcher( 8 [{name: "thing1"}, {name: "thing2"}], 9 {keySelector: (obj) => obj.name}, 10); 11 12//some options can be overridden per call 13searcher.search("abc", {returnMatchData: true}); 14/* returns [{ 15 item: 'abc', original: 'abc', key: 'abc', score: 1, 16 match: {index: 0, length: 3}, 17}, { 18 item: 'bcd', original: 'bcd', key: 'bcd', score: 0.6666666666666667, 19 match: {index: 0, length: 2}, 20}] */
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/28 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More