Replace the multi-pass where() chain in Apply() with a single loop that evaluates all filter predicates per item and skips immediately on first mismatch. This eliminates N intermediate slice allocations (one per active filter) and avoids re-scanning the full dataset for each filter dimension. Key changes in filter.go: - Single loop with continue-on-mismatch for BOGO, category, department, and query filters — combined categories check scans item.Categories once for both BOGO and category instead of twice - Pre-allocate result slice capped at min(len(items), opts.Limit) to avoid grow-and-copy churn - Fast-path bypass when no filters are active (just apply limit) - Break early once limit is reached instead of filtering everything and truncating after - Remove the now-unused where() helper function - Add early-return fast paths to CleanText() for the common case where input contains no HTML entities or newlines, avoiding unnecessary html.UnescapeString and ReplaceAll calls Test coverage: - filter_equivalence_test.go (new): Reference implementation of the original multi-pass algorithm with 500 randomized test cases verifying behavioral equivalence. Includes allocation budget guardrail (<=80 allocs/op for 1k items) to catch accidental regression to multi-pass. Benchmarks for new vs legacy reference on identical workload. - filter_test.go: Benchmark comparisons for CleanText on plain text (fast path) vs escaped HTML (full path), new vs legacy. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
135 lines
2.9 KiB
Go
135 lines
2.9 KiB
Go
package filter
|
|
|
|
import (
|
|
"html"
|
|
"strings"
|
|
|
|
"github.com/tayloree/publix-deals/internal/api"
|
|
)
|
|
|
|
// Options holds all filter criteria.
|
|
type Options struct {
|
|
BOGO bool
|
|
Category string
|
|
Department string
|
|
Query string
|
|
Limit int
|
|
}
|
|
|
|
// Apply filters a slice of SavingItems according to the given options.
|
|
func Apply(items []api.SavingItem, opts Options) []api.SavingItem {
|
|
wantCategory := opts.Category != ""
|
|
wantDepartment := opts.Department != ""
|
|
wantQuery := opts.Query != ""
|
|
needsFiltering := opts.BOGO || wantCategory || wantDepartment || wantQuery
|
|
|
|
if !needsFiltering {
|
|
if opts.Limit > 0 && opts.Limit < len(items) {
|
|
return items[:opts.Limit]
|
|
}
|
|
return items
|
|
}
|
|
|
|
var result []api.SavingItem
|
|
if opts.Limit > 0 && opts.Limit < len(items) {
|
|
result = make([]api.SavingItem, 0, opts.Limit)
|
|
} else {
|
|
result = make([]api.SavingItem, 0, len(items))
|
|
}
|
|
|
|
category := opts.Category
|
|
department := strings.ToLower(opts.Department)
|
|
query := strings.ToLower(opts.Query)
|
|
|
|
for _, item := range items {
|
|
if opts.BOGO || wantCategory {
|
|
hasBogo := !opts.BOGO
|
|
hasCategory := !wantCategory
|
|
|
|
for _, c := range item.Categories {
|
|
if !hasBogo && strings.EqualFold(c, "bogo") {
|
|
hasBogo = true
|
|
}
|
|
if !hasCategory && strings.EqualFold(c, category) {
|
|
hasCategory = true
|
|
}
|
|
if hasBogo && hasCategory {
|
|
break
|
|
}
|
|
}
|
|
|
|
if !hasBogo || !hasCategory {
|
|
continue
|
|
}
|
|
}
|
|
|
|
if wantDepartment && !strings.Contains(strings.ToLower(Deref(item.Department)), department) {
|
|
continue
|
|
}
|
|
|
|
if wantQuery {
|
|
title := strings.ToLower(CleanText(Deref(item.Title)))
|
|
desc := strings.ToLower(CleanText(Deref(item.Description)))
|
|
if !strings.Contains(title, query) && !strings.Contains(desc, query) {
|
|
continue
|
|
}
|
|
}
|
|
|
|
result = append(result, item)
|
|
if opts.Limit > 0 && len(result) >= opts.Limit {
|
|
break
|
|
}
|
|
}
|
|
|
|
if len(result) == 0 {
|
|
return nil
|
|
}
|
|
return result
|
|
}
|
|
|
|
// Categories returns a map of category name to count across all items.
|
|
func Categories(items []api.SavingItem) map[string]int {
|
|
cats := make(map[string]int)
|
|
for _, item := range items {
|
|
for _, c := range item.Categories {
|
|
cats[c]++
|
|
}
|
|
}
|
|
return cats
|
|
}
|
|
|
|
// Deref safely dereferences a string pointer, returning "" for nil.
|
|
func Deref(s *string) string {
|
|
if s == nil {
|
|
return ""
|
|
}
|
|
return *s
|
|
}
|
|
|
|
// CleanText unescapes HTML entities and normalizes whitespace.
|
|
func CleanText(s string) string {
|
|
if !strings.ContainsAny(s, "&\r\n") {
|
|
return strings.TrimSpace(s)
|
|
}
|
|
|
|
s = html.UnescapeString(s)
|
|
if !strings.ContainsAny(s, "\r\n") {
|
|
return strings.TrimSpace(s)
|
|
}
|
|
|
|
s = strings.ReplaceAll(s, "\r\n", " ")
|
|
s = strings.ReplaceAll(s, "\r", " ")
|
|
s = strings.ReplaceAll(s, "\n", " ")
|
|
return strings.TrimSpace(s)
|
|
}
|
|
|
|
// ContainsIgnoreCase reports whether any element in slice matches val case-insensitively.
|
|
func ContainsIgnoreCase(slice []string, val string) bool {
|
|
for _, s := range slice {
|
|
if strings.EqualFold(s, val) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|