Skip to content

Commit

Permalink
Allow tabs (in between account name and amount). Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed Feb 23, 2016
1 parent 743552f commit 01215d6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
5 changes: 4 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"math/big"
"regexp"
"sort"
"strings"
"time"
Expand All @@ -24,6 +25,8 @@ func ParseLedger(ledgerReader io.Reader) (generalLedger []*Transaction, err erro
scanner := bufio.NewScanner(ledgerReader)
var line string
var lineCount int

accountToAmountSpace := regexp.MustCompile(" {2,}|\t+")
for scanner.Scan() {
line = scanner.Text()
// remove heading and tailing space from the line
Expand Down Expand Up @@ -54,7 +57,7 @@ func ParseLedger(ledgerReader io.Reader) (generalLedger []*Transaction, err erro
trans = &Transaction{Payee: payeeString, Date: transDate}
} else {
var accChange Account
lineSplit := strings.Split(trimmedLine, " ")
lineSplit := accountToAmountSpace.Split(trimmedLine, -1)
nonEmptyWords := []string{}
for _, word := range lineSplit {
if len(word) > 0 {
Expand Down
58 changes: 58 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,64 @@ var testCases = []testCase{
},
nil,
},
testCase{
`1970/01/01 Payee
Expense:test 369.0
Assets
; Handle tabs between account and amount
; Also handle accounts with spaces
1970/01/01 Payee 5
Expense:Cars R Us
Expense:Cars 358.0
Expense:Cranks 10
Expense:Cranks Unlimited 10
Expense:Cranks United 10
`,
[]*Transaction{
&Transaction{
Payee: "Payee",
Date: time.Unix(0, 0).UTC(),
AccountChanges: []Account{
Account{
"Expense:test",
big.NewRat(369.0, 1),
},
Account{
"Assets",
big.NewRat(-369.0, 1),
},
},
},
&Transaction{
Payee: "Payee 5",
Date: time.Unix(0, 0).UTC(),
AccountChanges: []Account{
Account{
"Expense:Cars R Us",
big.NewRat(-388.0, 1),
},
Account{
"Expense:Cars",
big.NewRat(358.0, 1),
},
Account{
"Expense:Cranks",
big.NewRat(10.0, 1),
},
Account{
"Expense:Cranks Unlimited",
big.NewRat(10.0, 1),
},
Account{
"Expense:Cranks United",
big.NewRat(10.0, 1),
},
},
},
},
nil,
},
}

func TestParseLedger(t *testing.T) {
Expand Down

0 comments on commit 01215d6

Please sign in to comment.