balances.NotLike

main
bel 2024-07-20 10:05:17 -06:00
parent f41386b3b4
commit fe8d80ffc8
3 changed files with 27 additions and 2 deletions

View File

@ -11,6 +11,17 @@ type Balances map[string]Balance
type Balance map[Currency]float64 type Balance map[Currency]float64
func (balances Balances) NotLike(pattern string) Balances {
result := make(Balances)
p := regexp.MustCompile(pattern)
for k, v := range balances {
if !p.MatchString(k) {
result[k] = maps.Clone(v)
}
}
return result
}
func (balances Balances) Like(pattern string) Balances { func (balances Balances) Like(pattern string) Balances {
result := make(Balances) result := make(Balances)
p := regexp.MustCompile(pattern) p := regexp.MustCompile(pattern)

View File

@ -61,6 +61,11 @@ func TestBalances(t *testing.T) {
if len(got) != 1 || got["ab"][USD] != 1.2 { if len(got) != 1 || got["ab"][USD] != 1.2 {
t.Error(got) t.Error(got)
} }
got = was.NotLike(`^ab$`)
if len(got) != 1 || got["a"][USD] != 0.1 {
t.Error(got)
}
}) })
t.Run("group", func(t *testing.T) { t.Run("group", func(t *testing.T) {

View File

@ -8,12 +8,21 @@ type Like func(Delta) bool
type Likes []Like type Likes []Like
func LikeTransaction(delta Delta) Like { func LikeTransactions(deltas ...Delta) Like {
return func(d Delta) bool { return func(d Delta) bool {
return d.Transaction == delta.Transaction for i := range deltas {
if deltas[i].Transaction == d.Transaction {
return true
}
}
return false
} }
} }
func LikeTransaction(delta Delta) Like {
return LikeTransactions(delta)
}
func LikeBefore(date string) Like { func LikeBefore(date string) Like {
return func(d Delta) bool { return func(d Delta) bool {
return date >= d.Date return date >= d.Date