make ledger.Deltas.Transactions()
cicd / ci (push) Successful in 1m8s
Details
cicd / ci (push) Successful in 1m8s
Details
parent
e8f42c7a5d
commit
80ca2ea61e
|
|
@ -10,9 +10,35 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"unicode"
|
"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 {
|
type transaction struct {
|
||||||
date string
|
date string
|
||||||
description string
|
description string
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,25 @@ import (
|
||||||
"testing"
|
"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) {
|
func TestReadTransaction(t *testing.T) {
|
||||||
cases := map[string]struct {
|
cases := map[string]struct {
|
||||||
input string
|
input string
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue