55 lines
1.1 KiB
Go
Raw Normal View History

2019-06-03 09:32:43 +00:00
package search
import (
"sort"
"strings"
"github.com/animenotifier/notify.moe/arn"
"github.com/animenotifier/notify.moe/arn/stringutils"
)
// Companies searches all companies.
func Companies(originalTerm string, maxLength int) []*arn.Company {
term := strings.ToLower(stringutils.RemoveSpecialCharacters(originalTerm))
results := make([]*Result, 0, maxLength)
for company := range arn.StreamCompanies() {
if company.ID == originalTerm {
return []*arn.Company{company}
}
if company.IsDraft {
continue
}
text := strings.ToLower(stringutils.RemoveSpecialCharacters(company.Name.English))
similarity := stringutils.AdvancedStringSimilarity(term, text)
if similarity >= MinimumStringSimilarity {
results = append(results, &Result{
obj: company,
similarity: similarity,
})
}
}
// Sort
sort.Slice(results, func(i, j int) bool {
return results[i].similarity > results[j].similarity
})
// Limit
if len(results) >= maxLength {
results = results[:maxLength]
}
// Final list
final := make([]*arn.Company, len(results))
for i, result := range results {
final[i] = result.obj.(*arn.Company)
}
return final
}