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