fuzzbunny
1.0.0
fuzzbunny是一個小的(1K),快速和內存有效的模糊字符串搜索/匹配/突出顯示庫。它在瀏覽器環境或node.js中同樣效果很好。
其他類似的庫是fuzzymatch,fuzzy,fuzzy-search,fuzzyjs。
fuzzbunny目標是敏捷而快速。它具有一個簡單的API,可以輕鬆地與任何前端庫集成以構建出色的搜索UI。我們在mixpanel.com上使用它來為UI下拉列表和表供電。
npm install --save fuzzbunny或yarn add fuzzbunny
Fuzzbunny Gutenberg目錄演示→

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'],
};
*/ fuzzbunny使用的評分算法優先考慮以下信號。請參閱_getMatchScore函數。
示例1:
{Mayfl}ower排名The {Mayfl}owerThe {Mayfl}ower排名Story of the {Mayfl}owerThe {Mayfl}ower排名{May} {fl}owerThe {May} {fl}ower排名This {May} {fl}ower 
示例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贏了,因為3個字母連續序列會產生更高的分數。
注意: fuzzbunny選擇有意義的結果。它僅進行substring/prefix/縮寫匹配,而不是貪婪的匹配。
這是因為人類的大腦在前綴召回中很棒。例如,以“ Ca”開頭的單詞比包含字母“ C”和“ A”的單詞容易得多。很容易記住{usa}代表{U}nited {S}tates of {A}merica ,而不是F{u}ll Java{s}cript Fr{a}mework
fuzzbunny在現代硬件上匹配了約百萬行/秒。在2018年MacBook Pro上測試了2.4GHz CPU。請參閱測試/績效
fuzzbunny帶有自動化的打字稿類型。請參閱index.d.ts