summaryrefslogtreecommitdiffstats
path: root/pkg/utils/fuzzy_search.go
blob: 5fce3dde90250b9a548231cd501878bf00d107bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package utils

import (
	"sort"

	"github.com/jesseduffield/generics/slices"
	"github.com/sahilm/fuzzy"
)

func FuzzySearch(needle string, haystack []string) []string {
	if needle == "" {
		return []string{}
	}

	matches := fuzzy.Find(needle, haystack)
	sort.Sort(matches)

	return slices.Map(matches, func(match fuzzy.Match) string {
		return match.Str
	})
}