Compare commits

..

No commits in common. "main" and "origin" have entirely different histories.
main ... origin

2 changed files with 8 additions and 73 deletions

View File

@ -5,7 +5,6 @@ import (
"math" "math"
"os" "os"
"regexp" "regexp"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -30,9 +29,7 @@ type Value struct {
Value float64 Value float64
} }
type Values []Value type Categories map[string][]Value
type Categories map[string]Values
func (c Categories) Insert(company, value string) error { func (c Categories) Insert(company, value string) error {
fValue, err := strconv.ParseFloat(value, 32) fValue, err := strconv.ParseFloat(value, 32)
@ -136,16 +133,19 @@ func Create(out, dir string) error {
return fmt.Errorf("failed to create new sheet: %w", err) return fmt.Errorf("failed to create new sheet: %w", err)
} }
item = CalcAndSort(item) m := map[string]float64{}
for _, i := range item {
m[i.Company] += i.Value
}
valueLength := 0 valueLength := 0
for _, i := range item { for k, v := range m {
if err := f.SetCellStr(cat, fmt.Sprintf("A%d", valueLength+1), i.Company); err != nil { if err := f.SetCellStr(cat, fmt.Sprintf("A%d", valueLength+1), k); err != nil {
return fmt.Errorf("failed to set cell: %w", err) return fmt.Errorf("failed to set cell: %w", err)
} }
if err := f.SetCellFloat(cat, fmt.Sprintf("B%d", valueLength+1), i.Value, 2, 64); err != nil { if err := f.SetCellFloat(cat, fmt.Sprintf("B%d", valueLength+1), v, 2, 64); err != nil {
return fmt.Errorf("failed to set cell: %w", err) return fmt.Errorf("failed to set cell: %w", err)
} }
@ -220,32 +220,3 @@ func createCategoryChart(category string, length int, f *excelize.File) error {
return nil return nil
} }
func CalcAndSort(v Values) Values {
n := Values{}
for _, i := range v {
added := false
for k, v := range n {
if i.Company == v.Company {
n[k].Value += i.Value
added = true
break
}
}
if !added {
n = append(n, i)
}
}
// Sorting for company names.
sort.SliceStable(n, func(i, j int) bool {
return n[i].Company < n[j].Company
})
return n
}

View File

@ -31,7 +31,6 @@ func TestCreate(t *testing.T) {
"aldi_10.00.jpg", "aldi_10.00.jpg",
"edeka_10.11.jpg", "edeka_10.11.jpg",
"casa espana_20221216_62.19.jpg", "casa espana_20221216_62.19.jpg",
"1und1_202302011_61.97_BAR.jpg",
}, },
map[string][]want{ map[string][]want{
"Food": { "Food": {
@ -51,10 +50,6 @@ func TestCreate(t *testing.T) {
}, },
}, },
"Other": { "Other": {
{
"1UND1",
"61.97",
},
{ {
"BMA-ZON", "BMA-ZON",
"12.34", "12.34",
@ -129,34 +124,3 @@ func TestCategoriesInsert(t *testing.T) {
is.Equal(dirtoexcel.Categories{"NonFood": []dirtoexcel.Value{{"AMAZON", 11.11}}}, m) is.Equal(dirtoexcel.Categories{"NonFood": []dirtoexcel.Value{{"AMAZON", 11.11}}}, m)
} }
func TestCalcAndSort(t *testing.T) {
tables := []struct {
name string
input dirtoexcel.Values
expected dirtoexcel.Values
}{
{
"00",
dirtoexcel.Values{
dirtoexcel.Value{Company: "BAR", Value: 3},
dirtoexcel.Value{Company: "ALDI", Value: 1},
dirtoexcel.Value{Company: "ALDI", Value: 2},
},
dirtoexcel.Values{
dirtoexcel.Value{Company: "ALDI", Value: 3},
dirtoexcel.Value{Company: "BAR", Value: 3},
},
},
}
is := is.New(t)
for _, tt := range tables {
tt := tt
t.Run(tt.name, func(t *testing.T) {
is.Equal(dirtoexcel.CalcAndSort(tt.input), tt.expected)
})
}
}