accept --cpi=FILE --cpiy=YEAR to translate money to target year
All checks were successful
cicd / ci (push) Successful in 1m30s

This commit is contained in:
Bel LaPointe
2025-05-07 09:55:16 -06:00
parent 0d91cd63db
commit 2f8dba4e23
5 changed files with 48 additions and 11 deletions

View File

@@ -68,15 +68,15 @@ func (n Normalizer) Normalize(deltas ledger.Deltas) ledger.Deltas {
}
func (n Normalizer) NormalizeDelta(delta ledger.Delta) ledger.Delta {
delta.Value /= n.NormalizeFactor(delta)
delta.Value /= n.NormalizeFactor(delta.Name, delta.Date)
return delta
}
func (n Normalizer) NormalizeFactor(delta ledger.Delta) float64 {
func (n Normalizer) NormalizeFactor(name, date string) float64 {
for pattern := range n.m {
if regexp.MustCompile(pattern).MatchString(delta.Name) {
if regexp.MustCompile(pattern).MatchString(name) {
for _, normalize := range n.m[pattern] {
if normalize.startDate < delta.Date {
if normalize.startDate < date {
return normalize.factor
}
}

View File

@@ -9,6 +9,10 @@ import (
"time"
)
type Normalizer interface {
NormalizeFactor(string, string) float64
}
type Balances map[string]Balance
type Balance map[Currency]float64
@@ -246,3 +250,14 @@ func (balance Balance) Debug() string {
}
return strings.Join(result, " + ")
}
func (balances Balances) Normalize(n Normalizer, date string) Balances {
result := make(Balances)
for name, balance := range balances {
result[name] = make(Balance)
for currency, value := range balance {
result[name][currency] = value / n.NormalizeFactor(name, date)
}
}
return result
}