deltas.Group(GroupName("^[^:]*"))
This commit is contained in:
@@ -6,6 +6,14 @@ import (
|
|||||||
|
|
||||||
type Deltas []Delta
|
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 {
|
func (deltas Deltas) Like(like ...Like) Deltas {
|
||||||
result := make(Deltas, 0, len(deltas))
|
result := make(Deltas, 0, len(deltas))
|
||||||
for i := range deltas {
|
for i := range deltas {
|
||||||
|
|||||||
@@ -3,6 +3,32 @@ package ledger
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestDeltas(t *testing.T) {
|
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) {
|
t.Run("balances", func(t *testing.T) {
|
||||||
deltas := Deltas{
|
deltas := Deltas{
|
||||||
{Name: "a", Value: 0},
|
{Name: "a", Value: 0},
|
||||||
|
|||||||
@@ -4,6 +4,15 @@ import "regexp"
|
|||||||
|
|
||||||
type Group func(Delta) Delta
|
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 {
|
func GroupDate(pattern string) Group {
|
||||||
p := regexp.MustCompile(pattern)
|
p := regexp.MustCompile(pattern)
|
||||||
return func(d Delta) Delta {
|
return func(d Delta) Delta {
|
||||||
|
|||||||
Reference in New Issue
Block a user