github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/app/lib/search/result/result.go (about)

     1  // Package result - Content managed by Project Forge, see [projectforge.md] for details.
     2  package result
     3  
     4  import (
     5  	"cmp"
     6  	"slices"
     7  	"strings"
     8  )
     9  
    10  type Result struct {
    11  	Type    string  `json:"type,omitempty"`
    12  	ID      string  `json:"id,omitempty"`
    13  	Title   string  `json:"title,omitempty"`
    14  	Icon    string  `json:"icon,omitempty"`
    15  	URL     string  `json:"url,omitempty"`
    16  	Matches Matches `json:"matches,omitempty"`
    17  	Data    any     `json:"data,omitempty"`
    18  	HTML    string  `json:"-"`
    19  }
    20  
    21  func NewResult(t string, id string, url string, title string, icon string, diff any, data any, q string) *Result {
    22  	return &Result{Type: t, ID: id, URL: url, Title: title, Icon: icon, Matches: MatchesFor("", diff, q), Data: data}
    23  }
    24  
    25  type Results []*Result
    26  
    27  func (rs Results) Sort() Results {
    28  	slices.SortFunc(rs, func(l *Result, r *Result) int {
    29  		if l.Type == r.Type {
    30  			return cmp.Compare(strings.ToLower(l.Title), strings.ToLower(r.Title))
    31  		}
    32  		return cmp.Compare(l.Type, r.Type)
    33  	})
    34  	return rs
    35  }