impl all
This commit is contained in:
@@ -36,3 +36,12 @@ func newDelta(d time.Time, a ledger.Account) Delta {
|
||||
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
|
||||
}
|
||||
|
||||
func (file File) Deltas(likes ...Like) ([]Delta, error) {
|
||||
func (file File) Deltas(like ...Like) ([]Delta, error) {
|
||||
transactions, err := file.transactions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -20,14 +20,7 @@ func (file File) Deltas(likes ...Like) ([]Delta, error) {
|
||||
result := make([]Delta, 0, len(transactions)*2)
|
||||
for _, transaction := range transactions {
|
||||
for _, delta := range newDeltas(transaction) {
|
||||
if got := func() bool {
|
||||
for _, like := range likes {
|
||||
if !like(delta) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}(); !got {
|
||||
if !likes(like).all(delta) {
|
||||
continue
|
||||
}
|
||||
result = append(result, delta)
|
||||
|
||||
@@ -4,6 +4,8 @@ import "regexp"
|
||||
|
||||
type Like func(Delta) bool
|
||||
|
||||
type likes []Like
|
||||
|
||||
func LikeAcc(pattern string) Like {
|
||||
return func(d Delta) bool {
|
||||
return like(pattern, d.Account)
|
||||
@@ -13,3 +15,12 @@ func LikeAcc(pattern string) Like {
|
||||
func like(pattern string, other string) bool {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user