feat: activity fuzzy matching (exact > prefix > substring)
2269f6a79319b9ffef2963a01011de76367c4ae3
humdrum-tiv <45084903+humdrum-tiv@users.noreply.github.com> · 2026-07-29 16:56
parent b08829a3
2 files changed
internal/day/match.go +41 −0
@@ -0,0 +1,41 @@
+package day
+
+import "strings"
+
+// ActivityType is the CLI-side view of a Dots activity type.
+type ActivityType struct {
+ ID string
+ Name string
+ Color string
+}
+
+// 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
+}
internal/day/match_test.go +69 −0
@@ -0,0 +1,69 @@
+package day
+
+import "testing"
+
+var types = []ActivityType{
+ {ID: "1", Name: "ARCHER", Color: "#c04000"},
+ {ID: "2", Name: "Archery practice", Color: "#00c040"},
+ {ID: "3", Name: "Foofaraw", Color: "#4000c0"},
+ {ID: "4", Name: "Reading", Color: "#404040"},
+}
+
+var typesWithDuplicates = []ActivityType{
+ {ID: "4a", Name: "Reading", Color: "#404040"},
+ {ID: "4b", Name: "READING", Color: "#505050"},
+}
+
+func names(ms []ActivityType) []string {
+ out := make([]string, len(ms))
+ for i, m := range ms {
+ out[i] = m.Name
+ }
+ return out
+}
+
+func ids(ms []ActivityType) []string {
+ out := make([]string, len(ms))
+ for i, m := range ms {
+ out[i] = m.ID
+ }
+ return out
+}
+
+func TestMatchActivity(t *testing.T) {
+ cases := []struct {
+ query string
+ want []string
+ }{
+ {"archer", []string{"ARCHER"}}, // exact beats prefix
+ {"arch", []string{"ARCHER", "Archery practice"}}, // prefix, both
+ {"foo", []string{"Foofaraw"}},
+ {"ading", []string{"Reading"}}, // substring fallback
+ {"xyz", []string{}},
+ {"READING", []string{"Reading"}},
+ {"", []string{}}, // empty query
+ {" ", []string{}}, // whitespace-only query
+ }
+ for _, c := range cases {
+ got := names(MatchActivity(c.query, types))
+ if len(got) != len(c.want) {
+ t.Fatalf("%q: got %v, want %v", c.query, got, c.want)
+ }
+ for i := range got {
+ if got[i] != c.want[i] {
+ t.Fatalf("%q: got %v, want %v", c.query, got, c.want)
+ }
+ }
+ }
+}
+
+func TestMatchActivityExactTies(t *testing.T) {
+ // When multiple names match exactly (case-insensitive), first wins
+ got := ids(MatchActivity("reading", typesWithDuplicates))
+ if len(got) != 1 {
+ t.Fatalf("reading: got %d results, want 1", len(got))
+ }
+ if got[0] != "4a" {
+ t.Fatalf("reading: got ID %q, want %q (first match)", got[0], "4a")
+ }
+}