Searching is case-insensitive unless capital letters are used.
MODIFY
util/fuzzy/fuzzy.go
util/fuzzy/fuzzy.go
@@ -12,28 +12,36 @@ type SearchResult struct {
func ScoreSearch(searchTerm string, searchItems []string) map[string]float64 {
mymap := make(map[string]float64)
+ // Ignore case if there are no capital letters in the search term.
+ caseInsensitive := searchTerm == strings.ToLower(searchTerm)
+ if caseInsensitive {
searchTerm = strings.ToLower(searchTerm)
+ }
for _, str := range searchItems {
+ if caseInsensitive {
mymap[str] = Score(searchTerm, strings.ToLower(str))
+ } else {
+ mymap[str] = Score(searchTerm, str)
+ }
}
return mymap
}
func SortSearchResults(results map[string]float64) []SearchResult {
+ // Convert the results into an array.
var arr []SearchResult
for key, value := range results {
if value >= 0.0 {
arr = append(arr, SearchResult{key, value})
}
}
-
+ // Sort results based on score. Equal scores are sorted alphbetically.
sort.Slice(arr, func(i, j int) bool {
if arr[i].Value == arr[j].Value {
return strings.Compare(strings.ToLower(arr[i].Key), strings.ToLower(arr[j].Key)) < 0
}
return arr[i].Value > arr[j].Value
})
-
return arr
}

MODIFY
util/fuzzy/fuzzy_test.go
util/fuzzy/fuzzy_test.go
@@ -5,13 +5,13 @@ import (
)
func TestFuzzyScore(t *testing.T) {
- scoreA := Score("Marc", "Marcel#4759")
+ scoreA := Score("Marc", "Marcel#3414")
scoreB := Score("Marc", "Maurice")
if scoreA <= scoreB {
t.Errorf("Incorrect score rating")
}
- scoreC := Score("Marc", "Marchesi#4377")
+ scoreC := Score("Marc", "Marchesi#9331")
if scoreC <= scoreB {
t.Errorf("Incorrect score rating")
}
@@ -33,4 +33,20 @@ func TestFuzzyScore(t *testing.T) {
t.Errorf("Expected\n%v\nbut received\n%v\n", expected, results)
}
+ if Score("S", "cvcx") >= 0 {
+ t.Errorf("Expected score to be < 0\n")
+ }
+
+ if Score("S", "scheme god#2313") >= 0 {
+ t.Errorf("Expected score to be < 0\n")
+ }
+
+ if Score("K", "KC") <= 0 {
+ t.Errorf("Expected score to be < 0\n")
+ }
+
+ if Score("s", "space") <= 0 {
+ t.Errorf("Expected score to be < 0\n")
+ }
+
}

Searching is case-insensitive unless capital letters are used.