Skip to content

Commit

Permalink
add fuzzySearch()
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Apr 3, 2023
1 parent fe841e8 commit cf166fc
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,9 @@ func Search(items Items, search string, opts ...SearchOption) Matches {

// Perform the search for each item.
for index := start; index < end; index++ {
item := items.ItemString(index)

// If case-insensitive, convert the item to lowercase.
if !o.caseSensitive {
item = strings.ToLower(item)
}

// Create a slice to store the matched indexes.
matchedIndexes := make([]int, 0, len(search))
j := 0

// Check for matching between the item's characters and the search string.
for i, r := range item {
if j < len(search) && r == rune(search[j]) {
matchedIndexes = append(matchedIndexes, i)
j++
}
}

// If all characters in the search string match, add the match to the local matches.
if j == len(search) {
m := Match{Str: items.ItemString(index), Index: index, MatchedIndexes: matchedIndexes}
m, ok := fuzzySearch(items.ItemString(index), search, o)
if ok {
m.Index = index
localMatches = append(localMatches, m)
}
}
Expand Down Expand Up @@ -152,3 +133,31 @@ func Search(items Items, search string, opts ...SearchOption) Matches {
result.sort()
return result
}

func fuzzySearch(str, search string, o searchOption) (Match, bool) {
item := str

// If case-insensitive, convert the item to lowercase.
if !o.caseSensitive {
item = strings.ToLower(item)
}

// Create a slice to store the matched indexes.
matchedIndexes := make([]int, 0, len(search))
j := 0

// Check for matching between the item's characters and the search string.
for i, r := range item {
if j < len(search) && r == rune(search[j]) {
matchedIndexes = append(matchedIndexes, i)
j++
}
}

// Returns Match if all characters in the search string match.
if j == len(search) {
return Match{Str: str, MatchedIndexes: matchedIndexes}, true
} else {
return Match{}, false
}
}

0 comments on commit cf166fc

Please sign in to comment.