Skip to content

Commit

Permalink
Add IntSliceToFloat64 and SliceToFloat64
Browse files Browse the repository at this point in the history
These two helper functions help make an easy conversion to the []float64 slice needed for our chart values.
  • Loading branch information
jentfoo committed Feb 10, 2025
1 parent ec85807 commit a88bf2d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
14 changes: 14 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ func reverseSlice[T any](s []T) {
}
}

// SliceToFloat64 converts a slice of arbitrary types to float64 to be used as chart values.
func SliceToFloat64[T any](slice []T, conversion func(T) float64) []float64 {
result := make([]float64, len(slice))
for i, v := range slice {
result[i] = conversion(v)
}
return result
}

// IntSliceToFloat64 converts an int slice to a float64 slice so that it can be used for chart values.
func IntSliceToFloat64(slice []int) []float64 {
return SliceToFloat64(slice, func(i int) float64 { return float64(i) })
}

func parseFlexibleValue(value string, percentTotal float64) (float64, error) {
if strings.HasSuffix(value, "%") {
percent, err := strconv.ParseFloat(strings.TrimSuffix(value, "%"), 64)
Expand Down
73 changes: 73 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package charts

import (
"math"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -113,6 +114,78 @@ func TestReverseSlice(t *testing.T) {
assert.Equal(t, []int{9, 7, 5, 3, 1}, numbers)
}

func TestSliceToFloat64(t *testing.T) {
t.Parallel()

t.Run("int", func(t *testing.T) {
input := []int{1, 2, 3, 4}
expected := []float64{1.0, 2.0, 3.0, 4.0}
result := SliceToFloat64(input, func(i int) float64 { return float64(i) })
assert.Equal(t, expected, result)
})
t.Run("string", func(t *testing.T) {
input := []string{"1.5", "2.5", "3.5"}
expected := []float64{1.5, 2.5, 3.5}
result := SliceToFloat64(input, func(s string) float64 {
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
return 0
})
assert.Equal(t, expected, result)
})
t.Run("empty", func(t *testing.T) {
input := []string{}
expected := []float64{}
result := SliceToFloat64(input, func(s string) float64 { return 0 })
assert.Equal(t, expected, result)
})
t.Run("nil", func(t *testing.T) {
var input []int
expected := []float64{}
result := SliceToFloat64(input, func(i int) float64 { return float64(i) })
assert.Equal(t, expected, result)
})
}

// TestIntSliceToFloat64 tests the IntSliceToFloat64 convenience function.
func TestIntSliceToFloat64(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input []int
want []float64
}{
{
name: "positive",
input: []int{1, 2, 3},
want: []float64{1.0, 2.0, 3.0},
},
{
name: "negative",
input: []int{0, -1, -2},
want: []float64{0.0, -1.0, -2.0},
},
{
name: "empty",
input: []int{},
want: []float64{},
},
{
name: "nil",
input: nil,
want: []float64{},
},
}

for i, tt := range tests {
t.Run(strconv.Itoa(i)+"-"+tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, IntSliceToFloat64(tt.input))
})
}
}

func TestParseFlexibleValue(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit a88bf2d

Please sign in to comment.