impl ana.Normalizer
parent
ed0ce83556
commit
5cc7b7ec55
|
|
@ -0,0 +1,63 @@
|
||||||
|
package ana
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gogs.inhome.blapointe.com/ana-ledger/src/ledger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Normalizer struct {
|
||||||
|
m map[string][]normalize
|
||||||
|
}
|
||||||
|
|
||||||
|
type normalize struct {
|
||||||
|
startDate string
|
||||||
|
factor float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNormalizer() Normalizer {
|
||||||
|
return Normalizer{
|
||||||
|
m: make(map[string][]normalize),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n Normalizer) With(pattern, startDate string, factor float64) Normalizer {
|
||||||
|
n.m[pattern] = append(n.m[pattern], normalize{startDate: startDate, factor: factor})
|
||||||
|
slices.SortFunc(n.m[pattern], func(a, b normalize) int {
|
||||||
|
return -1 * strings.Compare(a.startDate, b.startDate)
|
||||||
|
})
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n Normalizer) Normalize(deltas ledger.Deltas) ledger.Deltas {
|
||||||
|
deltas = slices.Clone(deltas)
|
||||||
|
|
||||||
|
patterns := []string{}
|
||||||
|
for pattern := range n.m {
|
||||||
|
patterns = append(patterns, pattern)
|
||||||
|
}
|
||||||
|
like := ledger.LikeName(strings.Join(patterns, "|"))
|
||||||
|
|
||||||
|
for i, delta := range deltas {
|
||||||
|
if !like(delta) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
func() {
|
||||||
|
for pattern := range n.m {
|
||||||
|
if regexp.MustCompile(pattern).MatchString(delta.Name) {
|
||||||
|
for _, normalize := range n.m[pattern] {
|
||||||
|
if normalize.startDate < delta.Date {
|
||||||
|
delta.Value /= normalize.factor
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
deltas[i] = delta
|
||||||
|
}
|
||||||
|
|
||||||
|
return deltas
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package ana
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gogs.inhome.blapointe.com/ana-ledger/src/ledger"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNormalize(t *testing.T) {
|
||||||
|
deltas := ledger.Deltas{
|
||||||
|
ledger.Delta{Date: "2024-12-04", Value: 100, Name: "Bel:Withdrawal"},
|
||||||
|
ledger.Delta{Date: "2024-12-06", Value: 100, Name: "Bel:Withdrawal"},
|
||||||
|
}
|
||||||
|
|
||||||
|
normalizer := NewNormalizer().
|
||||||
|
With("^Bel:", "2024-12-05", 190_000).
|
||||||
|
With("^Bel:", "2018-02-16", 86_000)
|
||||||
|
|
||||||
|
normalized := normalizer.Normalize(deltas)
|
||||||
|
for i, delta := range normalized {
|
||||||
|
t.Logf("[%d] %+v", i, delta)
|
||||||
|
}
|
||||||
|
|
||||||
|
if normalized[0].Value != 100/float64(86_000) {
|
||||||
|
t.Errorf("earliest transaction didnt use earliest salary")
|
||||||
|
}
|
||||||
|
if normalized[1].Value != 100/float64(190_000) {
|
||||||
|
t.Errorf("latest transaction didnt use latest salary")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue