From 01215d6a06ae67b2e24ca1c27e2a12720aae17be Mon Sep 17 00:00:00 2001 From: Chris Howey Date: Tue, 23 Feb 2016 13:16:57 -0600 Subject: [PATCH] Allow tabs (in between account name and amount). Fixes #4 --- parse.go | 5 ++++- parse_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/parse.go b/parse.go index 42a2c995..1055cc1b 100644 --- a/parse.go +++ b/parse.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "math/big" + "regexp" "sort" "strings" "time" @@ -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 @@ -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 { diff --git a/parse_test.go b/parse_test.go index 018a01a4..b0e810ca 100644 --- a/parse_test.go +++ b/parse_test.go @@ -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) {