make ledger.Deltas.Transactions()
cicd / ci (push) Successful in 1m8s Details

main
Bel LaPointe 2024-07-20 11:29:10 -06:00
parent e8f42c7a5d
commit 80ca2ea61e
2 changed files with 45 additions and 0 deletions

View File

@ -10,9 +10,35 @@ import (
"regexp"
"slices"
"strconv"
"strings"
"unicode"
)
type Transaction Deltas
type Transactions []Transaction
func (deltas Deltas) Transactions() Transactions {
m := make(map[string]Transaction)
for _, d := range deltas {
m[d.Transaction] = append(m[d.Transaction], d)
}
result := make(Transactions, 0, len(m))
for _, v := range m {
result = append(result, v)
}
slices.SortFunc(result, func(a, b Transaction) int {
if a[0].Date == b[0].Date {
return strings.Compare(a[0].Transaction, b[0].Transaction)
}
return strings.Compare(a[0].Date, b[0].Date)
})
return result
}
type transaction struct {
date string
description string

View File

@ -8,6 +8,25 @@ import (
"testing"
)
func TestDeltasTransactions(t *testing.T) {
given := Deltas{
Delta{Date: "2", Name: "x", Transaction: "a"},
Delta{Date: "2", Name: "y", Transaction: "a"},
Delta{Date: "1", Name: "z", Transaction: "b"},
}
got := given.Transactions()
if len(got) != 2 {
t.Error(len(got))
}
if len(got[0]) != 1 {
t.Error("first xaction is not earliest date", len(got[0]))
}
if len(got[1]) != 2 {
t.Error("second xaction is not latest date", len(got[1]))
}
}
func TestReadTransaction(t *testing.T) {
cases := map[string]struct {
input string