File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -539,14 +539,17 @@ AutoComplete.levenshteinDistanceFilter = (distanceLessThan) => {
539539} ;
540540
541541AutoComplete . fuzzyFilter = ( searchText , key ) => {
542- if ( searchText . length === 0 ) {
543- return false ;
544- }
542+ const compareString = key . toLowerCase ( ) ;
543+ searchText = searchText . toLowerCase ( ) ;
545544
546- const subMatchKey = key . substring ( 0 , searchText . length ) ;
547- const distance = AutoComplete . levenshteinDistance ( searchText . toLowerCase ( ) , subMatchKey . toLowerCase ( ) ) ;
545+ let searchTextIndex = 0 ;
546+ for ( let index = 0 ; index < key . length ; index ++ ) {
547+ if ( compareString [ index ] === searchText [ searchTextIndex ] ) {
548+ searchTextIndex += 1 ;
549+ }
550+ }
548551
549- return searchText . length > 3 ? distance < 2 : distance === 0 ;
552+ return searchTextIndex === searchText . length ;
550553} ;
551554
552555AutoComplete . Item = MenuItem ;
Original file line number Diff line number Diff line change 1+ /* eslint-env mocha */
2+ import { assert } from 'chai' ;
3+ import AutoComplete from './AutoComplete' ;
4+
5+ describe ( '<AutoComplete />' , ( ) => {
6+ it ( 'search using fuzzy filter' , ( ) => {
7+ assert . ok ( AutoComplete . fuzzyFilter ( 'ea' , 'Peach' ) , 'should match Peach with ea' ) ;
8+ assert . ok ( AutoComplete . fuzzyFilter ( 'pah' , 'Peach' ) , 'should match Peach with pah' ) ;
9+ assert . ok ( AutoComplete . fuzzyFilter ( 'peach' , 'Peach' ) , 'should match Peach with peach' ) ;
10+
11+ assert . notOk ( AutoComplete . fuzzyFilter ( 'phc' , 'Peach' ) , 'should not match Peach with phc' ) ;
12+ assert . notOk ( AutoComplete . fuzzyFilter ( 'pp' , 'Peach' ) , 'should not match Peach with pp' ) ;
13+ assert . notOk ( AutoComplete . fuzzyFilter ( 'pb' , 'Peach' ) , 'should not match Peach with pb' ) ;
14+ } ) ;
15+ } ) ;
You can’t perform that action at this time.
0 commit comments