▍ humdrum codex / dots v1.0.0
1022 B raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package day

import "strings"

// ActivityType is the CLI-side view of a Dots activity type.
type ActivityType struct {
	ID    string `json:"id"`
	Name  string `json:"name"`
	Color string `json:"color"`
}

// MatchActivity finds candidate types for a query: exact (case-insensitive)
// wins outright; if multiple names match exactly, the first wins; otherwise all
// prefix matches; otherwise all substring matches. Empty or whitespace-only queries
// return no results.
func MatchActivity(query string, types []ActivityType) []ActivityType {
	q := strings.TrimSpace(query)
	if q == "" {
		return []ActivityType{}
	}
	q = strings.ToLower(q)
	var prefix, substr []ActivityType
	for _, t := range types {
		n := strings.ToLower(t.Name)
		if n == q {
			return []ActivityType{t}
		}
		if strings.HasPrefix(n, q) {
			prefix = append(prefix, t)
		} else if strings.Contains(n, q) {
			substr = append(substr, t)
		}
	}
	if len(prefix) > 0 {
		return prefix
	}
	if substr == nil {
		return []ActivityType{}
	}
	return substr
}