filter

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package filter is the single source of truth for the todo list's filter query language. It parses free-text qualifiers (label:, date:, has:, and bare search terms) into a Query and evaluates it against a board's todos, preserving tree context by keeping every ancestor of a match visible.

The package has no dependency on the models or db packages: callers map their persisted todos into the small Item projection. The reference date ("today") is supplied by the caller so date presets (week, today, tomorrow) resolve deterministically and are unit-testable without touching the system clock.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	ID            int64
	ParentID      *int64
	Position      int // sibling index (used for stable ordering when sorting)
	Title         string
	Description   string
	Completed     bool
	Labels        []string
	Priority      string // priority name, "" = none
	PriorityRank  int    // user-defined priority order (-1 = no rank / ad-hoc)
	DueDate       string // "YYYY-MM-DD" or "" (none)
	HasRecurrence bool
}

Item is the projection of a todo needed to evaluate a filter.

func Apply

func Apply(items []Item, q Query, today string) []Item

Apply evaluates q against a board's todos and returns the subset that should be visible: every matching item plus the ancestors that keep the tree connected, in the original input order. An empty query returns all items unchanged. today is the reference ISO date.

func Sort

func Sort(items []Item, q Query) []Item

Sort reorders items so each sibling group is sorted by the query's sort keys in declared order, preserving input order for equal keys (normally the database manual order by position then id). It also reassigns each item's Position to its new 0-based sibling index, so downstream consumers (e.g. the rendered tree) reflect the new order without bespoke handling. When q has no sort keys the slice is returned unchanged.

Sort operates per sibling group (items sharing a parent) so the tree's hierarchical structure is preserved — sorting never moves a todo out of its parent.

type Query

type Query struct {
	// contains filtered or unexported fields
}

Query is a parsed filter expression. All criteria are ANDed; a todo matches only when it satisfies every active criterion. Its fields are unexported so the grammar stays in one place; use Parse and Query.Match.

func Parse

func Parse(input string) (Query, error)

Parse tokenises the query text into a Query. label and date accept a leading "!" for negation; has accepts one of complete/label/recur/date. Unrecognized date or has values return an error so the caller can surface it rather than silently dropping the token.

func (Query) Empty

func (q Query) Empty() bool

Empty reports whether the query has no criteria (matches everything).

func (Query) Match

func (q Query) Match(it Item, today, weekEnd string) bool

Match reports whether the item satisfies every active criterion. today is the reference ISO date (YYYY-MM-DD); weekEnd is today+7. Label and priority matching is case-insensitive.

func (Query) Sorts

func (q Query) Sorts() []SortKey

Sorts returns the ordered sort criteria attached to q (empty when none). It is read-only so callers cannot mutate the query's sort list.

type SortKey

type SortKey struct {
	Field string
	Desc  bool
}

SortKey is a single ordered sort criterion attached to a Query. Field is one of "priority", "label", "date"; Desc reverses the comparison. Multiple SortKeys apply in declared order with stable tie-breaking on position/id.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL