impl all
parent
078c425d9e
commit
e5bc3b3840
|
|
@ -36,3 +36,12 @@ func newDelta(d time.Time, a ledger.Account) Delta {
|
||||||
Currency: USD, // TODO
|
Currency: USD, // TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (delta Delta) Plus(other Delta) Delta {
|
||||||
|
return Delta{
|
||||||
|
Date: other.Date,
|
||||||
|
Account: delta.Account,
|
||||||
|
Value: delta.Value + other.Value,
|
||||||
|
Currency: other.Currency,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ func NewFile(p string) (File, error) {
|
||||||
return f, err
|
return f, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (file File) Deltas(likes ...Like) ([]Delta, error) {
|
func (file File) Deltas(like ...Like) ([]Delta, error) {
|
||||||
transactions, err := file.transactions()
|
transactions, err := file.transactions()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -20,14 +20,7 @@ func (file File) Deltas(likes ...Like) ([]Delta, error) {
|
||||||
result := make([]Delta, 0, len(transactions)*2)
|
result := make([]Delta, 0, len(transactions)*2)
|
||||||
for _, transaction := range transactions {
|
for _, transaction := range transactions {
|
||||||
for _, delta := range newDeltas(transaction) {
|
for _, delta := range newDeltas(transaction) {
|
||||||
if got := func() bool {
|
if !likes(like).all(delta) {
|
||||||
for _, like := range likes {
|
|
||||||
if !like(delta) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}(); !got {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
result = append(result, delta)
|
result = append(result, delta)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import "regexp"
|
||||||
|
|
||||||
type Like func(Delta) bool
|
type Like func(Delta) bool
|
||||||
|
|
||||||
|
type likes []Like
|
||||||
|
|
||||||
func LikeAcc(pattern string) Like {
|
func LikeAcc(pattern string) Like {
|
||||||
return func(d Delta) bool {
|
return func(d Delta) bool {
|
||||||
return like(pattern, d.Account)
|
return like(pattern, d.Account)
|
||||||
|
|
@ -13,3 +15,12 @@ func LikeAcc(pattern string) Like {
|
||||||
func like(pattern string, other string) bool {
|
func like(pattern string, other string) bool {
|
||||||
return regexp.MustCompile(pattern).MatchString(other)
|
return regexp.MustCompile(pattern).MatchString(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (likes likes) all(delta Delta) bool {
|
||||||
|
for i := range likes {
|
||||||
|
if !likes[i](delta) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue