deltas.Group(GroupName("^[^:]*"))

main
Bel LaPointe 2023-10-25 08:21:16 -06:00
parent 40cec44c0b
commit 54913b21c9
3 changed files with 43 additions and 0 deletions

View File

@ -6,6 +6,14 @@ import (
type Deltas []Delta
func (deltas Deltas) Group(group ...Group) Deltas {
result := make(Deltas, 0, len(deltas))
for i := range deltas {
result = append(result, Groups(group).Each(deltas[i]))
}
return result
}
func (deltas Deltas) Like(like ...Like) Deltas {
result := make(Deltas, 0, len(deltas))
for i := range deltas {

View File

@ -3,6 +3,32 @@ package ledger
import "testing"
func TestDeltas(t *testing.T) {
t.Run("Groups", func(t *testing.T) {
deltas := Deltas{
{Name: "a1"},
{Name: "b1"},
{Name: "b2"},
{Name: "b3"},
}
deltas = deltas.Group(GroupName("^."))
if len(deltas) != 4 {
t.Error(len(deltas))
}
if deltas[0].Name != "a" {
t.Error(deltas[0].Name)
}
if deltas[1].Name != "b" {
t.Error(deltas[1].Name)
}
if deltas[2].Name != "b" {
t.Error(deltas[2].Name)
}
if deltas[3].Name != "b" {
t.Error(deltas[3].Name)
}
})
t.Run("balances", func(t *testing.T) {
deltas := Deltas{
{Name: "a", Value: 0},

View File

@ -4,6 +4,15 @@ 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 {