Add fuzzy match filter
This commit is contained in:
parent
f18d04e87b
commit
b194acdaeb
73
filter.go
73
filter.go
@ -7,50 +7,49 @@ type Filter func([]Suggest, string, bool) []Suggest
|
|||||||
|
|
||||||
// FilterHasPrefix checks whether the string completions.Text begins with sub.
|
// FilterHasPrefix checks whether the string completions.Text begins with sub.
|
||||||
func FilterHasPrefix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
func FilterHasPrefix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||||
if sub == "" {
|
return filterCommon(completions, sub, ignoreCase, strings.HasPrefix)
|
||||||
return completions
|
|
||||||
}
|
|
||||||
if ignoreCase {
|
|
||||||
sub = strings.ToUpper(sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := make([]Suggest, 0, len(completions))
|
|
||||||
for i := range completions {
|
|
||||||
c := completions[i].Text
|
|
||||||
if ignoreCase {
|
|
||||||
c = strings.ToUpper(c)
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(c, sub) {
|
|
||||||
ret = append(ret, completions[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterHasSuffix checks whether the completion.Text ends with sub.
|
// FilterHasSuffix checks whether the completion.Text ends with sub.
|
||||||
func FilterHasSuffix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
func FilterHasSuffix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||||
if sub == "" {
|
return filterCommon(completions, sub, ignoreCase, strings.HasSuffix)
|
||||||
return completions
|
|
||||||
}
|
|
||||||
if ignoreCase {
|
|
||||||
sub = strings.ToUpper(sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := make([]Suggest, 0, len(completions))
|
|
||||||
for i := range completions {
|
|
||||||
c := completions[i].Text
|
|
||||||
if ignoreCase {
|
|
||||||
c = strings.ToUpper(c)
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(c, sub) {
|
|
||||||
ret = append(ret, completions[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterContains checks whether the completion.Text contains sub.
|
// FilterContains checks whether the completion.Text contains sub.
|
||||||
func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||||
|
return filterCommon(completions, sub, ignoreCase, strings.Contains)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterFuzzy checks whether the completion.Text fuzzy matches sub.
|
||||||
|
// Fuzzy searching for "dog" is equivalent to "*d*o*g*". This search term
|
||||||
|
// would match, for example, "Good food is gone"
|
||||||
|
// ^ ^ ^
|
||||||
|
func FilterFuzzy(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||||
|
return filterCommon(completions, sub, ignoreCase, fuzzyMatch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fuzzyMatch(s, sub string) bool {
|
||||||
|
sChars := []rune(s)
|
||||||
|
subChars := []rune(sub)
|
||||||
|
sIdx := 0
|
||||||
|
|
||||||
|
for _, c := range subChars {
|
||||||
|
found := false
|
||||||
|
for ; sIdx < len(sChars); sIdx++ {
|
||||||
|
if sChars[sIdx] == c {
|
||||||
|
found = true
|
||||||
|
sIdx++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterCommon(completions []Suggest, sub string, ignoreCase bool, test func(string, string) bool) []Suggest {
|
||||||
if sub == "" {
|
if sub == "" {
|
||||||
return completions
|
return completions
|
||||||
}
|
}
|
||||||
@ -64,7 +63,7 @@ func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Sugges
|
|||||||
if ignoreCase {
|
if ignoreCase {
|
||||||
c = strings.ToUpper(c)
|
c = strings.ToUpper(c)
|
||||||
}
|
}
|
||||||
if strings.Contains(c, sub) {
|
if test(c, sub) {
|
||||||
ret = append(ret, completions[i])
|
ret = append(ret, completions[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,6 +101,35 @@ func TestFilter(t *testing.T) {
|
|||||||
{Text: "ABCDE"},
|
{Text: "ABCDE"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "Fuzzy don't ignore case",
|
||||||
|
filter: FilterFuzzy,
|
||||||
|
list: []Suggest{
|
||||||
|
{Text: "abcde"},
|
||||||
|
{Text: "fcdej"},
|
||||||
|
{Text: "ABCDE"},
|
||||||
|
},
|
||||||
|
substr: "ae",
|
||||||
|
ignoreCase: false,
|
||||||
|
expected: []Suggest{
|
||||||
|
{Text: "abcde"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
scenario: "Fuzzy ignore case",
|
||||||
|
filter: FilterFuzzy,
|
||||||
|
list: []Suggest{
|
||||||
|
{Text: "abcde"},
|
||||||
|
{Text: "fcdej"},
|
||||||
|
{Text: "ABCDE"},
|
||||||
|
},
|
||||||
|
substr: "ae",
|
||||||
|
ignoreCase: true,
|
||||||
|
expected: []Suggest{
|
||||||
|
{Text: "abcde"},
|
||||||
|
{Text: "ABCDE"},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range scenarioTable {
|
for _, s := range scenarioTable {
|
||||||
@ -109,3 +138,27 @@ func TestFilter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFuzzy(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
s string
|
||||||
|
sub string
|
||||||
|
match bool
|
||||||
|
}{
|
||||||
|
{"dog house", "dog", true},
|
||||||
|
{"dog house", "", true},
|
||||||
|
{"", "", true},
|
||||||
|
{"this is much longer", "hhg", true},
|
||||||
|
{"this is much longer", "hhhg", false},
|
||||||
|
{"long", "longer", false},
|
||||||
|
{"can we do unicode 文字 with this 今日", "文字今日", true},
|
||||||
|
{"can we do unicode 文字 with this 今日", "d文字tt今日", true},
|
||||||
|
{"can we do unicode 文字 with this 今日", "d文字ttt今日", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
if fuzzyMatch(test.s, test.sub) != test.match {
|
||||||
|
t.Errorf("fuzzymatch, %s in %s: expected %v, got %v", test.sub, test.s, test.match, !test.match)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user