package ledger

import (
	"regexp"
)

type Group func(Delta) Delta

type Groups []Group

func (groups Groups) Each(d Delta) Delta {
	for i := range groups {
		d = groups[i](d)
	}
	return d
}

func GroupDate(pattern string) Group {
	p := regexp.MustCompile(pattern)
	return func(d Delta) Delta {
		d.Date = p.FindString(d.Date)
		for i := range d.with {
			d.with[i].Date = p.FindString(d.with[i].Date)
		}
		return d
	}
}

func GroupName(pattern string) Group {
	p := regexp.MustCompile(pattern)
	return func(d Delta) Delta {
		d.Name = p.FindString(d.Name)
		for i := range d.with {
			d.with[i].Name = p.FindString(d.with[i].Name)
		}
		return d
	}
}
