ledger.Balances accept Like, Group kinda
parent
0eaeeec359
commit
fb1ddc72c3
|
|
@ -46,6 +46,34 @@ func (balances Balances) NotLike(pattern string) Balances {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (balances Balances) KindaLike(like Like) Balances {
|
||||||
|
ks := []string{}
|
||||||
|
for k := range balances {
|
||||||
|
ks = append(ks, k)
|
||||||
|
}
|
||||||
|
sort.Strings(ks)
|
||||||
|
|
||||||
|
result := make(Balances)
|
||||||
|
for _, k := range ks {
|
||||||
|
v := balances[k]
|
||||||
|
if like(v.kinda(k)) {
|
||||||
|
result[k] = maps.Clone(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b Balance) kinda(k string) Delta {
|
||||||
|
return Delta{
|
||||||
|
Date: time.Now().Format("2006-01-02"),
|
||||||
|
Name: k,
|
||||||
|
Value: b[USD],
|
||||||
|
Description: k,
|
||||||
|
Transaction: k,
|
||||||
|
Payee: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
||||||
|
|
@ -157,16 +157,16 @@ P 2023-10-22 07:33:56 GME $17.18
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if got := got["USDC"].Lookup("2099-01-01"); got != 0 {
|
if got := got["USDC"].Lookup("2099-01-01"); got != nil {
|
||||||
t.Error("default got != 0:", got)
|
t.Error("default got != 0:", got)
|
||||||
}
|
}
|
||||||
if got := got["GME"].Lookup("2099-01-01"); got != 17.18 {
|
if got := got["GME"].Lookup("2099-01-01"); got == nil || *got != 17.18 {
|
||||||
t.Errorf("shouldve returned latest but got %v", got)
|
t.Errorf("shouldve returned latest but got %v", got)
|
||||||
}
|
}
|
||||||
if got := got["GME"].Lookup("2023-09-19"); got != 18.22 {
|
if got := got["GME"].Lookup("2023-09-19"); got == nil || *got != 18.22 {
|
||||||
t.Errorf("shouldve returned one day before but got %v", got)
|
t.Errorf("shouldve returned one day before but got %v", got)
|
||||||
}
|
}
|
||||||
if got := got["GME"].Lookup("2023-09-18"); got != 18.22 {
|
if got := got["GME"].Lookup("2023-09-18"); got == nil || *got != 18.22 {
|
||||||
t.Errorf("shouldve returned day of but got %v", got)
|
t.Errorf("shouldve returned day of but got %v", got)
|
||||||
}
|
}
|
||||||
t.Log(got)
|
t.Log(got)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue