Skip to content

Commit

Permalink
feat: fuzzy find without sorting
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 authored and sahilm committed May 30, 2023
1 parent c72cd38 commit c48e322
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,36 @@ The following types of matches apply a bonus:
Penalties are applied for every character in the search string that wasn't matched and all leading
characters upto the first match.
Results are sorted by best match.
*/
func Find(pattern string, data []string) Matches {
return FindFrom(pattern, stringSource(data))
}

/*
FindNoSort is an alternative Find implementation that does not sort
the results in the end.
*/
func FindNoSort(pattern string, data []string) Matches {
return FindFromNoSort(pattern, stringSource(data))
}

/*
FindFrom is an alternative implementation of Find using a Source
instead of a list of strings.
*/
func FindFrom(pattern string, data Source) Matches {
matches := FindFromNoSort(pattern, data)
sort.Stable(matches)
return matches
}

/*
FindFromNoSort is an alternative FindFrom implementation that does
not sort results in the end.
*/
func FindFromNoSort(pattern string, data Source) Matches {
if len(pattern) == 0 {
return nil
}
Expand Down Expand Up @@ -181,7 +201,6 @@ func FindFrom(pattern string, data Source) Matches {
matchedIndexes = match.MatchedIndexes[:0] // Recycle match index slice
}
}
sort.Stable(matches)
return matches
}

Expand Down

0 comments on commit c48e322

Please sign in to comment.